D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
everqlsh
/
.cagefs
/
tmp
/
Filename :
phpy3FVbc
back
Copy
<?php namespace ElementPack\Modules\CharitableDonations\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Typography; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Charitable_Donations extends Module_Base { public function get_name() { return 'bdt-charitable-donations'; } public function get_title() { return BDTEP . __( 'Charitable Donations', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-charitable-donations'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'charitable', 'charity', 'donation', 'donor', 'history', 'charitable', 'wall', 'form', 'donations' ]; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-charitable-donations']; } } public function get_custom_help_url() { return 'https://youtu.be/C38sbaKx9x0'; } protected function register_controls() { $this->start_controls_section( 'section_style_header', [ 'label' => __('Header', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'header_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#e7ebef', 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table th' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'header_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#333', 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table th' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'header_border_style', [ 'label' => __('Border Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'none' => __('None', 'bdthemes-element-pack'), 'solid' => __('Solid', 'bdthemes-element-pack'), 'double' => __('Double', 'bdthemes-element-pack'), 'dotted' => __('Dotted', 'bdthemes-element-pack'), 'dashed' => __('Dashed', 'bdthemes-element-pack'), 'groove' => __('Groove', 'bdthemes-element-pack'), ], 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table th' => 'border-style: {{VALUE}};', ], ] ); $this->add_control( 'header_border_width', [ 'label' => __('Border Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 20, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table th' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'header_border_color', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#ccc', 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table th' => 'border-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'header_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'default' => [ 'top' => 1, 'bottom' => 1, 'left' => 1, 'right' => 2, 'unit' => 'em' ], 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table th' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'header_text_typography', 'selector' => '{{WRAPPER}} .bdt-charitable-donations .charitable-table th', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_body', [ 'label' => __('Body', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'cell_border_style', [ 'label' => __('Border Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'none' => __('None', 'bdthemes-element-pack'), 'solid' => __('Solid', 'bdthemes-element-pack'), 'double' => __('Double', 'bdthemes-element-pack'), 'dotted' => __('Dotted', 'bdthemes-element-pack'), 'dashed' => __('Dashed', 'bdthemes-element-pack'), 'groove' => __('Groove', 'bdthemes-element-pack'), ], 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table td' => 'border-style: {{VALUE}};', ], ] ); $this->add_control( 'cell_border_width', [ 'label' => __('Border Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 20, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table td' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'cell_padding', [ 'label' => __('Cell Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'default' => [ 'top' => 0.5, 'bottom' => 0.5, 'left' => 1, 'right' => 1, 'unit' => 'em' ], 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table td' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'body_text_typography', 'selector' => '{{WRAPPER}} .bdt-charitable-donations .charitable-table td', ] ); $this->start_controls_tabs('tabs_body_style'); $this->start_controls_tab( 'tab_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'normal_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#fff', 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table td' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'normal_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table td' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'normal_border_color', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#ccc', 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table td' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_hover', [ 'label' => __('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'row_hover_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table tr:hover td' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'row_hover_text_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table tr:hover td' => 'color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_stripe', [ 'label' => __('Stripe', 'bdthemes-element-pack'), ] ); $this->add_control( 'stripe_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#f5f5f5', 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table tr:nth-child(even) td' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'stripe_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table tr:nth-child(even) td' => 'color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_link_text', [ 'label' => __('Link', 'bdthemes-element-pack'), ] ); $this->add_control( 'link_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table td a' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'link_hover_color', [ 'label' => __('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-charitable-donations .charitable-table td a:hover' => 'color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } private function get_shortcode() { $settings = $this->get_settings_for_display(); $attributes = []; $this->add_render_attribute( 'shortcode', $attributes ); $shortcode = []; $shortcode[] = sprintf( '[charitable_my_donations %s]', $this->get_render_attribute_string( 'shortcode' ) ); return implode("", $shortcode); } public function render() { $this->add_render_attribute( 'charitable_wrapper', 'class', 'bdt-charitable-donations' ); ?> <div <?php $this->print_render_attribute_string('charitable_wrapper'); ?>> <?php echo do_shortcode( $this->get_shortcode() ); ?> </div> <?php } public function render_plain_content() { echo wp_kses_post($this->get_shortcode()); } }<?php namespace ElementPack\Modules\CharitableDonations; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'charitable-donations'; } public function get_widgets() { // $charitable_donations = element_pack_option('charitable-donations', 'element_pack_third_party_widget', 'off' ); $widgets = ['Charitable_Donations']; // if ( 'on' === $charitable_donations ) { // $widgets[] = 'Charitable_Donations'; // } return $widgets; } }<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Charitable Donations', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_style' => true, ]; <?php namespace ElementPack\Modules\UserLogin\Widgets; use Elementor\Repeater; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Typography; use Elementor\Icons_Manager; use ElementPack\Modules\UserLogin\Skins; use ElementPack\Element_Pack_Loader; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class User_Login extends Module_Base { public function get_name() { return 'bdt-user-login'; } public function get_title() { return BDTEP . esc_html__( 'User Login', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-user-login'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'user', 'login', 'form' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-styles' ]; } else { return [ 'ep-font', 'ep-user-login' ]; } } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'recaptcha', 'ep-google-login', 'ep-scripts' ]; } else { return [ 'recaptcha', 'ep-google-login', 'ep-user-login' ]; } } public function get_custom_help_url() { return 'https://youtu.be/JLdKfv_-R6c'; } protected function register_skins() { $this->add_skin( new Skins\Skin_Dropdown( $this ) ); $this->add_skin( new Skins\Skin_Modal( $this ) ); } protected function register_controls() { $this->start_controls_section( 'section_forms_layout', [ 'label' => esc_html__( 'Forms Layout', 'bdthemes-element-pack' ), ] ); $this->add_control( 'labels_title', [ 'label' => esc_html__( 'Labels', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'separator' => 'before', ] ); $this->add_control( 'show_labels', [ 'label' => esc_html__( 'Label', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'fields_title', [ 'label' => esc_html__( 'Fields', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, ] ); $this->add_control( 'input_size', [ 'label' => esc_html__( 'Input Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'options' => [ 'small' => esc_html__( 'Small', 'bdthemes-element-pack' ), 'default' => esc_html__( 'Default', 'bdthemes-element-pack' ), 'large' => esc_html__( 'Large', 'bdthemes-element-pack' ), ], 'default' => 'default', ] ); $this->add_control( 'button_title', [ 'label' => esc_html__( 'Submit Button', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'separator' => 'before', ] ); $this->add_control( 'button_text', [ 'label' => esc_html__( 'Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Log In', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'button_size', [ 'label' => esc_html__( 'Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'options' => [ 'small' => esc_html__( 'Small', 'bdthemes-element-pack' ), '' => esc_html__( 'Default', 'bdthemes-element-pack' ), 'large' => esc_html__( 'Large', 'bdthemes-element-pack' ), ], 'default' => '', ] ); $this->add_responsive_control( 'align', [ 'label' => esc_html__( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'start' => [ 'title' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'end' => [ 'title' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'stretch' => [ 'title' => esc_html__( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'prefix_class' => 'elementor%s-button-align-', 'default' => '', 'condition' => [ '_skin!' => [ 'bdt-modal' ], ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_custom_nav', [ 'label' => esc_html__( 'Dropdown Content', 'bdthemes-element-pack' ), 'condition' => [ '_skin' => [ 'bdt-dropdown', 'bdt-modal' ], ], ] ); $this->start_controls_tabs( 'tabs_logged' ); $this->start_controls_tab( 'tab_button_logged', [ 'label' => esc_html__( 'Logged In', 'bdthemes-element-pack' ), ] ); $this->add_control( 'header_heading', [ 'label' => __( 'Header', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, ] ); $this->add_control( 'show_domain_link', [ 'label' => esc_html__( 'Show Domain Link', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'hr_7', [ 'type' => Controls_Manager::DIVIDER, ] ); $this->add_control( 'content_heading', [ 'label' => __( 'Content', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, ] ); $repeater = new Repeater(); $repeater->add_control( 'custom_nav_title', [ 'name' => '', 'label' => esc_html__( 'Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Title', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], ] ); $repeater->add_control( 'custom_nav_icon', [ 'label' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::ICONS, 'fa4compatibility' => 'icon', ] ); $repeater->add_control( 'custom_nav_link', [ 'label' => esc_html__( 'Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::URL, 'default' => [ 'url' => '#' ], 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'custom_navs', [ 'label' => esc_html__( 'Dropdown Menus', 'bdthemes-element-pack' ), 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(), 'default' => [ [ 'custom_nav_title' => esc_html__( 'Billing', 'bdthemes-element-pack' ), 'custom_nav_icon' => [ 'value' => 'fas fa-dollar-sign', 'library' => 'fa-solid' ], 'custom_nav_link' => [ 'url' => '#', ] ], [ 'custom_nav_title' => esc_html__( 'Settings', 'bdthemes-element-pack' ), 'custom_nav_icon' => [ 'value' => 'fas fa-cog', 'library' => 'fa-solid' ], 'custom_nav_link' => [ 'url' => '#', ] ], [ 'custom_nav_title' => esc_html__( 'Support', 'bdthemes-element-pack' ), 'custom_nav_icon' => [ 'value' => 'far fa-life-ring', 'library' => 'fa-regular' ], 'custom_nav_link' => [ 'url' => '#', ] ], ], 'title_field' => '{{{ custom_nav_title }}}', ] ); $this->add_control( 'hr_8', [ 'type' => Controls_Manager::DIVIDER, ] ); $this->add_control( 'show_edit_profile', [ 'label' => __( 'Edit Profile', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_button_logged_out', [ 'label' => esc_html__( 'Logged Out', 'bdthemes-element-pack' ), ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_forms_additional_options', [ 'label' => esc_html__( 'Additional Options', 'bdthemes-element-pack' ), ] ); $this->start_controls_tabs( 'tabs_additional_options' ); $this->start_controls_tab( 'tab_additional_options_logged_out', [ 'label' => esc_html__( 'Logged Out', 'bdthemes-element-pack' ), ] ); $this->add_control( 'redirect_after_logOut', [ 'label' => esc_html__( 'Redirect After Log Out', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'redirect_logOut_url', [ 'type' => Controls_Manager::URL, 'show_label' => false, 'show_external' => false, 'separator' => false, 'placeholder' => 'http://your-link.com/', 'description' => esc_html__( 'Note: Because of security reasons, you can ONLY use your current domain here.', 'bdthemes-element-pack' ), 'condition' => [ 'redirect_after_logOut' => 'yes', ], 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'hr_3', [ 'type' => Controls_Manager::DIVIDER, 'condition' => [ 'redirect_after_login' => 'yes', ], ] ); $this->add_control( 'show_lost_password', [ 'label' => esc_html__( 'Lost Password link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'custom_lost_password', [ 'label' => esc_html__( 'Custom Lost Password URL', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'show_lost_password' => 'yes', ], ] ); $this->add_control( 'custom_lost_password_url', [ 'type' => Controls_Manager::URL, 'show_label' => false, 'show_external' => false, 'separator' => false, 'placeholder' => 'http://your-link.com/', 'condition' => [ 'custom_lost_password' => 'yes', ], ] ); $this->add_control( 'hr_1', [ 'type' => Controls_Manager::DIVIDER, ] ); if ( get_option( 'users_can_register' ) ) { $this->add_control( 'show_register', [ 'label' => esc_html__( 'Register Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'custom_register', [ 'label' => esc_html__( 'Custom Register URL', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'show_register' => 'yes', ], ] ); $this->add_control( 'custom_register_url', [ 'type' => Controls_Manager::URL, 'show_label' => false, 'show_external' => false, 'separator' => false, 'placeholder' => 'http://your-link.com/', 'condition' => [ 'custom_register' => 'yes', ], ] ); } $this->add_control( 'hr_2', [ 'type' => Controls_Manager::DIVIDER, ] ); $this->add_control( 'show_remember_me', [ 'label' => esc_html__( 'Remember Me Checkbox', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'logout_text', [ 'label' => esc_html__( 'Logout Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Logout', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], 'condition' => [ 'show_logged_in_content' => '', '_skin!' => '' ], ] ); $this->add_control( 'hr', [ 'type' => Controls_Manager::DIVIDER, 'condition' => [ 'show_logged_in_content' => 'yes', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_additional_options_logged_in', [ 'label' => esc_html__( 'Logged In', 'bdthemes-element-pack' ), ] ); $this->add_control( 'redirect_after_login', [ 'label' => esc_html__( 'Redirect After Login', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'redirect_url', [ 'type' => Controls_Manager::URL, 'show_label' => false, 'show_external' => false, 'separator' => false, 'placeholder' => 'http://your-link.com/', 'description' => esc_html__( 'Note: Because of security reasons, you can ONLY use your current domain here.', 'bdthemes-element-pack' ), 'condition' => [ 'redirect_after_login' => 'yes', ], 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'show_logged_in_content', [ 'label' => esc_html__( 'Logged in Content', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_logged_in_message', [ 'label' => esc_html__( 'Welcome Message', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'show_logged_in_content' => 'yes', ], ] ); $this->add_control( 'show_user_name', [ 'label' => esc_html__( 'Show User Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'show_logged_in_content' => 'yes', '_skin!' => '', ], ] ); $this->add_control( 'show_avatar_in_button', [ 'label' => esc_html__( 'Avatar in Button', 'bdthemes-element-pack' ), 'description' => esc_html__( 'When user logged in this avatar shown in dropdown/modal button.', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ '_skin!' => '', 'show_avatar_icon_in_button!' => 'yes', ], ] ); $this->add_control( 'show_avatar_icon_in_button', [ 'label' => esc_html__( 'Avatar Icon in Button', 'bdthemes-element-pack' ) . BDTEP_NC, 'description' => esc_html__( 'When user logged in this icon shown in dropdown/modal button.', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ '_skin!' => '', ], ] ); $this->add_control( 'avatar_icon', [ 'label' => esc_html__( 'Choose Avatar Icon', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::ICONS, 'condition' => [ '_skin!' => '', 'show_avatar_icon_in_button' => 'yes', ], 'default' => [ 'value' => 'fas fa-level-down-alt', 'library' => 'fa-solid', ], 'label_block' => false, 'skin' => 'inline' ] ); $this->add_control( 'show_facebook_login', [ 'label' => esc_html__( 'Show Facebook Login', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'separator' => 'before', ] ); $this->add_control( 'show_google_login', [ 'label' => esc_html__( 'Show Google Login', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'show_separator', [ 'label' => esc_html__( 'Show Separator', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'conditions' => [ 'relation' => 'or', 'terms' => [ [ 'name' => 'show_facebook_login', 'value' => 'yes', ], [ 'name' => 'show_google_login', 'value' => 'yes', ], ], ], ] ); $this->add_control( 'show_recaptcha_checker', [ 'label' => esc_html__( 'reCAPTCHA Enable', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'prefix_class' => 'bdt-show-recaptcha-badge-', 'separator' => 'before', ] ); $this->add_control( 'hide_recaptcha_badge', [ 'label' => esc_html__( 'Hide reCAPTCHA Bagde', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'prefix_class' => 'bdt-hide-recaptcha-badge-', 'condition' => [ 'show_recaptcha_checker' => 'yes', ], ] ); $this->add_control( 'custom_labels', [ 'label' => esc_html__( 'Custom Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'separator' => 'before', ] ); $this->add_control( 'user_label', [ 'label' => esc_html__( 'Username Label', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Username or Email', 'bdthemes-element-pack' ), 'condition' => [ 'show_labels' => 'yes', 'custom_labels' => 'yes', ], ] ); $this->add_control( 'user_placeholder', [ 'label' => esc_html__( 'Username Placeholder', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Username or Email', 'bdthemes-element-pack' ), 'condition' => [ 'custom_labels' => 'yes', ], ] ); $this->add_control( 'password_label', [ 'label' => esc_html__( 'Password Label', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Password', 'bdthemes-element-pack' ), 'condition' => [ 'show_labels' => 'yes', 'custom_labels' => 'yes', ], ] ); $this->add_control( 'password_placeholder', [ 'label' => esc_html__( 'Password Placeholder', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Password', 'bdthemes-element-pack' ), 'condition' => [ 'custom_labels' => 'yes', ], ] ); $this->add_control( 'custom_password_text', [ 'label' => esc_html__( 'Lost Password Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Lost Password?', 'bdthemes-element-pack' ), 'condition' => [ 'show_lost_password' => 'yes', 'custom_labels' => 'yes', ], ] ); $this->add_control( 'custom_register_text', [ 'label' => esc_html__( 'Register Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Register', 'bdthemes-element-pack' ), 'condition' => [ 'show_register' => 'yes', 'custom_labels' => 'yes', ], ] ); $this->add_control( 'custom_remember_text', [ 'label' => esc_html__( 'Remember Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Remember Me', 'bdthemes-element-pack' ), 'condition' => [ 'show_remember_me' => 'yes', 'custom_labels' => 'yes', ], ] ); $this->add_control( 'logged_in_custom_message', [ 'label' => esc_html__( 'Welcome Message', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Hey,', 'bdthemes-element-pack' ), 'condition' => [ 'show_logged_in_message' => 'yes', 'custom_labels' => 'yes', 'show_logged_in_content' => 'yes', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_additional_options_dropdown', [ 'label' => esc_html__( 'Dropdown', 'bdthemes-element-pack' ), 'condition' => [ '_skin' => [ 'bdt-dropdown' ], ], ] ); $this->add_responsive_control( 'dropdown_width', [ 'label' => esc_html__( 'Dropdown Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => '400', ], 'range' => [ 'px' => [ 'min' => 200, 'max' => 1200, ], ], 'separator' => 'before', 'condition' => [ '_skin' => [ 'bdt-dropdown' ], ], 'selectors' => [ '{{WRAPPER}} .bdt-user-login-skin-dropdown .bdt-dropdown' => 'width: {{SIZE}}{{UNIT}} !important;', ], ] ); $this->add_control( 'dropdown_offset', [ 'label' => esc_html__( 'Dropdown Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], //'separator' => 'before', 'condition' => [ '_skin' => [ 'bdt-dropdown', 'bdt-modal' ], ], ] ); $this->add_control( 'dropdown_position', [ 'label' => esc_html__( 'Dropdown Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'bottom-right', 'options' => element_pack_drop_position(), 'condition' => [ '_skin' => [ 'bdt-dropdown', 'bdt-modal' ], ], ] ); $this->add_control( 'dropdown_mode', [ 'label' => esc_html__( 'Dropdown Mode', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'hover', 'options' => [ 'hover' => esc_html__( 'Hover', 'bdthemes-element-pack' ), 'click' => esc_html__( 'Clicked', 'bdthemes-element-pack' ), ], 'condition' => [ '_skin' => [ 'bdt-dropdown', 'bdt-modal' ], ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); //style Modal Button $this->start_controls_section( 'section_style_modal_button', [ 'label' => esc_html__( 'Modal Button', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => 'bdt-modal' ] ] ); $this->start_controls_tabs( 'tabs_modal_button_style' ); $this->start_controls_tab( 'tab_modal_button_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'modal_button_text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-button-modal' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'modal_button_typography', 'selector' => '{{WRAPPER}} .bdt-button-modal', ] ); $this->add_control( 'modal_button_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-button-modal' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'modal_button_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-button-modal', ] ); $this->add_control( 'modal_button_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-button-modal' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'modal_button_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-button-modal' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_modal_button_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'modal_button_hover_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-button-modal:hover' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'modal_button_hover_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-button-modal:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'modal_button_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-button-modal:hover' => 'border-color: {{VALUE}};', ], 'condition' => [ 'modal_button_border_border!' => '', ], ] ); $this->add_control( 'modal_button_animation', [ 'label' => esc_html__( 'Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HOVER_ANIMATION, ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->add_control( 'modal_avatar_size', [ 'label' => esc_html__( 'Avatar Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 24, ], 'range' => [ 'px' => [ 'min' => 8, 'max' => 32, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-user-login-button-avatar img' => 'width: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style', [ 'label' => esc_html__( 'Form Style', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'row_gap', [ 'label' => esc_html__( 'Rows Gap', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => '15', ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 60, ], ], 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group:not(:last-child)' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'checkbox_color', [ 'label' => esc_html__( 'Checkbox Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-form-stacked .bdt-field-group .bdt-checkbox' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'checkbox_active_color', [ 'label' => esc_html__( 'Checkbox Active Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-form-stacked .bdt-field-group .bdt-checkbox:checked' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'links_heading', [ 'label' => esc_html__( 'L I N K S', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] ); $this->add_control( 'links_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group > a' => 'color: {{VALUE}};', '#bdt-user-login{{ID}} .bdt-user-login-password a:not(:last-child):after' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'links_hover_color', [ 'label' => esc_html__( 'Hover Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group > a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'links_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ) . BDTEP_NC, 'selector' => '#bdt-user-login{{ID}} .bdt-field-group > a', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_labels', [ 'label' => esc_html__( 'Form Label', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'conditions' => [ 'relation' => 'or', 'terms' => [ [ 'name' => 'show_labels', 'value' => 'yes' ], [ 'name' => 'show_remember_me', 'value' => 'yes' ], ] ] ] ); $this->add_responsive_control( 'label_spacing', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group > label' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'label_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-form-label' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'label_typography', 'selector' => '#bdt-user-login{{ID}} .bdt-form-label', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_field_style', [ 'label' => esc_html__( 'Form Fields', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs( 'tabs_field_style' ); $this->start_controls_tab( 'tab_field_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'field_text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group .bdt-input' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_placeholder_color', [ 'label' => esc_html__( 'Placeholder Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group .bdt-input::placeholder' => 'color: {{VALUE}};', '#bdt-user-login{{ID}} .bdt-field-group .bdt-input::-moz-placeholder' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group .bdt-input' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'field_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '#bdt-user-login{{ID}} .bdt-field-group .bdt-input', 'separator' => 'before', ] ); $this->add_responsive_control( 'field_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group .bdt-input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'field_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group .bdt-input' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; height: auto;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'field_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ), 'selector' => '#bdt-user-login{{ID}} .bdt-field-group .bdt-input', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'field_box_shadow', 'selector' => '#bdt-user-login{{ID}} .bdt-field-group .bdt-input', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_field_hover', [ 'label' => esc_html__( 'Focus', 'bdthemes-element-pack' ), ] ); $this->add_control( 'field_text_color_focus', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group .bdt-input:focus' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_placeholder_color_focus', [ 'label' => esc_html__( 'Placeholder Color', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group .bdt-input:focus::placeholder' => 'color: {{VALUE}};', '#bdt-user-login{{ID}} .bdt-field-group .bdt-input:focus::-moz-placeholder' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_background_color_focus', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group .bdt-input:focus' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'field_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'field_border_border!' => '', ], 'selectors' => [ '#bdt-user-login{{ID}} .bdt-field-group .bdt-input:focus' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_submit_button_style', [ 'label' => esc_html__( 'Form Submit Button', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs( 'tabs_button_style' ); $this->start_controls_tab( 'tab_button_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'button_text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-button' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'button_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-button' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'button_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '#bdt-user-login{{ID}} .bdt-button', 'separator' => 'before', ] ); $this->add_responsive_control( 'button_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '#bdt-user-login{{ID}} .bdt-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_text_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '#bdt-user-login{{ID}} .bdt-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_text_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '#bdt-user-login{{ID}} .bdt-button' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'button_typography', 'selector' => '#bdt-user-login{{ID}} .bdt-button', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_box_shadow', 'label' => esc_html__( 'Box Shadow', 'bdthemes-element-pack' ) . BDTEP_NC, 'selector' => '#bdt-user-login{{ID}} .bdt-button', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_button_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'button_hover_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-button:hover' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'button_background_hover_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-button:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'button_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#bdt-user-login{{ID}} .bdt-button:hover' => 'border-color: {{VALUE}};', ], 'condition' => [ 'button_border_border!' => '', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_hover_box_shadow', 'label' => esc_html__( 'Box Shadow', 'bdthemes-element-pack' ) . BDTEP_NC, 'selector' => '#bdt-user-login{{ID}} .bdt-button:hover', ] ); $this->add_control( 'button_hover_animation', [ 'label' => esc_html__( 'Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HOVER_ANIMATION, ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); //style Dropdown Button $this->start_controls_section( 'section_style_dropdown_button', [ 'label' => esc_html__( 'Dropdown Button', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => 'bdt-dropdown' ] ] ); $this->start_controls_tabs( 'tabs_dropdown_button_style' ); $this->start_controls_tab( 'tab_dropdown_button_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'dropdown_button_text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-button-dropdown' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-button-dropdown svg' => 'fill: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'dropdown_button_typography', 'selector' => '{{WRAPPER}} .bdt-button-dropdown', ] ); $this->add_control( 'hr_10', [ 'type' => Controls_Manager::DIVIDER, ] ); $this->add_control( 'dropdown_button_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-button-dropdown' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'dropdown_button_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-button-dropdown', ] ); $this->add_responsive_control( 'dropdown_button_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-button-dropdown' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'dropdown_button_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-button-dropdown' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'dropdown_avatar_size', [ 'label' => esc_html__( 'Avatar Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 8, 'max' => 32, ], ], 'default' => [ 'size' => 24, ], 'selectors' => [ '{{WRAPPER}} .bdt-user-login-button-avatar img' => 'width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'dropdown_button_icon_typography', 'label' => __( 'Icon Typography', 'bdthemes-element-pack' ) . BDTEP_NC, 'selector' => '{{WRAPPER}} .bdt-button-dropdown-icon', 'condition' => [ 'bdt_dropdown_user_login_dropdown_icon[value]!' => '', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_dropdown_button_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'dropdown_button_hover_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-button-dropdown:hover' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-button-dropdown:hover svg' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'dropdown_button_hover_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-button-dropdown:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'dropdown_button_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-button-dropdown:hover' => 'border-color: {{VALUE}};', ], 'condition' => [ 'dropdown_button_border_border!' => '', ], ] ); $this->add_control( 'dropdown_button_animation', [ 'label' => esc_html__( 'Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HOVER_ANIMATION, ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_dropdown_style', [ 'label' => esc_html__( 'Dropdown Content', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => [ 'bdt-dropdown', 'bdt-modal' ], ], ] ); $this->add_control( 'dropdown_text_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-dropdown' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'dropdown_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-dropdown' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'dropdown_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-user-login .bdt-dropdown', 'separator' => 'before', ] ); $this->add_responsive_control( 'dropdown_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-dropdown' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'dropdown_text_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-dropdown, {{WRAPPER}} .bdt-user-login .bdt-dropdown .bdt-user-card-small' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', '{{WRAPPER}} .bdt-user-login .bdt-dropdown .bdt-user-card-small' => 'margin-top: -{{TOP}}{{UNIT}}; margin-right: -{{RIGHT}}{{UNIT}}; margin-left: -{{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'dropdown_link_color', [ 'label' => esc_html__( 'Link Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-dropdown a' => 'color: {{VALUE}};', ], 'separator' => 'before', ] ); $this->add_control( 'dropdown_link_hover_color', [ 'label' => esc_html__( 'Link Hover Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-dropdown a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'hr_5', [ 'type' => Controls_Manager::DIVIDER, ] ); $this->add_control( 'title_heading', [ 'label' => __( 'Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ) . BDTEP_NC, 'selector' => '{{WRAPPER}} .bdt-user-login .bdt-dropdown-nav li a', ] ); $this->add_control( 'hr_6', [ 'type' => Controls_Manager::DIVIDER, ] ); $this->add_control( 'icon_heading', [ 'label' => __( 'Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'icon_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ) . BDTEP_NC, 'selector' => '{{WRAPPER}} .bdt-user-login .bdt-dropdown-nav .bdt-ul-custom-nav-icon', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_logged_style', [ 'label' => esc_html__( 'Logged In Style', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin!' => [ 'bdt-dropdown', 'bdt-modal' ], ], ] ); $this->add_control( 'looged_text_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'looged_link_color', [ 'label' => esc_html__( 'Link Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login a' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'looged_link_hover_color', [ 'label' => esc_html__( 'Link Hover Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login a:hover' => 'color: {{VALUE}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_logout_button_style', [ 'label' => esc_html__( 'Logout Button', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_logged_in_content' => '', '_skin!' => '' ], ] ); $this->start_controls_tabs( 'tabs_logout_button_style' ); $this->start_controls_tab( 'tab_logout_button_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'logout_button_text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-logout-button' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'logout_button_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-logout-button' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'logout_button_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-user-login .bdt-logout-button', 'separator' => 'before', ] ); $this->add_responsive_control( 'logout_button_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-logout-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'logout_button_text_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-logout-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'logout_button_typography', 'selector' => '{{WRAPPER}} .bdt-user-login .bdt-logout-button', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_logout_button_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'logout_button_hover_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-logout-button:hover' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'logout_button_background_hover_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-logout-button:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'logout_button_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-user-login .bdt-logout-button:hover' => 'border-color: {{VALUE}};', ], 'condition' => [ 'logout_button_border_border!' => '', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } public function form_fields_render_attributes() { $settings = $this->get_settings_for_display(); $id = $this->get_id(); if ( ! empty ( $settings['button_size'] ) ) { $this->add_render_attribute( 'button', 'class', 'bdt-button-' . $settings['button_size'] ); } if ( $settings['button_hover_animation'] ) { $this->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['button_hover_animation'] ); } $this->add_render_attribute( [ 'wrapper' => [ 'class' => [ 'elementor-form-fields-wrapper', ], ], 'field-group' => [ 'class' => [ 'bdt-field-group', 'bdt-width-1-1', ], ], 'submit-group' => [ 'class' => [ 'elementor-field-type-submit', 'bdt-field-group', 'bdt-flex', ], ], 'button' => [ 'class' => [ 'elementor-button', 'bdt-button', 'bdt-button-primary', ], 'name' => 'wp-submit', ], 'user_label' => [ 'for' => 'user' . esc_attr( $id ), 'class' => [ 'bdt-form-label', ] ], 'password_label' => [ 'for' => 'password' . esc_attr( $id ), 'class' => [ 'bdt-form-label', ] ], 'user_input' => [ 'type' => 'text', 'name' => 'user_login', 'id' => 'user' . esc_attr( $id ), 'placeholder' => ( $settings['user_placeholder'] ) ? $settings['user_placeholder'] : 'Username or Email', 'class' => [ 'bdt-input', 'bdt-form-' . $settings['input_size'], ], ], 'password_input' => [ 'type' => 'password', 'name' => 'user_password', 'id' => 'password' . esc_attr( $id ), 'placeholder' => ( $settings['password_placeholder'] ) ? $settings['password_placeholder'] : 'Password', 'class' => [ 'bdt-input', 'bdt-form-' . $settings['input_size'], ], ], ] ); if ( ! $settings['show_labels'] ) { $this->add_render_attribute( 'label', 'class', 'elementor-screen-only' ); } $this->add_render_attribute( 'field-group', 'class', 'elementor-field-required' ) ->add_render_attribute( 'input', 'required', true ) ->add_render_attribute( 'input', 'aria-required', 'true' ); } public function render_loop_custom_nav_list( $list ) { $this->add_render_attribute( 'custom-nav-item', 'title', $list["custom_nav_title"], true ); $this->add_render_attribute( 'custom-nav-item', 'href', $list['custom_nav_link']['url'], true ); if ( $list['custom_nav_link']['is_external'] ) { $this->add_render_attribute( 'custom-nav-item', 'target', '_blank', true ); } if ( $list['custom_nav_link']['nofollow'] ) { $this->add_render_attribute( 'custom-nav-item', 'rel', 'nofollow', true ); } if ( ! isset ( $list['icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $list['icon'] = 'fas fa-user'; } $migrated = isset ( $list['__fa4_migrated']['custom_nav_icon'] ); $is_new = empty ( $list['icon'] ) && Icons_Manager::is_migration_allowed(); ?> <li class="bdt-user-login-custom-item"> <a <?php $this->print_render_attribute_string( 'custom-nav-item' ); ?>> <?php if ( $list['custom_nav_icon']['value'] ) : ?> <span class="bdt-ul-custom-nav-icon"> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $list['custom_nav_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $list['icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </span> <?php endif; ?> <?php echo wp_kses( $list['custom_nav_title'], element_pack_allow_tags( 'title' ) ); ?> </a> </li> <?php } public function user_dropdown_menu() { $settings = $this->get_settings_for_display(); $current_user = wp_get_current_user(); $dropdown_offset = $settings['dropdown_offset']; $current_url = remove_query_arg( 'fake_arg' ); $this->add_render_attribute( [ 'dropdown-settings' => [ 'data-bdt-dropdown' => [ wp_json_encode( array_filter( [ "mode" => $settings["dropdown_mode"], "pos" => $settings["dropdown_position"], "offset" => $dropdown_offset["size"] ] ) ) ] ] ] ); $this->add_render_attribute( 'dropdown-settings', 'class', 'bdt-dropdown bdt-drop bdt-text-left bdt-overflow-hidden' ); ?> <div <?php $this->print_render_attribute_string( 'dropdown-settings' ); ?>> <div class="bdt-user-card-small"> <div class="bdt-grid-small bdt-flex-middle" data-bdt-grid> <div class="bdt-width-auto"> <?php echo get_avatar( $current_user->user_email, 48 ); ?> </div> <div class="bdt-width-expand"> <div class="bdt-card-title"> <?php echo esc_html( $current_user->display_name ); ?> </div> <?php if ( 'yes' == $settings["show_domain_link"] ) : ?> <p class="bdt-text-meta bdt-margin-remove-top"> <a href="<?php echo esc_url( $current_user->user_url ); ?>" target="_blank"> <?php echo esc_url( $current_user->user_url ); ?> </a> </p> <?php endif; ?> </div> </div> </div> <ul class="bdt-nav bdt-dropdown-nav"> <?php if ( $settings['show_edit_profile'] ) : ?> <li> <a href="<?php echo esc_url( get_edit_user_link() ); ?>"> <span class="bdt-ul-custom-nav-icon"> <i class="ep-icon-edit fa-fw"></i> </span> <?php esc_html_e( 'Edit Profile', 'bdthemes-element-pack' ); ?> </a> </li> <?php endif; ?> <?php foreach ( $settings['custom_navs'] as $key => $nav ) : $this->render_loop_custom_nav_list( $nav ); endforeach; ?> <?php $logout_url = $current_url; if ( isset ( $settings['redirect_after_logOut'] ) && ! empty ( $settings['redirect_logOut_url']['url'] ) ) { $logout_url = $settings['redirect_logOut_url']['url']; } ?> <li class="bdt-nav-divider"></li> <li> <a href="<?php echo esc_url( wp_logout_url( $logout_url ) ); ?>" class="bdt-ul-logout-menu"><span class="bdt-ul-custom-nav-icon"><i class="ep-icon-lock fa-fw"></i></span> <?php esc_html_e( 'Logout', 'bdthemes-element-pack' ); ?> </a> </li> </ul> </div> <?php } public function render() { $settings = $this->get_settings_for_display(); $current_url = remove_query_arg( 'fake_arg' ); if ( is_user_logged_in() && ! Element_Pack_Loader::elementor()->editor->is_edit_mode() ) { if ( $settings['show_logged_in_content'] ) { $current_user = wp_get_current_user(); ?> <div class="bdt-user-login bdt-text-center"> <ul class="bdt-list bdt-list-divider"> <li class="bdt-user-avatar bdt-margin-large-bottom"> <?php echo get_avatar( $current_user->user_email, 128 ); ?> </li> <li> <span class="bdt-user-name"> <?php if ( $settings['show_logged_in_message'] ) : ?> <?php if ( $settings['logged_in_custom_message'] and $settings['custom_labels'] ) : ?> <?php echo esc_html( $settings['logged_in_custom_message'] ); ?> <?php else : ?> <?php esc_html_e( 'Hi', 'bdthemes-element-pack' ); ?>, <?php endif; ?> <?php endif; ?> <a href="<?php echo esc_url( get_edit_user_link() ); ?>"> <?php echo esc_html( $current_user->display_name ); ?> </a> </span> </li> <li class="bdt-user-website"> <?php esc_html_e( 'Website:', 'bdthemes-element-pack' ); ?> <a href="<?php echo esc_url( $current_user->user_url ); ?>" target="_blank"> <?php echo esc_url( $current_user->user_url ); ?> </a> </li> <li class="bdt-user-bio"> <?php esc_html_e( 'Description:', 'bdthemes-element-pack' ) . ' '; ?> <?php echo esc_html( $current_user->user_description ); ?> </li> <li class="bdt-user-logged-out"> <?php $logout_url = $current_url; if ( isset ( $settings['redirect_after_logOut'] ) && ! empty ( $settings['redirect_logOut_url']['url'] ) ) { $logout_url = $settings['redirect_logOut_url']['url']; } ?> <a href="<?php echo esc_url( wp_logout_url( $logout_url ) ); ?>" class="bdt-button bdt-button-primary"> <?php esc_html_e( 'Logout', 'bdthemes-element-pack' ); ?> </a> </li> </ul> </div> <?php } return; } $this->form_fields_render_attributes(); ?> <div class="bdt-user-login bdt-user-login-skin-default"> <div class="elementor-form-fields-wrapper"> <?php $this->user_login_form(); ?> <?php $this->social_login(); ?> </div> </div> <?php } public function social_login() { $settings = $this->get_settings_for_display(); $options = get_option( 'element_pack_api_settings' ); $fb_app_id = ( isset ( $options['facebook_app_id'] ) && ! empty ( $options['facebook_app_id'] ) ) ? sanitize_text_field( $options['facebook_app_id'] ) : ''; $fb_secret = ( isset ( $options['facebook_app_secret'] ) && ! empty ( $options['facebook_app_secret'] ) ) ? sanitize_text_field( $options['facebook_app_secret'] ) : ''; $gl_app_id = ( isset ( $options['google_client_id'] ) && ! empty ( $options['google_client_id'] ) ) ? sanitize_text_field( $options['google_client_id'] ) : ''; if ( 'yes' == $settings['show_facebook_login'] or 'yes' == $settings['show_google_login'] ) { if ( 'yes' == $settings['show_separator'] ) { $this->add_render_attribute( 'separator', 'class', 'bdt-separator' ); } } if ( 'yes' == $settings['show_facebook_login'] && 'yes' == $settings['show_google_login'] ) { $this->add_render_attribute( 'facebook-login', 'class', 'bdt-facebook bdt-margin-bottom-30' ); } else { $this->add_render_attribute( 'facebook-login', 'class', 'bdt-facebook' ); } ?> <?php if ( 'yes' == $settings['show_facebook_login'] or 'yes' == $settings['show_google_login'] ) : ?> <div class="bdt-width-1-1 bdt-width-1-2@s bdt-position-relative"> <span <?php $this->print_render_attribute_string( 'separator' ); ?>> <div class="bdt-social-wrapper bdt-flex bdt-flex-middle"> <div class="bdt-social-login"> <?php if ( 'yes' == $settings['show_facebook_login'] ) : ?> <div <?php $this->print_render_attribute_string( 'facebook-login' ); ?>> <a href="javascript:void(0);" data-appid="<?php echo esc_attr( $gl_app_id ) ?>" class="fb_btn_link"><span class="bdt-facebook-icon"><i class="ep-icon-facebook fa-fw"></i></span> <?php echo esc_html( 'Facebook' ); ?> </a> </div> <?php endif; ?> <?php if ( ( ! $fb_secret or ! $fb_app_id ) && 'yes' == $settings['show_facebook_login'] ) : $fb_message = ( $fb_secret && $fb_app_id ) ? '' : 'Facebook App ID or Secret Key'; ?> <div class="bdt-alert-warning" data-bdt-alert> <a class="bdt-alert-close" data-bdt-close></a> <p> <?php echo sprintf( esc_html__( 'Ops! %1s Missing. Please add them from: Element Pack Settings > API Settings > Social Login Access. ', 'bdthemes-element-pack' ), esc_html( $fb_message ) ); ?> </p> </div> <?php endif; ?> <?php if ( 'yes' == $settings['show_google_login'] ) : ?> <div class="bdt-google"> <a href="javascript:void(0);" data-clientid="<?php echo esc_attr( $gl_app_id ) ?>" id="google_btn_link"><span class="bdt-google-icon"><i class="ep-icon-google fa-fw"></i></span> <?php echo esc_html( 'Google' ); ?> </a> </div> <?php endif; ?> <?php if ( ! $gl_app_id && 'yes' == $settings['show_google_login'] ) : $google_app_message = ( $gl_app_id ) ? '' : 'Google App ID'; ?> <div class="bdt-alert-warning" data-bdt-alert> <a class="bdt-alert-close" data-bdt-close></a> <p> <?php echo sprintf( esc_html__( 'Ops! %1s Missing. Please add them from: Element Pack Settings > API Settings > Social Login Access. ', 'bdthemes-element-pack' ), esc_html( $google_app_message ) ); ?> </p> </div> <?php endif; ?> </div> </div> </span> </div> <?php endif; ?> <?php } public function user_login_form() { $settings = $this->get_settings_for_display(); $current_url = remove_query_arg( 'fake_arg' ); if ( $settings['redirect_after_login'] && ! empty ( $settings['redirect_url']['url'] ) ) { $redirect_url = $settings['redirect_url']['url']; } else { $redirect_url = $current_url; } $id = $this->get_id(); if ( 'yes' == $settings['show_facebook_login'] or 'yes' == $settings['show_google_login'] ) { $this->add_render_attribute( 'login-form-wrapper', 'class', 'bdt-user-login-form bdt-form-stacked bdt-width-1-1 bdt-width-1-2@s bdt-padding-50' ); } else { $this->add_render_attribute( 'login-form-wrapper', 'class', 'bdt-user-login-form bdt-form-stacked bdt-width-1-1' ); } ?> <form id="bdt-user-login<?php echo esc_attr( $id ); ?>" <?php $this->print_render_attribute_string( 'login-form-wrapper' ); ?> method="post"> <input type="hidden" name="action" value="element_pack_ajax_login"> <?php if ( $settings['show_recaptcha_checker'] ) { do_action( 'element_pack_google_rechatcha_render', $this, 'onLoadElementPackLoginCaptcha', 'button' ); } ?> <input type="hidden" class="widget_id" name="widget_id" value="<?php echo esc_attr( $id ); ?>" /> <input type="hidden" class="page_id" name="page_id" value="<?php echo esc_attr( get_the_ID() ) ?>" /> <input type="hidden" name="redirect_after_login" value="<?php echo esc_url( $redirect_url ) ?>" class="redirect_after_login" /> <div class="bdt-user-login-status"></div> <div <?php $this->print_render_attribute_string( 'field-group' ); ?>> <?php if ( $settings['show_labels'] ) { ?> <label <?php $this->print_render_attribute_string( 'user_label' ); ?>> <?php if ( 'yes' == $settings['custom_labels'] ) { echo wp_kses_post( $settings['user_label'] ); } else { echo esc_html__( 'Username or Email', 'bdthemes-element-pack' ); } ?> </label> <?php } echo '<div class="bdt-form-controls">'; echo '<input ' . wp_kses_post( $this->get_render_attribute_string( 'user_input' ) ) . ' required>'; echo '</div>'; ?> </div> <div <?php $this->print_render_attribute_string( 'field-group' ); ?>> <?php if ( $settings['show_labels'] ) : ?> <label <?php $this->print_render_attribute_string( 'password_label' ); ?>> <?php if ( 'yes' == $settings['custom_labels'] ) { echo wp_kses_post( $settings['password_label'] ); } else { echo esc_html__( 'Password', 'bdthemes-element-pack' ); } ?> </label> <?php endif; echo '<div class="bdt-form-controls">'; echo '<input ' . wp_kses_post( $this->get_render_attribute_string( 'password_input' ) ) . ' required>'; echo '</div>'; ?> </div> <?php if ( $settings['show_remember_me'] ) : ?> <div class="bdt-field-group bdt-remember-me"> <label for="remember-me-<?php echo esc_attr( $id ); ?>" class="bdt-form-label"> <input type="checkbox" id="remember-me-<?php echo esc_attr( $id ); ?>" class="bdt-checkbox" name="rememberme" value="forever"> <?php if ( $settings['custom_labels'] ) : ?> <?php echo esc_html( $settings['custom_remember_text'] ); ?> <?php else : ?> <?php esc_html_e( 'Remember Me', 'bdthemes-element-pack' ); ?> <?php endif; ?> </label> </div> <?php endif; ?> <div <?php $this->print_render_attribute_string( 'submit-group' ); ?>> <button type="submit" <?php $this->print_render_attribute_string( 'button' ); ?>> <?php if ( ! empty ( $settings['button_text'] ) ) : ?> <span> <?php echo wp_kses( $settings['button_text'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> </button> </div> <?php $show_lost_password = $settings['show_lost_password']; $show_register = get_option( 'users_can_register' ) && $settings['show_register']; if ( $show_lost_password || $show_register ) : ?> <div class="bdt-field-group bdt-width-1-1 bdt-margin-remove-bottom bdt-user-login-password"> <?php if ( $show_lost_password ) : ?> <?php if ( $settings['custom_lost_password'] and $settings['custom_lost_password_url']['url'] ) { $lost_password_url = esc_url( $settings['custom_lost_password_url']['url'] ); } else { $lost_password_url = wp_lostpassword_url( $current_url ); } ?> <a class="bdt-lost-password" href="<?php echo esc_url( $lost_password_url ); ?>"> <?php if ( $settings['custom_labels'] ) : ?> <?php echo esc_html( $settings['custom_password_text'] ); ?> <?php else : ?> <?php esc_html_e( 'Lost password?', 'bdthemes-element-pack' ); ?> <?php endif; ?> </a> <?php endif; ?> <?php if ( $show_register ) : ?> <?php if ( $settings['custom_register'] and $settings['custom_register_url']['url'] ) { $register_url = esc_url( $settings['custom_register_url']['url'] ); } else { $register_url = wp_registration_url(); } ?> <a class="bdt-register" href="<?php echo esc_url( $register_url ); ?>"> <?php if ( $settings['custom_labels'] ) : ?> <?php echo esc_html( $settings['custom_register_text'] ); ?> <?php else : ?> <?php esc_html_e( 'Register', 'bdthemes-element-pack' ); ?> <?php endif; ?> </a> <?php endif; ?> </div> <?php endif; ?> <?php wp_nonce_field( 'ajax-login-nonce', 'bdt-user-login-sc' ); ?> </form> <?php } } <?php namespace ElementPack\Modules\UserLogin; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { protected $fb_app_id; protected $fb_app_secret; protected $go_client_id; public function get_name() { return 'user-login'; } public function get_widgets() { $widgets = [ 'User_Login', ]; return $widgets; } /** * Constructor. */ public function __construct() { parent::__construct(); $options = get_option( 'element_pack_api_settings' ); $this->fb_app_id = ( isset( $options['facebook_app_id'] ) && ! empty( $options['facebook_app_id'] ) ) ? sanitize_text_field( $options['facebook_app_id'] ) : ''; $this->fb_app_secret = ( isset( $options['facebook_app_secret'] ) && ! empty( $options['facebook_app_secret'] ) ) ? sanitize_text_field( $options['facebook_app_secret'] ) : ''; $this->go_client_id = ( isset( $options['google_client_id'] ) && ! empty( $options['google_client_id'] ) ) ? sanitize_text_field( $options['google_client_id'] ) : ''; add_action( 'wp_ajax_element_pack_social_facebook_login', array( $this, 'get_facebook_data' ) ); add_action( 'wp_ajax_nopriv_element_pack_social_facebook_login', array( $this, 'get_facebook_data' ) ); add_action( 'wp_ajax_element_pack_social_google_login', array( $this, 'get_google_data' ) ); add_action( 'wp_ajax_nopriv_element_pack_social_google_login', array( $this, 'get_google_data' ) ); add_action( 'elementor/frontend/before_register_scripts', [ $this, 'register_site_scripts' ] ); add_action( 'wp_head', array( $this, 'init_facebook' ) ); add_action( 'wp_ajax_nopriv_element_pack_ajax_login', [ $this, "element_pack_ajax_login" ] ); } public function element_pack_ajax_login() { // First check the nonce, if it fails the function will break check_ajax_referer( 'ajax-login-nonce', 'bdt-user-login-sc' ); /** Recaptcha*/ $post_id = (int) $_REQUEST['page_id']; $widget_id = (int) $_REQUEST['widget_id']; $result = $this->get_widget_settings( $post_id, $widget_id ); if ( isset( $result['show_recaptcha_checker'] ) && $result['show_recaptcha_checker'] == 'yes' ) { $gRecaptcha = esc_textarea( $_REQUEST['g-recaptcha-response'] ); if ( ! apply_filters( 'element_pack_google_recaptcha_validation', $gRecaptcha ) ) { echo wp_json_encode( [ 'loggedin' => false, 'message' => esc_html__( 'reCAPTCHA is invalid!', 'bdthemes-element-pack' ) ] ); exit; } } // Nonce is checked, get the POST data and sign user on $access_info = []; $access_info['user_login'] = ! empty( $_POST['user_login'] ) ? sanitize_text_field( $_POST['user_login'] ) : ""; /** * Do not sanitize password field */ $access_info['user_password'] = ! empty( $_POST['user_password'] ) ? $_POST['user_password'] : ""; $access_info['remember'] = ! empty( $_POST['rememberme'] ) ? true : false; $user_signon = wp_signon( $access_info, false ); if ( ! is_wp_error( $user_signon ) ) { echo wp_json_encode( [ 'loggedin' => true, 'message' => esc_html__( 'Login successful, Redirecting...', 'bdthemes-element-pack' ) ] ); } else { echo wp_json_encode( [ 'loggedin' => false, 'message' => esc_html__( 'Oops! Wrong username or password!', 'bdthemes-element-pack' ) ] ); } die(); } public function register_site_scripts() { wp_register_script( 'ep-google-login', 'https://apis.google.com/js/api:client.js', [ 'jquery' ], null, true ); } public function init_facebook() { if ( strlen( $this->fb_app_id ) > 10 && ! is_user_logged_in() ) : ?> <script> window.fbAsyncInit = function () { FB.init({ appId: '<?php echo esc_html( $this->fb_app_id ) ?>', autoLogAppEvents: true, xfbml: true, version: 'v5.0' }); }; (function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) { return; } js = d.createElement(s); js.id = id; js.src = "https://connect.facebook.net/en_US/sdk.js"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); </script> <?php endif; } /** * Get Google Form Data via AJAX call. * return void */ public function get_google_data() { $data = array(); $response = array(); $user_data = array(); $result = ''; if ( isset( $_POST['id_token'] ) ) { $id_token = filter_input( INPUT_POST, 'id_token', FILTER_SANITIZE_STRING ); $google_client_id = $this->go_client_id; $googleUserdata = $this->verify_google_data( $id_token, $google_client_id ); $name = isset( $googleUserdata['name'] ) ? sanitize_text_field( $googleUserdata['name'] ) : ''; $email = isset( $googleUserdata['email'] ) ? sanitize_email( $googleUserdata['email'] ) : ''; $should_send_email = apply_filters( 'elementor_pack_send_mail_create_user', 0 ); // Check if email is verified with Google. if ( empty( $googleUserdata ) || ( $googleUserdata['aud'] !== $google_client_id ) || ( isset( $googleUserdata['email'] ) && $googleUserdata['email'] !== $email ) ) { wp_send_json_error( array( 'error' => esc_attr_x( 'Unauthorized access', 'User Login and Register', 'bdthemes-element-pack' ), ) ); } $user_data = get_user_by( 'email', $email ); $response['username'] = $name; if ( ! empty( $user_data ) && false !== $user_data ) { $user_ID = $user_data->ID; $user_email = $user_data->user_email; wp_set_auth_cookie( $user_ID ); wp_set_current_user( $user_ID, $name ); do_action( 'wp_login', $user_data->user_login, $user_data ); $response['success'] = true; } else { $password = wp_generate_password( 12, true, false ); if ( username_exists( $name ) ) { // Generate something unique to append to the username in case of a conflict with another user. $suffix = '-' . zeroise( wp_rand( 0, 9999 ), 4 ); $name .= $suffix; $user_array = array( 'user_login' => strtolower( preg_replace( '/\s+/', '', $name ) ), 'user_pass' => $password, 'user_email' => $email, 'first_name' => $googleUserdata['name'], ); $user_array = apply_filters( 'elementor_pack_user_login_insert_user', $user_array ); $result = wp_insert_user( $user_array ); } else { $user_array = array( 'user_login' => strtolower( $name ), 'user_pass' => $password, 'user_email' => $email, 'first_name' => $googleUserdata['name'], ); $user_array = apply_filters( 'elementor_pack_user_login_insert_user', $user_array ); $result = wp_insert_user( $user_array ); } if ( 1 == $should_send_email ) { $this->send_created_user_email( $result, $should_send_email ); } $user_data = get_user_by( 'email', $email ); if ( $user_data ) { $user_ID = $user_data->ID; $user_email = $user_data->user_email; $user_meta = array( 'provider' => 'google', ); update_user_meta( $user_ID, 'ep_login_form', $user_meta ); if ( wp_check_password( $password, $user_data->user_pass, $user_data->ID ) ) { wp_set_auth_cookie( $user_ID ); wp_set_current_user( $user_ID, $name ); do_action( 'wp_login', $user_data->user_login, $user_data ); $response['success'] = true; } } } echo wp_json_encode( $response, true ); } else { die; } } /** * Get access token info. */ public function verify_google_data( $id_token, $uae_google_client_id ) { require_once BDTEP_MODULES_PATH . 'user-login/vendor/autoload.php'; // Get $id_token via HTTPS POST. $client = new \Google_Client( array( 'client_id' => $uae_google_client_id ) ); //PHPCS:ignore:PHPCompatibility.PHP.ShortArray.Found $verified_data = $client->verifyIdToken( $id_token ); if ( $verified_data ) { return $verified_data; } else { wp_send_json_error( array( 'error' => esc_attr_x( 'Unauthorized access', 'User Login and Register', 'bdthemes-element-pack' ), ) ); } } public function get_facebook_data() { $data = array(); $response = array(); $user_data = array(); $result = ''; if ( isset( $_POST['data'] ) ) { $data = $_POST['data']; $fb_user_id = filter_input( INPUT_POST, 'userID', FILTER_SANITIZE_STRING ); $access_token = filter_input( INPUT_POST, 'security_string', FILTER_SANITIZE_STRING ); $fb_app_id = $this->fb_app_id; $fb_app_secret = $this->fb_app_secret; $fbUserData = $this->get_fb_user_info( $access_token, $fb_app_id, $fb_app_secret ); if ( empty( $fb_app_id ) || empty( $fb_app_secret ) || empty( $fb_user_id ) || empty( $fbUserData ) || ( $fb_user_id !== $fbUserData['data']['user_id'] ) || ( $fb_app_id !== $fbUserData['data']['app_id'] ) || ( ! $fbUserData['data']['is_valid'] ) ) { wp_send_json_error( esc_html_x( 'Invalid Authorized Information', 'User Login and Register', 'bdthemes-element-pack' ) ); } $name = sanitize_user( $data['name'] ); $first_name = sanitize_user( $data['first_name'] ); $last_name = sanitize_user( $data['last_name'] ); $should_send_email = apply_filters( 'elementor_pack_send_mail_create_user', 0 ); $verified_email = $this->get_fb_user_email( $fbUserData['data']['user_id'], $access_token ); if ( isset( $data['email'] ) && is_email( $data['email'] ) ) { if ( $data['email'] === $verified_email['email'] ) { $email = sanitize_email( $verified_email['email'] ); } else { wp_send_json_error( esc_html_x( 'Invalid Authorization', 'User Login and Register', 'bdthemes-element-pack' ) ); } } else { $email = $fbUserData['data']['user_id'] . '@facebook.com'; } $user_data = get_user_by( 'email', $email ); if ( ! empty( $user_data ) && false !== $user_data ) { $user_ID = $user_data->ID; $user_email = $user_data->user_email; wp_set_auth_cookie( $user_ID ); wp_set_current_user( $user_ID, $name ); do_action( 'wp_login', $user_data->user_login, $user_data ); $response['success'] = true; } else { $password = wp_generate_password( 12, true, false ); $facebook_array = array( 'user_login' => $name, 'user_pass' => $password, 'user_email' => $email, 'first_name' => isset( $first_name ) ? $first_name : $name, 'last_name' => $last_name, ); if ( username_exists( $name ) ) { // Generate something unique to append to the username in case of a conflict with another user. $suffix = '-' . zeroise( wp_rand( 0, 9999 ), 4 ); $name .= $suffix; $facebook_array['user_login'] = strtolower( preg_replace( '/\s+/', '', $name ) ); } $facebook_array = apply_filters( 'elementor_pack_user_login_insert_user', $facebook_array ); $result = wp_insert_user( $facebook_array ); if ( 1 == $should_send_email ) { $this->send_created_user_email( $result, $should_send_email ); } $user_data = get_user_by( 'email', $email ); if ( $user_data ) { $user_ID = $user_data->ID; $user_email = $user_data->user_email; $user_meta = array( 'provider' => 'facebook', ); update_user_meta( $user_ID, 'ep_login_form', $user_meta ); if ( wp_check_password( $password, $user_data->user_pass, $user_data->ID ) ) { wp_set_auth_cookie( $user_ID ); wp_set_current_user( $user_ID, $name ); do_action( 'wp_login', $user_data->user_login, $user_data ); $response['success'] = true; } } } echo wp_json_encode( $response, true ); } else { die; } } public function get_fb_user_info( $access_token, $uae_facebook_app_id, $uae_facebook_app_secret ) { $fb_url = 'https://graph.facebook.com/oauth/access_token'; $fb_url = add_query_arg( array( 'client_id' => $uae_facebook_app_id, 'client_secret' => $uae_facebook_app_secret, 'grant_type' => 'client_credentials', ), $fb_url ); $fb_response = wp_remote_get( $fb_url ); if ( is_wp_error( $fb_response ) ) { wp_send_json_error(); } $fb_app_response = json_decode( wp_remote_retrieve_body( $fb_response ), true ); $app_token = $fb_app_response['access_token']; $url = 'https://graph.facebook.com/debug_token'; $url = add_query_arg( array( 'input_token' => $access_token, 'access_token' => $app_token, ), $url ); $response = wp_remote_get( $url ); if ( is_wp_error( $response ) ) { wp_send_json_error(); } return json_decode( wp_remote_retrieve_body( $response ), true ); } /** * Function that retrieves authenticatated Facebook email. */ public function get_fb_user_email( $user_id, $access_token ) { $fb_email_url = 'https://graph.facebook.com/' . $user_id; $fb_email_url = add_query_arg( array( 'fields' => 'email', 'access_token' => $access_token, ), $fb_email_url ); $email_response = wp_remote_get( $fb_email_url ); if ( is_wp_error( $email_response ) ) { wp_send_json_error(); } return json_decode( wp_remote_retrieve_body( $email_response ), true ); } public function send_created_user_email( $result, $notify ) { do_action( 'edit_user_created_user', $result, $notify ); } } { "name": "psr/log", "description": "Common interface for logging libraries", "keywords": ["psr", "psr-3", "log"], "homepage": "https://github.com/php-fig/log", "license": "MIT", "authors": [ { "name": "PHP-FIG", "homepage": "http://www.php-fig.org/" } ], "require": { "php": ">=5.3.0" }, "autoload": { "psr-4": { "Psr\\Log\\": "Psr/Log/" } }, "extra": { "branch-alias": { "dev-master": "1.1.x-dev" } } } <?php namespace Psr\Log; /** * Basic Implementation of LoggerAwareInterface. */ trait LoggerAwareTrait { /** * The logger instance. * * @var LoggerInterface */ protected $logger; /** * Sets a logger. * * @param LoggerInterface $logger */ public function setLogger(LoggerInterface $logger) { $this->logger = $logger; } } <?php namespace Psr\Log; class InvalidArgumentException extends \InvalidArgumentException { } <?php namespace Psr\Log; /** * Describes log levels. */ class LogLevel { const EMERGENCY = 'emergency'; const ALERT = 'alert'; const CRITICAL = 'critical'; const ERROR = 'error'; const WARNING = 'warning'; const NOTICE = 'notice'; const INFO = 'info'; const DEBUG = 'debug'; } <?php namespace Psr\Log\Test; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; use PHPUnit\Framework\TestCase; /** * Provides a base test class for ensuring compliance with the LoggerInterface. * * Implementors can extend the class and implement abstract methods to run this * as part of their test suite. */ abstract class LoggerInterfaceTest extends TestCase { /** * @return LoggerInterface */ abstract public function getLogger(); /** * This must return the log messages in order. * * The simple formatting of the messages is: "<LOG LEVEL> <MESSAGE>". * * Example ->error('Foo') would yield "error Foo". * * @return string[] */ abstract public function getLogs(); public function testImplements() { $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); } /** * @dataProvider provideLevelsAndMessages */ public function testLogsAtAllLevels($level, $message) { $logger = $this->getLogger(); $logger->{$level}($message, array('user' => 'Bob')); $logger->log($level, $message, array('user' => 'Bob')); $expected = array( $level.' message of level '.$level.' with context: Bob', $level.' message of level '.$level.' with context: Bob', ); $this->assertEquals($expected, $this->getLogs()); } public function provideLevelsAndMessages() { return array( LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'), LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'), LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'), LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'), LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'), LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'), LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'), LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'), ); } /** * @expectedException \Psr\Log\InvalidArgumentException */ public function testThrowsOnInvalidLevel() { $logger = $this->getLogger(); $logger->log('invalid level', 'Foo'); } public function testContextReplacement() { $logger = $this->getLogger(); $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar')); $expected = array('info {Message {nothing} Bob Bar a}'); $this->assertEquals($expected, $this->getLogs()); } public function testObjectCastToString() { if (method_exists($this, 'createPartialMock')) { $dummy = $this->createPartialMock('Psr\Log\Test\DummyTest', array('__toString')); } else { $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString')); } $dummy->expects($this->once()) ->method('__toString') ->will($this->returnValue('DUMMY')); $this->getLogger()->warning($dummy); $expected = array('warning DUMMY'); $this->assertEquals($expected, $this->getLogs()); } public function testContextCanContainAnything() { $closed = fopen('php://memory', 'r'); fclose($closed); $context = array( 'bool' => true, 'null' => null, 'string' => 'Foo', 'int' => 0, 'float' => 0.5, 'nested' => array('with object' => new DummyTest), 'object' => new \DateTime, 'resource' => fopen('php://memory', 'r'), 'closed' => $closed, ); $this->getLogger()->warning('Crazy context data', $context); $expected = array('warning Crazy context data'); $this->assertEquals($expected, $this->getLogs()); } public function testContextExceptionKeyCanBeExceptionOrOtherValues() { $logger = $this->getLogger(); $logger->warning('Random message', array('exception' => 'oops')); $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail'))); $expected = array( 'warning Random message', 'critical Uncaught Exception!' ); $this->assertEquals($expected, $this->getLogs()); } } class DummyTest { public function __toString() { return 'DummyTest'; } } <?php namespace Psr\Log\Test; use Psr\Log\AbstractLogger; /** * Used for testing purposes. * * It records all records and gives you access to them for verification. * * @method bool hasEmergency($record) * @method bool hasAlert($record) * @method bool hasCritical($record) * @method bool hasError($record) * @method bool hasWarning($record) * @method bool hasNotice($record) * @method bool hasInfo($record) * @method bool hasDebug($record) * * @method bool hasEmergencyRecords() * @method bool hasAlertRecords() * @method bool hasCriticalRecords() * @method bool hasErrorRecords() * @method bool hasWarningRecords() * @method bool hasNoticeRecords() * @method bool hasInfoRecords() * @method bool hasDebugRecords() * * @method bool hasEmergencyThatContains($message) * @method bool hasAlertThatContains($message) * @method bool hasCriticalThatContains($message) * @method bool hasErrorThatContains($message) * @method bool hasWarningThatContains($message) * @method bool hasNoticeThatContains($message) * @method bool hasInfoThatContains($message) * @method bool hasDebugThatContains($message) * * @method bool hasEmergencyThatMatches($message) * @method bool hasAlertThatMatches($message) * @method bool hasCriticalThatMatches($message) * @method bool hasErrorThatMatches($message) * @method bool hasWarningThatMatches($message) * @method bool hasNoticeThatMatches($message) * @method bool hasInfoThatMatches($message) * @method bool hasDebugThatMatches($message) * * @method bool hasEmergencyThatPasses($message) * @method bool hasAlertThatPasses($message) * @method bool hasCriticalThatPasses($message) * @method bool hasErrorThatPasses($message) * @method bool hasWarningThatPasses($message) * @method bool hasNoticeThatPasses($message) * @method bool hasInfoThatPasses($message) * @method bool hasDebugThatPasses($message) */ class TestLogger extends AbstractLogger { /** * @var array */ public $records = []; public $recordsByLevel = []; /** * @inheritdoc */ public function log($level, $message, array $context = []) { $record = [ 'level' => $level, 'message' => $message, 'context' => $context, ]; $this->recordsByLevel[$record['level']][] = $record; $this->records[] = $record; } public function hasRecords($level) { return isset($this->recordsByLevel[$level]); } public function hasRecord($record, $level) { if (is_string($record)) { $record = ['message' => $record]; } return $this->hasRecordThatPasses(function ($rec) use ($record) { if ($rec['message'] !== $record['message']) { return false; } if (isset($record['context']) && $rec['context'] !== $record['context']) { return false; } return true; }, $level); } public function hasRecordThatContains($message, $level) { return $this->hasRecordThatPasses(function ($rec) use ($message) { return strpos($rec['message'], $message) !== false; }, $level); } public function hasRecordThatMatches($regex, $level) { return $this->hasRecordThatPasses(function ($rec) use ($regex) { return preg_match($regex, $rec['message']) > 0; }, $level); } public function hasRecordThatPasses(callable $predicate, $level) { if (!isset($this->recordsByLevel[$level])) { return false; } foreach ($this->recordsByLevel[$level] as $i => $rec) { if (call_user_func($predicate, $rec, $i)) { return true; } } return false; } public function __call($method, $args) { if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) { $genericMethod = $matches[1] . ('Records' !== $matches[3] ? 'Record' : '') . $matches[3]; $level = strtolower($matches[2]); if (method_exists($this, $genericMethod)) { $args[] = $level; return call_user_func_array([$this, $genericMethod], $args); } } throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()'); } public function reset() { $this->records = []; $this->recordsByLevel = []; } } <?php namespace Psr\Log; /** * Describes a logger instance. * * The message MUST be a string or object implementing __toString(). * * The message MAY contain placeholders in the form: {foo} where foo * will be replaced by the context data in key "foo". * * The context array can contain arbitrary data. The only assumption that * can be made by implementors is that if an Exception instance is given * to produce a stack trace, it MUST be in a key named "exception". * * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md * for the full interface specification. */ interface LoggerInterface { /** * System is unusable. * * @param string $message * @param array $context * * @return void */ public function emergency($message, array $context = array()); /** * Action must be taken immediately. * * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * * @param string $message * @param array $context * * @return void */ public function alert($message, array $context = array()); /** * Critical conditions. * * Example: Application component unavailable, unexpected exception. * * @param string $message * @param array $context * * @return void */ public function critical($message, array $context = array()); /** * Runtime errors that do not require immediate action but should typically * be logged and monitored. * * @param string $message * @param array $context * * @return void */ public function error($message, array $context = array()); /** * Exceptional occurrences that are not errors. * * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * * @param string $message * @param array $context * * @return void */ public function warning($message, array $context = array()); /** * Normal but significant events. * * @param string $message * @param array $context * * @return void */ public function notice($message, array $context = array()); /** * Interesting events. * * Example: User logs in, SQL logs. * * @param string $message * @param array $context * * @return void */ public function info($message, array $context = array()); /** * Detailed debug information. * * @param string $message * @param array $context * * @return void */ public function debug($message, array $context = array()); /** * Logs with an arbitrary level. * * @param mixed $level * @param string $message * @param array $context * * @return void * * @throws \Psr\Log\InvalidArgumentException */ public function log($level, $message, array $context = array()); } <?php namespace Psr\Log; /** * This is a simple Logger implementation that other Loggers can inherit from. * * It simply delegates all log-level-specific methods to the `log` method to * reduce boilerplate code that a simple Logger that does the same thing with * messages regardless of the error level has to implement. */ abstract class AbstractLogger implements LoggerInterface { /** * System is unusable. * * @param string $message * @param array $context * * @return void */ public function emergency($message, array $context = array()) { $this->log(LogLevel::EMERGENCY, $message, $context); } /** * Action must be taken immediately. * * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * * @param string $message * @param array $context * * @return void */ public function alert($message, array $context = array()) { $this->log(LogLevel::ALERT, $message, $context); } /** * Critical conditions. * * Example: Application component unavailable, unexpected exception. * * @param string $message * @param array $context * * @return void */ public function critical($message, array $context = array()) { $this->log(LogLevel::CRITICAL, $message, $context); } /** * Runtime errors that do not require immediate action but should typically * be logged and monitored. * * @param string $message * @param array $context * * @return void */ public function error($message, array $context = array()) { $this->log(LogLevel::ERROR, $message, $context); } /** * Exceptional occurrences that are not errors. * * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * * @param string $message * @param array $context * * @return void */ public function warning($message, array $context = array()) { $this->log(LogLevel::WARNING, $message, $context); } /** * Normal but significant events. * * @param string $message * @param array $context * * @return void */ public function notice($message, array $context = array()) { $this->log(LogLevel::NOTICE, $message, $context); } /** * Interesting events. * * Example: User logs in, SQL logs. * * @param string $message * @param array $context * * @return void */ public function info($message, array $context = array()) { $this->log(LogLevel::INFO, $message, $context); } /** * Detailed debug information. * * @param string $message * @param array $context * * @return void */ public function debug($message, array $context = array()) { $this->log(LogLevel::DEBUG, $message, $context); } } <?php namespace Psr\Log; /** * Describes a logger-aware instance. */ interface LoggerAwareInterface { /** * Sets a logger instance on the object. * * @param LoggerInterface $logger * * @return void */ public function setLogger(LoggerInterface $logger); } <?php namespace Psr\Log; /** * This Logger can be used to avoid conditional log calls. * * Logging should always be optional, and if no logger is provided to your * library creating a NullLogger instance to have something to throw logs at * is a good way to avoid littering your code with `if ($this->logger) { }` * blocks. */ class NullLogger extends AbstractLogger { /** * Logs with an arbitrary level. * * @param mixed $level * @param string $message * @param array $context * * @return void * * @throws \Psr\Log\InvalidArgumentException */ public function log($level, $message, array $context = array()) { // noop } } <?php namespace Psr\Log; /** * This is a simple Logger trait that classes unable to extend AbstractLogger * (because they extend another class, etc) can include. * * It simply delegates all log-level-specific methods to the `log` method to * reduce boilerplate code that a simple Logger that does the same thing with * messages regardless of the error level has to implement. */ trait LoggerTrait { /** * System is unusable. * * @param string $message * @param array $context * * @return void */ public function emergency($message, array $context = array()) { $this->log(LogLevel::EMERGENCY, $message, $context); } /** * Action must be taken immediately. * * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * * @param string $message * @param array $context * * @return void */ public function alert($message, array $context = array()) { $this->log(LogLevel::ALERT, $message, $context); } /** * Critical conditions. * * Example: Application component unavailable, unexpected exception. * * @param string $message * @param array $context * * @return void */ public function critical($message, array $context = array()) { $this->log(LogLevel::CRITICAL, $message, $context); } /** * Runtime errors that do not require immediate action but should typically * be logged and monitored. * * @param string $message * @param array $context * * @return void */ public function error($message, array $context = array()) { $this->log(LogLevel::ERROR, $message, $context); } /** * Exceptional occurrences that are not errors. * * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * * @param string $message * @param array $context * * @return void */ public function warning($message, array $context = array()) { $this->log(LogLevel::WARNING, $message, $context); } /** * Normal but significant events. * * @param string $message * @param array $context * * @return void */ public function notice($message, array $context = array()) { $this->log(LogLevel::NOTICE, $message, $context); } /** * Interesting events. * * Example: User logs in, SQL logs. * * @param string $message * @param array $context * * @return void */ public function info($message, array $context = array()) { $this->log(LogLevel::INFO, $message, $context); } /** * Detailed debug information. * * @param string $message * @param array $context * * @return void */ public function debug($message, array $context = array()) { $this->log(LogLevel::DEBUG, $message, $context); } /** * Logs with an arbitrary level. * * @param mixed $level * @param string $message * @param array $context * * @return void * * @throws \Psr\Log\InvalidArgumentException */ abstract public function log($level, $message, array $context = array()); } PSR Log ======= This repository holds all interfaces/classes/traits related to [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md). Note that this is not a logger of its own. It is merely an interface that describes a logger. See the specification for more details. Installation ------------ ```bash composer require psr/log ``` Usage ----- If you need a logger, you can use the interface like this: ```php <?php use Psr\Log\LoggerInterface; class Foo { private $logger; public function __construct(LoggerInterface $logger = null) { $this->logger = $logger; } public function doSomething() { if ($this->logger) { $this->logger->info('Doing work'); } try { $this->doSomethingElse(); } catch (Exception $exception) { $this->logger->error('Oh no!', array('exception' => $exception)); } // do something useful } } ``` You can then pick one of the implementations of the interface to get a logger. If you want to implement the interface, you can require this package and implement `Psr\Log\LoggerInterface` in your code. Please read the [specification text](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) for details. <?php namespace Psr\Cache; /** * Exception interface for invalid cache arguments. * * Any time an invalid argument is passed into a method it must throw an * exception class which implements Psr\Cache\InvalidArgumentException. */ interface InvalidArgumentException extends CacheException { } <?php namespace Psr\Cache; /** * Exception interface for all exceptions thrown by an Implementing Library. */ interface CacheException { } <?php namespace Psr\Cache; /** * CacheItemInterface defines an interface for interacting with objects inside a cache. * * Each Item object MUST be associated with a specific key, which can be set * according to the implementing system and is typically passed by the * Cache\CacheItemPoolInterface object. * * The Cache\CacheItemInterface object encapsulates the storage and retrieval of * cache items. Each Cache\CacheItemInterface is generated by a * Cache\CacheItemPoolInterface object, which is responsible for any required * setup as well as associating the object with a unique Key. * Cache\CacheItemInterface objects MUST be able to store and retrieve any type * of PHP value defined in the Data section of the specification. * * Calling Libraries MUST NOT instantiate Item objects themselves. They may only * be requested from a Pool object via the getItem() method. Calling Libraries * SHOULD NOT assume that an Item created by one Implementing Library is * compatible with a Pool from another Implementing Library. */ interface CacheItemInterface { /** * Returns the key for the current cache item. * * The key is loaded by the Implementing Library, but should be available to * the higher level callers when needed. * * @return string * The key string for this cache item. */ public function getKey(); /** * Retrieves the value of the item from the cache associated with this object's key. * * The value returned must be identical to the value originally stored by set(). * * If isHit() returns false, this method MUST return null. Note that null * is a legitimate cached value, so the isHit() method SHOULD be used to * differentiate between "null value was found" and "no value was found." * * @return mixed * The value corresponding to this cache item's key, or null if not found. */ public function get(); /** * Confirms if the cache item lookup resulted in a cache hit. * * Note: This method MUST NOT have a race condition between calling isHit() * and calling get(). * * @return bool * True if the request resulted in a cache hit. False otherwise. */ public function isHit(); /** * Sets the value represented by this cache item. * * The $value argument may be any item that can be serialized by PHP, * although the method of serialization is left up to the Implementing * Library. * * @param mixed $value * The serializable value to be stored. * * @return static * The invoked object. */ public function set($value); /** * Sets the expiration time for this cache item. * * @param \DateTimeInterface|null $expiration * The point in time after which the item MUST be considered expired. * If null is passed explicitly, a default value MAY be used. If none is set, * the value should be stored permanently or for as long as the * implementation allows. * * @return static * The called object. */ public function expiresAt($expiration); /** * Sets the expiration time for this cache item. * * @param int|\DateInterval|null $time * The period of time from the present after which the item MUST be considered * expired. An integer parameter is understood to be the time in seconds until * expiration. If null is passed explicitly, a default value MAY be used. * If none is set, the value should be stored permanently or for as long as the * implementation allows. * * @return static * The called object. */ public function expiresAfter($time); } <?php namespace Psr\Cache; /** * CacheItemPoolInterface generates CacheItemInterface objects. * * The primary purpose of Cache\CacheItemPoolInterface is to accept a key from * the Calling Library and return the associated Cache\CacheItemInterface object. * It is also the primary point of interaction with the entire cache collection. * All configuration and initialization of the Pool is left up to an * Implementing Library. */ interface CacheItemPoolInterface { /** * Returns a Cache Item representing the specified key. * * This method must always return a CacheItemInterface object, even in case of * a cache miss. It MUST NOT return null. * * @param string $key * The key for which to return the corresponding Cache Item. * * @throws InvalidArgumentException * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException * MUST be thrown. * * @return CacheItemInterface * The corresponding Cache Item. */ public function getItem($key); /** * Returns a traversable set of cache items. * * @param string[] $keys * An indexed array of keys of items to retrieve. * * @throws InvalidArgumentException * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException * MUST be thrown. * * @return array|\Traversable * A traversable collection of Cache Items keyed by the cache keys of * each item. A Cache item will be returned for each key, even if that * key is not found. However, if no keys are specified then an empty * traversable MUST be returned instead. */ public function getItems(array $keys = array()); /** * Confirms if the cache contains specified cache item. * * Note: This method MAY avoid retrieving the cached value for performance reasons. * This could result in a race condition with CacheItemInterface::get(). To avoid * such situation use CacheItemInterface::isHit() instead. * * @param string $key * The key for which to check existence. * * @throws InvalidArgumentException * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException * MUST be thrown. * * @return bool * True if item exists in the cache, false otherwise. */ public function hasItem($key); /** * Deletes all items in the pool. * * @return bool * True if the pool was successfully cleared. False if there was an error. */ public function clear(); /** * Removes the item from the pool. * * @param string $key * The key to delete. * * @throws InvalidArgumentException * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException * MUST be thrown. * * @return bool * True if the item was successfully removed. False if there was an error. */ public function deleteItem($key); /** * Removes multiple items from the pool. * * @param string[] $keys * An array of keys that should be removed from the pool. * @throws InvalidArgumentException * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException * MUST be thrown. * * @return bool * True if the items were successfully removed. False if there was an error. */ public function deleteItems(array $keys); /** * Persists a cache item immediately. * * @param CacheItemInterface $item * The cache item to save. * * @return bool * True if the item was successfully persisted. False if there was an error. */ public function save(CacheItemInterface $item); /** * Sets a cache item to be persisted later. * * @param CacheItemInterface $item * The cache item to save. * * @return bool * False if the item could not be queued or if a commit was attempted and failed. True otherwise. */ public function saveDeferred(CacheItemInterface $item); /** * Persists any deferred cache items. * * @return bool * True if all not-yet-saved items were successfully saved or there were none. False otherwise. */ public function commit(); } # Changelog All notable changes to this project will be documented in this file, in reverse chronological order by release. ## 1.0.1 - 2016-08-06 ### Fixed - Make spacing consistent in phpdoc annotations php-fig/cache#9 - chalasr - Fix grammar in phpdoc annotations php-fig/cache#10 - chalasr - Be more specific in docblocks that `getItems()` and `deleteItems()` take an array of strings (`string[]`) compared to just `array` php-fig/cache#8 - GrahamCampbell - For `expiresAt()` and `expiresAfter()` in CacheItemInterface fix docblock to specify null as a valid parameters as well as an implementation of DateTimeInterface php-fig/cache#7 - GrahamCampbell ## 1.0.0 - 2015-12-11 Initial stable release; reflects accepted PSR-6 specification Copyright (c) 2015 PHP Framework Interoperability Group Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. { "name": "psr/cache", "description": "Common interface for caching libraries", "keywords": ["psr", "psr-6", "cache"], "license": "MIT", "authors": [ { "name": "PHP-FIG", "homepage": "http://www.php-fig.org/" } ], "require": { "php": ">=5.3.0" }, "autoload": { "psr-4": { "Psr\\Cache\\": "src/" } }, "extra": { "branch-alias": { "dev-master": "1.0.x-dev" } } } PSR Cache ========= This repository holds all interfaces defined by [PSR-6](http://www.php-fig.org/psr/psr-6/). Note that this is not a Cache implementation of its own. It is merely an interface that describes a Cache implementation. See the specification for more details. <?php namespace Psr\Http\Message; /** * Representation of an incoming, server-side HTTP request. * * Per the HTTP specification, this interface includes properties for * each of the following: * * - Protocol version * - HTTP method * - URI * - Headers * - Message body * * Additionally, it encapsulates all data as it has arrived to the * application from the CGI and/or PHP environment, including: * * - The values represented in $_SERVER. * - Any cookies provided (generally via $_COOKIE) * - Query string arguments (generally via $_GET, or as parsed via parse_str()) * - Upload files, if any (as represented by $_FILES) * - Deserialized body parameters (generally from $_POST) * * $_SERVER values MUST be treated as immutable, as they represent application * state at the time of request; as such, no methods are provided to allow * modification of those values. The other values provide such methods, as they * can be restored from $_SERVER or the request body, and may need treatment * during the application (e.g., body parameters may be deserialized based on * content type). * * Additionally, this interface recognizes the utility of introspecting a * request to derive and match additional parameters (e.g., via URI path * matching, decrypting cookie values, deserializing non-form-encoded body * content, matching authorization headers to users, etc). These parameters * are stored in an "attributes" property. * * Requests are considered immutable; all methods that might change state MUST * be implemented such that they retain the internal state of the current * message and return an instance that contains the changed state. */ interface ServerRequestInterface extends RequestInterface { /** * Retrieve server parameters. * * Retrieves data related to the incoming request environment, * typically derived from PHP's $_SERVER superglobal. The data IS NOT * REQUIRED to originate from $_SERVER. * * @return array */ public function getServerParams(); /** * Retrieve cookies. * * Retrieves cookies sent by the client to the server. * * The data MUST be compatible with the structure of the $_COOKIE * superglobal. * * @return array */ public function getCookieParams(); /** * Return an instance with the specified cookies. * * The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST * be compatible with the structure of $_COOKIE. Typically, this data will * be injected at instantiation. * * This method MUST NOT update the related Cookie header of the request * instance, nor related values in the server params. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * updated cookie values. * * @param array $cookies Array of key/value pairs representing cookies. * @return static */ public function withCookieParams(array $cookies); /** * Retrieve query string arguments. * * Retrieves the deserialized query string arguments, if any. * * Note: the query params might not be in sync with the URI or server * params. If you need to ensure you are only getting the original * values, you may need to parse the query string from `getUri()->getQuery()` * or from the `QUERY_STRING` server param. * * @return array */ public function getQueryParams(); /** * Return an instance with the specified query string arguments. * * These values SHOULD remain immutable over the course of the incoming * request. They MAY be injected during instantiation, such as from PHP's * $_GET superglobal, or MAY be derived from some other value such as the * URI. In cases where the arguments are parsed from the URI, the data * MUST be compatible with what PHP's parse_str() would return for * purposes of how duplicate query parameters are handled, and how nested * sets are handled. * * Setting query string arguments MUST NOT change the URI stored by the * request, nor the values in the server params. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * updated query string arguments. * * @param array $query Array of query string arguments, typically from * $_GET. * @return static */ public function withQueryParams(array $query); /** * Retrieve normalized file upload data. * * This method returns upload metadata in a normalized tree, with each leaf * an instance of Psr\Http\Message\UploadedFileInterface. * * These values MAY be prepared from $_FILES or the message body during * instantiation, or MAY be injected via withUploadedFiles(). * * @return array An array tree of UploadedFileInterface instances; an empty * array MUST be returned if no data is present. */ public function getUploadedFiles(); /** * Create a new instance with the specified uploaded files. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * updated body parameters. * * @param array $uploadedFiles An array tree of UploadedFileInterface instances. * @return static * @throws \InvalidArgumentException if an invalid structure is provided. */ public function withUploadedFiles(array $uploadedFiles); /** * Retrieve any parameters provided in the request body. * * If the request Content-Type is either application/x-www-form-urlencoded * or multipart/form-data, and the request method is POST, this method MUST * return the contents of $_POST. * * Otherwise, this method may return any results of deserializing * the request body content; as parsing returns structured content, the * potential types MUST be arrays or objects only. A null value indicates * the absence of body content. * * @return null|array|object The deserialized body parameters, if any. * These will typically be an array or object. */ public function getParsedBody(); /** * Return an instance with the specified body parameters. * * These MAY be injected during instantiation. * * If the request Content-Type is either application/x-www-form-urlencoded * or multipart/form-data, and the request method is POST, use this method * ONLY to inject the contents of $_POST. * * The data IS NOT REQUIRED to come from $_POST, but MUST be the results of * deserializing the request body content. Deserialization/parsing returns * structured data, and, as such, this method ONLY accepts arrays or objects, * or a null value if nothing was available to parse. * * As an example, if content negotiation determines that the request data * is a JSON payload, this method could be used to create a request * instance with the deserialized parameters. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * updated body parameters. * * @param null|array|object $data The deserialized body data. This will * typically be in an array or object. * @return static * @throws \InvalidArgumentException if an unsupported argument type is * provided. */ public function withParsedBody($data); /** * Retrieve attributes derived from the request. * * The request "attributes" may be used to allow injection of any * parameters derived from the request: e.g., the results of path * match operations; the results of decrypting cookies; the results of * deserializing non-form-encoded message bodies; etc. Attributes * will be application and request specific, and CAN be mutable. * * @return array Attributes derived from the request. */ public function getAttributes(); /** * Retrieve a single derived request attribute. * * Retrieves a single derived request attribute as described in * getAttributes(). If the attribute has not been previously set, returns * the default value as provided. * * This method obviates the need for a hasAttribute() method, as it allows * specifying a default value to return if the attribute is not found. * * @see getAttributes() * @param string $name The attribute name. * @param mixed $default Default value to return if the attribute does not exist. * @return mixed */ public function getAttribute($name, $default = null); /** * Return an instance with the specified derived request attribute. * * This method allows setting a single derived request attribute as * described in getAttributes(). * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * updated attribute. * * @see getAttributes() * @param string $name The attribute name. * @param mixed $value The value of the attribute. * @return static */ public function withAttribute($name, $value); /** * Return an instance that removes the specified derived request attribute. * * This method allows removing a single derived request attribute as * described in getAttributes(). * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that removes * the attribute. * * @see getAttributes() * @param string $name The attribute name. * @return static */ public function withoutAttribute($name); } <?php namespace Psr\Http\Message; /** * HTTP messages consist of requests from a client to a server and responses * from a server to a client. This interface defines the methods common to * each. * * Messages are considered immutable; all methods that might change state MUST * be implemented such that they retain the internal state of the current * message and return an instance that contains the changed state. * * @link http://www.ietf.org/rfc/rfc7230.txt * @link http://www.ietf.org/rfc/rfc7231.txt */ interface MessageInterface { /** * Retrieves the HTTP protocol version as a string. * * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). * * @return string HTTP protocol version. */ public function getProtocolVersion(); /** * Return an instance with the specified HTTP protocol version. * * The version string MUST contain only the HTTP version number (e.g., * "1.1", "1.0"). * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * new protocol version. * * @param string $version HTTP protocol version * @return static */ public function withProtocolVersion($version); /** * Retrieves all message header values. * * The keys represent the header name as it will be sent over the wire, and * each value is an array of strings associated with the header. * * // Represent the headers as a string * foreach ($message->getHeaders() as $name => $values) { * echo $name . ": " . implode(", ", $values); * } * * // Emit headers iteratively: * foreach ($message->getHeaders() as $name => $values) { * foreach ($values as $value) { * header(sprintf('%s: %s', $name, $value), false); * } * } * * While header names are not case-sensitive, getHeaders() will preserve the * exact case in which headers were originally specified. * * @return string[][] Returns an associative array of the message's headers. Each * key MUST be a header name, and each value MUST be an array of strings * for that header. */ public function getHeaders(); /** * Checks if a header exists by the given case-insensitive name. * * @param string $name Case-insensitive header field name. * @return bool Returns true if any header names match the given header * name using a case-insensitive string comparison. Returns false if * no matching header name is found in the message. */ public function hasHeader($name); /** * Retrieves a message header value by the given case-insensitive name. * * This method returns an array of all the header values of the given * case-insensitive header name. * * If the header does not appear in the message, this method MUST return an * empty array. * * @param string $name Case-insensitive header field name. * @return string[] An array of string values as provided for the given * header. If the header does not appear in the message, this method MUST * return an empty array. */ public function getHeader($name); /** * Retrieves a comma-separated string of the values for a single header. * * This method returns all of the header values of the given * case-insensitive header name as a string concatenated together using * a comma. * * NOTE: Not all header values may be appropriately represented using * comma concatenation. For such headers, use getHeader() instead * and supply your own delimiter when concatenating. * * If the header does not appear in the message, this method MUST return * an empty string. * * @param string $name Case-insensitive header field name. * @return string A string of values as provided for the given header * concatenated together using a comma. If the header does not appear in * the message, this method MUST return an empty string. */ public function getHeaderLine($name); /** * Return an instance with the provided value replacing the specified header. * * While header names are case-insensitive, the casing of the header will * be preserved by this function, and returned from getHeaders(). * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * new and/or updated header and value. * * @param string $name Case-insensitive header field name. * @param string|string[] $value Header value(s). * @return static * @throws \InvalidArgumentException for invalid header names or values. */ public function withHeader($name, $value); /** * Return an instance with the specified header appended with the given value. * * Existing values for the specified header will be maintained. The new * value(s) will be appended to the existing list. If the header did not * exist previously, it will be added. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * new header and/or value. * * @param string $name Case-insensitive header field name to add. * @param string|string[] $value Header value(s). * @return static * @throws \InvalidArgumentException for invalid header names or values. */ public function withAddedHeader($name, $value); /** * Return an instance without the specified header. * * Header resolution MUST be done without case-sensitivity. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that removes * the named header. * * @param string $name Case-insensitive header field name to remove. * @return static */ public function withoutHeader($name); /** * Gets the body of the message. * * @return StreamInterface Returns the body as a stream. */ public function getBody(); /** * Return an instance with the specified message body. * * The body MUST be a StreamInterface object. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return a new instance that has the * new body stream. * * @param StreamInterface $body Body. * @return static * @throws \InvalidArgumentException When the body is not valid. */ public function withBody(StreamInterface $body); } <?php namespace Psr\Http\Message; /** * Representation of an outgoing, server-side response. * * Per the HTTP specification, this interface includes properties for * each of the following: * * - Protocol version * - Status code and reason phrase * - Headers * - Message body * * Responses are considered immutable; all methods that might change state MUST * be implemented such that they retain the internal state of the current * message and return an instance that contains the changed state. */ interface ResponseInterface extends MessageInterface { /** * Gets the response status code. * * The status code is a 3-digit integer result code of the server's attempt * to understand and satisfy the request. * * @return int Status code. */ public function getStatusCode(); /** * Return an instance with the specified status code and, optionally, reason phrase. * * If no reason phrase is specified, implementations MAY choose to default * to the RFC 7231 or IANA recommended reason phrase for the response's * status code. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * updated status and reason phrase. * * @link http://tools.ietf.org/html/rfc7231#section-6 * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml * @param int $code The 3-digit integer result code to set. * @param string $reasonPhrase The reason phrase to use with the * provided status code; if none is provided, implementations MAY * use the defaults as suggested in the HTTP specification. * @return static * @throws \InvalidArgumentException For invalid status code arguments. */ public function withStatus($code, $reasonPhrase = ''); /** * Gets the response reason phrase associated with the status code. * * Because a reason phrase is not a required element in a response * status line, the reason phrase value MAY be null. Implementations MAY * choose to return the default RFC 7231 recommended reason phrase (or those * listed in the IANA HTTP Status Code Registry) for the response's * status code. * * @link http://tools.ietf.org/html/rfc7231#section-6 * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml * @return string Reason phrase; must return an empty string if none present. */ public function getReasonPhrase(); } <?php namespace Psr\Http\Message; /** * Representation of an outgoing, client-side request. * * Per the HTTP specification, this interface includes properties for * each of the following: * * - Protocol version * - HTTP method * - URI * - Headers * - Message body * * During construction, implementations MUST attempt to set the Host header from * a provided URI if no Host header is provided. * * Requests are considered immutable; all methods that might change state MUST * be implemented such that they retain the internal state of the current * message and return an instance that contains the changed state. */ interface RequestInterface extends MessageInterface { /** * Retrieves the message's request target. * * Retrieves the message's request-target either as it will appear (for * clients), as it appeared at request (for servers), or as it was * specified for the instance (see withRequestTarget()). * * In most cases, this will be the origin-form of the composed URI, * unless a value was provided to the concrete implementation (see * withRequestTarget() below). * * If no URI is available, and no request-target has been specifically * provided, this method MUST return the string "/". * * @return string */ public function getRequestTarget(); /** * Return an instance with the specific request-target. * * If the request needs a non-origin-form request-target — e.g., for * specifying an absolute-form, authority-form, or asterisk-form — * this method may be used to create an instance with the specified * request-target, verbatim. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * changed request target. * * @link http://tools.ietf.org/html/rfc7230#section-5.3 (for the various * request-target forms allowed in request messages) * @param mixed $requestTarget * @return static */ public function withRequestTarget($requestTarget); /** * Retrieves the HTTP method of the request. * * @return string Returns the request method. */ public function getMethod(); /** * Return an instance with the provided HTTP method. * * While HTTP method names are typically all uppercase characters, HTTP * method names are case-sensitive and thus implementations SHOULD NOT * modify the given string. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * changed request method. * * @param string $method Case-sensitive method. * @return static * @throws \InvalidArgumentException for invalid HTTP methods. */ public function withMethod($method); /** * Retrieves the URI instance. * * This method MUST return a UriInterface instance. * * @link http://tools.ietf.org/html/rfc3986#section-4.3 * @return UriInterface Returns a UriInterface instance * representing the URI of the request. */ public function getUri(); /** * Returns an instance with the provided URI. * * This method MUST update the Host header of the returned request by * default if the URI contains a host component. If the URI does not * contain a host component, any pre-existing Host header MUST be carried * over to the returned request. * * You can opt-in to preserving the original state of the Host header by * setting `$preserveHost` to `true`. When `$preserveHost` is set to * `true`, this method interacts with the Host header in the following ways: * * - If the Host header is missing or empty, and the new URI contains * a host component, this method MUST update the Host header in the returned * request. * - If the Host header is missing or empty, and the new URI does not contain a * host component, this method MUST NOT update the Host header in the returned * request. * - If a Host header is present and non-empty, this method MUST NOT update * the Host header in the returned request. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * new UriInterface instance. * * @link http://tools.ietf.org/html/rfc3986#section-4.3 * @param UriInterface $uri New request URI to use. * @param bool $preserveHost Preserve the original state of the Host header. * @return static */ public function withUri(UriInterface $uri, $preserveHost = false); } <?php namespace Psr\Http\Message; /** * Value object representing a file uploaded through an HTTP request. * * Instances of this interface are considered immutable; all methods that * might change state MUST be implemented such that they retain the internal * state of the current instance and return an instance that contains the * changed state. */ interface UploadedFileInterface { /** * Retrieve a stream representing the uploaded file. * * This method MUST return a StreamInterface instance, representing the * uploaded file. The purpose of this method is to allow utilizing native PHP * stream functionality to manipulate the file upload, such as * stream_copy_to_stream() (though the result will need to be decorated in a * native PHP stream wrapper to work with such functions). * * If the moveTo() method has been called previously, this method MUST raise * an exception. * * @return StreamInterface Stream representation of the uploaded file. * @throws \RuntimeException in cases when no stream is available or can be * created. */ public function getStream(); /** * Move the uploaded file to a new location. * * Use this method as an alternative to move_uploaded_file(). This method is * guaranteed to work in both SAPI and non-SAPI environments. * Implementations must determine which environment they are in, and use the * appropriate method (move_uploaded_file(), rename(), or a stream * operation) to perform the operation. * * $targetPath may be an absolute path, or a relative path. If it is a * relative path, resolution should be the same as used by PHP's rename() * function. * * The original file or stream MUST be removed on completion. * * If this method is called more than once, any subsequent calls MUST raise * an exception. * * When used in an SAPI environment where $_FILES is populated, when writing * files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be * used to ensure permissions and upload status are verified correctly. * * If you wish to move to a stream, use getStream(), as SAPI operations * cannot guarantee writing to stream destinations. * * @see http://php.net/is_uploaded_file * @see http://php.net/move_uploaded_file * @param string $targetPath Path to which to move the uploaded file. * @throws \InvalidArgumentException if the $targetPath specified is invalid. * @throws \RuntimeException on any error during the move operation, or on * the second or subsequent call to the method. */ public function moveTo($targetPath); /** * Retrieve the file size. * * Implementations SHOULD return the value stored in the "size" key of * the file in the $_FILES array if available, as PHP calculates this based * on the actual size transmitted. * * @return int|null The file size in bytes or null if unknown. */ public function getSize(); /** * Retrieve the error associated with the uploaded file. * * The return value MUST be one of PHP's UPLOAD_ERR_XXX constants. * * If the file was uploaded successfully, this method MUST return * UPLOAD_ERR_OK. * * Implementations SHOULD return the value stored in the "error" key of * the file in the $_FILES array. * * @see http://php.net/manual/en/features.file-upload.errors.php * @return int One of PHP's UPLOAD_ERR_XXX constants. */ public function getError(); /** * Retrieve the filename sent by the client. * * Do not trust the value returned by this method. A client could send * a malicious filename with the intention to corrupt or hack your * application. * * Implementations SHOULD return the value stored in the "name" key of * the file in the $_FILES array. * * @return string|null The filename sent by the client or null if none * was provided. */ public function getClientFilename(); /** * Retrieve the media type sent by the client. * * Do not trust the value returned by this method. A client could send * a malicious media type with the intention to corrupt or hack your * application. * * Implementations SHOULD return the value stored in the "type" key of * the file in the $_FILES array. * * @return string|null The media type sent by the client or null if none * was provided. */ public function getClientMediaType(); } <?php namespace Psr\Http\Message; /** * Value object representing a URI. * * This interface is meant to represent URIs according to RFC 3986 and to * provide methods for most common operations. Additional functionality for * working with URIs can be provided on top of the interface or externally. * Its primary use is for HTTP requests, but may also be used in other * contexts. * * Instances of this interface are considered immutable; all methods that * might change state MUST be implemented such that they retain the internal * state of the current instance and return an instance that contains the * changed state. * * Typically the Host header will be also be present in the request message. * For server-side requests, the scheme will typically be discoverable in the * server parameters. * * @link http://tools.ietf.org/html/rfc3986 (the URI specification) */ interface UriInterface { /** * Retrieve the scheme component of the URI. * * If no scheme is present, this method MUST return an empty string. * * The value returned MUST be normalized to lowercase, per RFC 3986 * Section 3.1. * * The trailing ":" character is not part of the scheme and MUST NOT be * added. * * @see https://tools.ietf.org/html/rfc3986#section-3.1 * @return string The URI scheme. */ public function getScheme(); /** * Retrieve the authority component of the URI. * * If no authority information is present, this method MUST return an empty * string. * * The authority syntax of the URI is: * * <pre> * [user-info@]host[:port] * </pre> * * If the port component is not set or is the standard port for the current * scheme, it SHOULD NOT be included. * * @see https://tools.ietf.org/html/rfc3986#section-3.2 * @return string The URI authority, in "[user-info@]host[:port]" format. */ public function getAuthority(); /** * Retrieve the user information component of the URI. * * If no user information is present, this method MUST return an empty * string. * * If a user is present in the URI, this will return that value; * additionally, if the password is also present, it will be appended to the * user value, with a colon (":") separating the values. * * The trailing "@" character is not part of the user information and MUST * NOT be added. * * @return string The URI user information, in "username[:password]" format. */ public function getUserInfo(); /** * Retrieve the host component of the URI. * * If no host is present, this method MUST return an empty string. * * The value returned MUST be normalized to lowercase, per RFC 3986 * Section 3.2.2. * * @see http://tools.ietf.org/html/rfc3986#section-3.2.2 * @return string The URI host. */ public function getHost(); /** * Retrieve the port component of the URI. * * If a port is present, and it is non-standard for the current scheme, * this method MUST return it as an integer. If the port is the standard port * used with the current scheme, this method SHOULD return null. * * If no port is present, and no scheme is present, this method MUST return * a null value. * * If no port is present, but a scheme is present, this method MAY return * the standard port for that scheme, but SHOULD return null. * * @return null|int The URI port. */ public function getPort(); /** * Retrieve the path component of the URI. * * The path can either be empty or absolute (starting with a slash) or * rootless (not starting with a slash). Implementations MUST support all * three syntaxes. * * Normally, the empty path "" and absolute path "/" are considered equal as * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically * do this normalization because in contexts with a trimmed base path, e.g. * the front controller, this difference becomes significant. It's the task * of the user to handle both "" and "/". * * The value returned MUST be percent-encoded, but MUST NOT double-encode * any characters. To determine what characters to encode, please refer to * RFC 3986, Sections 2 and 3.3. * * As an example, if the value should include a slash ("/") not intended as * delimiter between path segments, that value MUST be passed in encoded * form (e.g., "%2F") to the instance. * * @see https://tools.ietf.org/html/rfc3986#section-2 * @see https://tools.ietf.org/html/rfc3986#section-3.3 * @return string The URI path. */ public function getPath(); /** * Retrieve the query string of the URI. * * If no query string is present, this method MUST return an empty string. * * The leading "?" character is not part of the query and MUST NOT be * added. * * The value returned MUST be percent-encoded, but MUST NOT double-encode * any characters. To determine what characters to encode, please refer to * RFC 3986, Sections 2 and 3.4. * * As an example, if a value in a key/value pair of the query string should * include an ampersand ("&") not intended as a delimiter between values, * that value MUST be passed in encoded form (e.g., "%26") to the instance. * * @see https://tools.ietf.org/html/rfc3986#section-2 * @see https://tools.ietf.org/html/rfc3986#section-3.4 * @return string The URI query string. */ public function getQuery(); /** * Retrieve the fragment component of the URI. * * If no fragment is present, this method MUST return an empty string. * * The leading "#" character is not part of the fragment and MUST NOT be * added. * * The value returned MUST be percent-encoded, but MUST NOT double-encode * any characters. To determine what characters to encode, please refer to * RFC 3986, Sections 2 and 3.5. * * @see https://tools.ietf.org/html/rfc3986#section-2 * @see https://tools.ietf.org/html/rfc3986#section-3.5 * @return string The URI fragment. */ public function getFragment(); /** * Return an instance with the specified scheme. * * This method MUST retain the state of the current instance, and return * an instance that contains the specified scheme. * * Implementations MUST support the schemes "http" and "https" case * insensitively, and MAY accommodate other schemes if required. * * An empty scheme is equivalent to removing the scheme. * * @param string $scheme The scheme to use with the new instance. * @return static A new instance with the specified scheme. * @throws \InvalidArgumentException for invalid or unsupported schemes. */ public function withScheme($scheme); /** * Return an instance with the specified user information. * * This method MUST retain the state of the current instance, and return * an instance that contains the specified user information. * * Password is optional, but the user information MUST include the * user; an empty string for the user is equivalent to removing user * information. * * @param string $user The user name to use for authority. * @param null|string $password The password associated with $user. * @return static A new instance with the specified user information. */ public function withUserInfo($user, $password = null); /** * Return an instance with the specified host. * * This method MUST retain the state of the current instance, and return * an instance that contains the specified host. * * An empty host value is equivalent to removing the host. * * @param string $host The hostname to use with the new instance. * @return static A new instance with the specified host. * @throws \InvalidArgumentException for invalid hostnames. */ public function withHost($host); /** * Return an instance with the specified port. * * This method MUST retain the state of the current instance, and return * an instance that contains the specified port. * * Implementations MUST raise an exception for ports outside the * established TCP and UDP port ranges. * * A null value provided for the port is equivalent to removing the port * information. * * @param null|int $port The port to use with the new instance; a null value * removes the port information. * @return static A new instance with the specified port. * @throws \InvalidArgumentException for invalid ports. */ public function withPort($port); /** * Return an instance with the specified path. * * This method MUST retain the state of the current instance, and return * an instance that contains the specified path. * * The path can either be empty or absolute (starting with a slash) or * rootless (not starting with a slash). Implementations MUST support all * three syntaxes. * * If the path is intended to be domain-relative rather than path relative then * it must begin with a slash ("/"). Paths not starting with a slash ("/") * are assumed to be relative to some base path known to the application or * consumer. * * Users can provide both encoded and decoded path characters. * Implementations ensure the correct encoding as outlined in getPath(). * * @param string $path The path to use with the new instance. * @return static A new instance with the specified path. * @throws \InvalidArgumentException for invalid paths. */ public function withPath($path); /** * Return an instance with the specified query string. * * This method MUST retain the state of the current instance, and return * an instance that contains the specified query string. * * Users can provide both encoded and decoded query characters. * Implementations ensure the correct encoding as outlined in getQuery(). * * An empty query string value is equivalent to removing the query string. * * @param string $query The query string to use with the new instance. * @return static A new instance with the specified query string. * @throws \InvalidArgumentException for invalid query strings. */ public function withQuery($query); /** * Return an instance with the specified URI fragment. * * This method MUST retain the state of the current instance, and return * an instance that contains the specified URI fragment. * * Users can provide both encoded and decoded fragment characters. * Implementations ensure the correct encoding as outlined in getFragment(). * * An empty fragment value is equivalent to removing the fragment. * * @param string $fragment The fragment to use with the new instance. * @return static A new instance with the specified fragment. */ public function withFragment($fragment); /** * Return the string representation as a URI reference. * * Depending on which components of the URI are present, the resulting * string is either a full URI or relative reference according to RFC 3986, * Section 4.1. The method concatenates the various components of the URI, * using the appropriate delimiters: * * - If a scheme is present, it MUST be suffixed by ":". * - If an authority is present, it MUST be prefixed by "//". * - The path can be concatenated without delimiters. But there are two * cases where the path has to be adjusted to make the URI reference * valid as PHP does not allow to throw an exception in __toString(): * - If the path is rootless and an authority is present, the path MUST * be prefixed by "/". * - If the path is starting with more than one "/" and no authority is * present, the starting slashes MUST be reduced to one. * - If a query is present, it MUST be prefixed by "?". * - If a fragment is present, it MUST be prefixed by "#". * * @see http://tools.ietf.org/html/rfc3986#section-4.1 * @return string */ public function __toString(); } <?php namespace Psr\Http\Message; /** * Describes a data stream. * * Typically, an instance will wrap a PHP stream; this interface provides * a wrapper around the most common operations, including serialization of * the entire stream to a string. */ interface StreamInterface { /** * Reads all data from the stream into a string, from the beginning to end. * * This method MUST attempt to seek to the beginning of the stream before * reading data and read the stream until the end is reached. * * Warning: This could attempt to load a large amount of data into memory. * * This method MUST NOT raise an exception in order to conform with PHP's * string casting operations. * * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring * @return string */ public function __toString(); /** * Closes the stream and any underlying resources. * * @return void */ public function close(); /** * Separates any underlying resources from the stream. * * After the stream has been detached, the stream is in an unusable state. * * @return resource|null Underlying PHP stream, if any */ public function detach(); /** * Get the size of the stream if known. * * @return int|null Returns the size in bytes if known, or null if unknown. */ public function getSize(); /** * Returns the current position of the file read/write pointer * * @return int Position of the file pointer * @throws \RuntimeException on error. */ public function tell(); /** * Returns true if the stream is at the end of the stream. * * @return bool */ public function eof(); /** * Returns whether or not the stream is seekable. * * @return bool */ public function isSeekable(); /** * Seek to a position in the stream. * * @link http://www.php.net/manual/en/function.fseek.php * @param int $offset Stream offset * @param int $whence Specifies how the cursor position will be calculated * based on the seek offset. Valid values are identical to the built-in * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to * offset bytes SEEK_CUR: Set position to current location plus offset * SEEK_END: Set position to end-of-stream plus offset. * @throws \RuntimeException on failure. */ public function seek($offset, $whence = SEEK_SET); /** * Seek to the beginning of the stream. * * If the stream is not seekable, this method will raise an exception; * otherwise, it will perform a seek(0). * * @see seek() * @link http://www.php.net/manual/en/function.fseek.php * @throws \RuntimeException on failure. */ public function rewind(); /** * Returns whether or not the stream is writable. * * @return bool */ public function isWritable(); /** * Write data to the stream. * * @param string $string The string that is to be written. * @return int Returns the number of bytes written to the stream. * @throws \RuntimeException on failure. */ public function write($string); /** * Returns whether or not the stream is readable. * * @return bool */ public function isReadable(); /** * Read data from the stream. * * @param int $length Read up to $length bytes from the object and return * them. Fewer than $length bytes may be returned if underlying stream * call returns fewer bytes. * @return string Returns the data read from the stream, or an empty string * if no bytes are available. * @throws \RuntimeException if an error occurs. */ public function read($length); /** * Returns the remaining contents in a string * * @return string * @throws \RuntimeException if unable to read or an error occurs while * reading. */ public function getContents(); /** * Get stream metadata as an associative array or retrieve a specific key. * * The keys returned are identical to the keys returned from PHP's * stream_get_meta_data() function. * * @link http://php.net/manual/en/function.stream-get-meta-data.php * @param string $key Specific metadata to retrieve. * @return array|mixed|null Returns an associative array if no key is * provided. Returns a specific key value if a key is provided and the * value is found, or null if the key is not found. */ public function getMetadata($key = null); } # Changelog All notable changes to this project will be documented in this file, in reverse chronological order by release. ## 1.0.1 - 2016-08-06 ### Added - Nothing. ### Deprecated - Nothing. ### Removed - Nothing. ### Fixed - Updated all `@return self` annotation references in interfaces to use `@return static`, which more closelly follows the semantics of the specification. - Updated the `MessageInterface::getHeaders()` return annotation to use the value `string[][]`, indicating the format is a nested array of strings. - Updated the `@link` annotation for `RequestInterface::withRequestTarget()` to point to the correct section of RFC 7230. - Updated the `ServerRequestInterface::withUploadedFiles()` parameter annotation to add the parameter name (`$uploadedFiles`). - Updated a `@throws` annotation for the `UploadedFileInterface::moveTo()` method to correctly reference the method parameter (it was referencing an incorrect parameter name previously). ## 1.0.0 - 2016-05-18 Initial stable release; reflects accepted PSR-7 specification. { "name": "psr/http-message", "description": "Common interface for HTTP messages", "keywords": ["psr", "psr-7", "http", "http-message", "request", "response"], "homepage": "https://github.com/php-fig/http-message", "license": "MIT", "authors": [ { "name": "PHP-FIG", "homepage": "http://www.php-fig.org/" } ], "require": { "php": ">=5.3.0" }, "autoload": { "psr-4": { "Psr\\Http\\Message\\": "src/" } }, "extra": { "branch-alias": { "dev-master": "1.0.x-dev" } } } PSR Http Message ================ This repository holds all interfaces/classes/traits related to [PSR-7](http://www.php-fig.org/psr/psr-7/). Note that this is not a HTTP message implementation of its own. It is merely an interface that describes a HTTP message. See the specification for more details. Usage ----- We'll certainly need some stuff in here.<?php /** * Pure-PHP ASN.1 Parser * * PHP version 5 * * @category File * @package ASN1 * @author Jim Wigginton <terrafrost@php.net> * @copyright 2012 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\File\ASN1; /** * ASN.1 Element * * Bypass normal encoding rules in phpseclib\File\ASN1::encodeDER() * * @package ASN1 * @author Jim Wigginton <terrafrost@php.net> * @access public */ class Element { /** * Raw element value * * @var string * @access private */ var $element; /** * Constructor * * @param string $encoded * @return \phpseclib\File\ASN1\Element * @access public */ function __construct($encoded) { $this->element = $encoded; } } <?php /** * Pure-PHP ANSI Decoder * * PHP version 5 * * If you call read() in \phpseclib\Net\SSH2 you may get {@link http://en.wikipedia.org/wiki/ANSI_escape_code ANSI escape codes} back. * They'd look like chr(0x1B) . '[00m' or whatever (0x1B = ESC). They tell a * {@link http://en.wikipedia.org/wiki/Terminal_emulator terminal emulator} how to format the characters, what * color to display them in, etc. \phpseclib\File\ANSI is a {@link http://en.wikipedia.org/wiki/VT100 VT100} terminal emulator. * * @category File * @package ANSI * @author Jim Wigginton <terrafrost@php.net> * @copyright 2012 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\File; /** * Pure-PHP ANSI Decoder * * @package ANSI * @author Jim Wigginton <terrafrost@php.net> * @access public */ class ANSI { /** * Max Width * * @var int * @access private */ var $max_x; /** * Max Height * * @var int * @access private */ var $max_y; /** * Max History * * @var int * @access private */ var $max_history; /** * History * * @var array * @access private */ var $history; /** * History Attributes * * @var array * @access private */ var $history_attrs; /** * Current Column * * @var int * @access private */ var $x; /** * Current Row * * @var int * @access private */ var $y; /** * Old Column * * @var int * @access private */ var $old_x; /** * Old Row * * @var int * @access private */ var $old_y; /** * An empty attribute cell * * @var object * @access private */ var $base_attr_cell; /** * The current attribute cell * * @var object * @access private */ var $attr_cell; /** * An empty attribute row * * @var array * @access private */ var $attr_row; /** * The current screen text * * @var array * @access private */ var $screen; /** * The current screen attributes * * @var array * @access private */ var $attrs; /** * Current ANSI code * * @var string * @access private */ var $ansi; /** * Tokenization * * @var array * @access private */ var $tokenization; /** * Default Constructor. * * @return \phpseclib\File\ANSI * @access public */ function __construct() { $attr_cell = new \stdClass(); $attr_cell->bold = false; $attr_cell->underline = false; $attr_cell->blink = false; $attr_cell->background = 'black'; $attr_cell->foreground = 'white'; $attr_cell->reverse = false; $this->base_attr_cell = clone $attr_cell; $this->attr_cell = clone $attr_cell; $this->setHistory(200); $this->setDimensions(80, 24); } /** * Set terminal width and height * * Resets the screen as well * * @param int $x * @param int $y * @access public */ function setDimensions($x, $y) { $this->max_x = $x - 1; $this->max_y = $y - 1; $this->x = $this->y = 0; $this->history = $this->history_attrs = array(); $this->attr_row = array_fill(0, $this->max_x + 2, $this->base_attr_cell); $this->screen = array_fill(0, $this->max_y + 1, ''); $this->attrs = array_fill(0, $this->max_y + 1, $this->attr_row); $this->ansi = ''; } /** * Set the number of lines that should be logged past the terminal height * * @param int $x * @param int $y * @access public */ function setHistory($history) { $this->max_history = $history; } /** * Load a string * * @param string $source * @access public */ function loadString($source) { $this->setDimensions($this->max_x + 1, $this->max_y + 1); $this->appendString($source); } /** * Appdend a string * * @param string $source * @access public */ function appendString($source) { $this->tokenization = array(''); for ($i = 0; $i < strlen($source); $i++) { if (strlen($this->ansi)) { $this->ansi.= $source[$i]; $chr = ord($source[$i]); // http://en.wikipedia.org/wiki/ANSI_escape_code#Sequence_elements // single character CSI's not currently supported switch (true) { case $this->ansi == "\x1B=": $this->ansi = ''; continue 2; case strlen($this->ansi) == 2 && $chr >= 64 && $chr <= 95 && $chr != ord('['): case strlen($this->ansi) > 2 && $chr >= 64 && $chr <= 126: break; default: continue 2; } $this->tokenization[] = $this->ansi; $this->tokenization[] = ''; // http://ascii-table.com/ansi-escape-sequences-vt-100.php switch ($this->ansi) { case "\x1B[H": // Move cursor to upper left corner $this->old_x = $this->x; $this->old_y = $this->y; $this->x = $this->y = 0; break; case "\x1B[J": // Clear screen from cursor down $this->history = array_merge($this->history, array_slice(array_splice($this->screen, $this->y + 1), 0, $this->old_y)); $this->screen = array_merge($this->screen, array_fill($this->y, $this->max_y, '')); $this->history_attrs = array_merge($this->history_attrs, array_slice(array_splice($this->attrs, $this->y + 1), 0, $this->old_y)); $this->attrs = array_merge($this->attrs, array_fill($this->y, $this->max_y, $this->attr_row)); if (count($this->history) == $this->max_history) { array_shift($this->history); array_shift($this->history_attrs); } case "\x1B[K": // Clear screen from cursor right $this->screen[$this->y] = substr($this->screen[$this->y], 0, $this->x); array_splice($this->attrs[$this->y], $this->x + 1, $this->max_x - $this->x, array_fill($this->x, $this->max_x - $this->x - 1, $this->base_attr_cell)); break; case "\x1B[2K": // Clear entire line $this->screen[$this->y] = str_repeat(' ', $this->x); $this->attrs[$this->y] = $this->attr_row; break; case "\x1B[?1h": // set cursor key to application case "\x1B[?25h": // show the cursor case "\x1B(B": // set united states g0 character set break; case "\x1BE": // Move to next line $this->_newLine(); $this->x = 0; break; default: switch (true) { case preg_match('#\x1B\[(\d+)B#', $this->ansi, $match): // Move cursor down n lines $this->old_y = $this->y; $this->y+= $match[1]; break; case preg_match('#\x1B\[(\d+);(\d+)H#', $this->ansi, $match): // Move cursor to screen location v,h $this->old_x = $this->x; $this->old_y = $this->y; $this->x = $match[2] - 1; $this->y = $match[1] - 1; break; case preg_match('#\x1B\[(\d+)C#', $this->ansi, $match): // Move cursor right n lines $this->old_x = $this->x; $this->x+= $match[1]; break; case preg_match('#\x1B\[(\d+)D#', $this->ansi, $match): // Move cursor left n lines $this->old_x = $this->x; $this->x-= $match[1]; if ($this->x < 0) { $this->x = 0; } break; case preg_match('#\x1B\[(\d+);(\d+)r#', $this->ansi, $match): // Set top and bottom lines of a window break; case preg_match('#\x1B\[(\d*(?:;\d*)*)m#', $this->ansi, $match): // character attributes $attr_cell = &$this->attr_cell; $mods = explode(';', $match[1]); foreach ($mods as $mod) { switch ($mod) { case 0: // Turn off character attributes $attr_cell = clone $this->base_attr_cell; break; case 1: // Turn bold mode on $attr_cell->bold = true; break; case 4: // Turn underline mode on $attr_cell->underline = true; break; case 5: // Turn blinking mode on $attr_cell->blink = true; break; case 7: // Turn reverse video on $attr_cell->reverse = !$attr_cell->reverse; $temp = $attr_cell->background; $attr_cell->background = $attr_cell->foreground; $attr_cell->foreground = $temp; break; default: // set colors //$front = $attr_cell->reverse ? &$attr_cell->background : &$attr_cell->foreground; $front = &$attr_cell->{ $attr_cell->reverse ? 'background' : 'foreground' }; //$back = $attr_cell->reverse ? &$attr_cell->foreground : &$attr_cell->background; $back = &$attr_cell->{ $attr_cell->reverse ? 'foreground' : 'background' }; switch ($mod) { // @codingStandardsIgnoreStart case 30: $front = 'black'; break; case 31: $front = 'red'; break; case 32: $front = 'green'; break; case 33: $front = 'yellow'; break; case 34: $front = 'blue'; break; case 35: $front = 'magenta'; break; case 36: $front = 'cyan'; break; case 37: $front = 'white'; break; case 40: $back = 'black'; break; case 41: $back = 'red'; break; case 42: $back = 'green'; break; case 43: $back = 'yellow'; break; case 44: $back = 'blue'; break; case 45: $back = 'magenta'; break; case 46: $back = 'cyan'; break; case 47: $back = 'white'; break; // @codingStandardsIgnoreEnd default: //user_error('Unsupported attribute: ' . $mod); $this->ansi = ''; break 2; } } } break; default: //user_error("{$this->ansi} is unsupported\r\n"); } } $this->ansi = ''; continue; } $this->tokenization[count($this->tokenization) - 1].= $source[$i]; switch ($source[$i]) { case "\r": $this->x = 0; break; case "\n": $this->_newLine(); break; case "\x08": // backspace if ($this->x) { $this->x--; $this->attrs[$this->y][$this->x] = clone $this->base_attr_cell; $this->screen[$this->y] = substr_replace( $this->screen[$this->y], $source[$i], $this->x, 1 ); } break; case "\x0F": // shift break; case "\x1B": // start ANSI escape code $this->tokenization[count($this->tokenization) - 1] = substr($this->tokenization[count($this->tokenization) - 1], 0, -1); //if (!strlen($this->tokenization[count($this->tokenization) - 1])) { // array_pop($this->tokenization); //} $this->ansi.= "\x1B"; break; default: $this->attrs[$this->y][$this->x] = clone $this->attr_cell; if ($this->x > strlen($this->screen[$this->y])) { $this->screen[$this->y] = str_repeat(' ', $this->x); } $this->screen[$this->y] = substr_replace( $this->screen[$this->y], $source[$i], $this->x, 1 ); if ($this->x > $this->max_x) { $this->x = 0; $this->_newLine(); } else { $this->x++; } } } } /** * Add a new line * * Also update the $this->screen and $this->history buffers * * @access private */ function _newLine() { //if ($this->y < $this->max_y) { // $this->y++; //} while ($this->y >= $this->max_y) { $this->history = array_merge($this->history, array(array_shift($this->screen))); $this->screen[] = ''; $this->history_attrs = array_merge($this->history_attrs, array(array_shift($this->attrs))); $this->attrs[] = $this->attr_row; if (count($this->history) >= $this->max_history) { array_shift($this->history); array_shift($this->history_attrs); } $this->y--; } $this->y++; } /** * Returns the current coordinate without preformating * * @access private * @return string */ function _processCoordinate($last_attr, $cur_attr, $char) { $output = ''; if ($last_attr != $cur_attr) { $close = $open = ''; if ($last_attr->foreground != $cur_attr->foreground) { if ($cur_attr->foreground != 'white') { $open.= '<span style="color: ' . $cur_attr->foreground . '">'; } if ($last_attr->foreground != 'white') { $close = '</span>' . $close; } } if ($last_attr->background != $cur_attr->background) { if ($cur_attr->background != 'black') { $open.= '<span style="background: ' . $cur_attr->background . '">'; } if ($last_attr->background != 'black') { $close = '</span>' . $close; } } if ($last_attr->bold != $cur_attr->bold) { if ($cur_attr->bold) { $open.= '<b>'; } else { $close = '</b>' . $close; } } if ($last_attr->underline != $cur_attr->underline) { if ($cur_attr->underline) { $open.= '<u>'; } else { $close = '</u>' . $close; } } if ($last_attr->blink != $cur_attr->blink) { if ($cur_attr->blink) { $open.= '<blink>'; } else { $close = '</blink>' . $close; } } $output.= $close . $open; } $output.= htmlspecialchars($char); return $output; } /** * Returns the current screen without preformating * * @access private * @return string */ function _getScreen() { $output = ''; $last_attr = $this->base_attr_cell; for ($i = 0; $i <= $this->max_y; $i++) { for ($j = 0; $j <= $this->max_x; $j++) { $cur_attr = $this->attrs[$i][$j]; $output.= $this->_processCoordinate($last_attr, $cur_attr, isset($this->screen[$i][$j]) ? $this->screen[$i][$j] : ''); $last_attr = $this->attrs[$i][$j]; } $output.= "\r\n"; } $output = substr($output, 0, -2); // close any remaining open tags $output.= $this->_processCoordinate($last_attr, $this->base_attr_cell, ''); return rtrim($output); } /** * Returns the current screen * * @access public * @return string */ function getScreen() { return '<pre width="' . ($this->max_x + 1) . '" style="color: white; background: black">' . $this->_getScreen() . '</pre>'; } /** * Returns the current screen and the x previous lines * * @access public * @return string */ function getHistory() { $scrollback = ''; $last_attr = $this->base_attr_cell; for ($i = 0; $i < count($this->history); $i++) { for ($j = 0; $j <= $this->max_x + 1; $j++) { $cur_attr = $this->history_attrs[$i][$j]; $scrollback.= $this->_processCoordinate($last_attr, $cur_attr, isset($this->history[$i][$j]) ? $this->history[$i][$j] : ''); $last_attr = $this->history_attrs[$i][$j]; } $scrollback.= "\r\n"; } $base_attr_cell = $this->base_attr_cell; $this->base_attr_cell = $last_attr; $scrollback.= $this->_getScreen(); $this->base_attr_cell = $base_attr_cell; return '<pre width="' . ($this->max_x + 1) . '" style="color: white; background: black">' . $scrollback . '</span></pre>'; } } <?php /** * Pure-PHP X.509 Parser * * PHP version 5 * * Encode and decode X.509 certificates. * * The extensions are from {@link http://tools.ietf.org/html/rfc5280 RFC5280} and * {@link http://web.archive.org/web/19961027104704/http://www3.netscape.com/eng/security/cert-exts.html Netscape Certificate Extensions}. * * Note that loading an X.509 certificate and resaving it may invalidate the signature. The reason being that the signature is based on a * portion of the certificate that contains optional parameters with default values. ie. if the parameter isn't there the default value is * used. Problem is, if the parameter is there and it just so happens to have the default value there are two ways that that parameter can * be encoded. It can be encoded explicitly or left out all together. This would effect the signature value and thus may invalidate the * the certificate all together unless the certificate is re-signed. * * @category File * @package X509 * @author Jim Wigginton <terrafrost@php.net> * @copyright 2012 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\File; use phpseclib\Crypt\Hash; use phpseclib\Crypt\Random; use phpseclib\Crypt\RSA; use phpseclib\File\ASN1\Element; use phpseclib\Math\BigInteger; use DateTime; use DateTimeZone; /** * Pure-PHP X.509 Parser * * @package X509 * @author Jim Wigginton <terrafrost@php.net> * @access public */ class X509 { /** * Flag to only accept signatures signed by certificate authorities * * Not really used anymore but retained all the same to suppress E_NOTICEs from old installs * * @access public */ const VALIDATE_SIGNATURE_BY_CA = 1; /**#@+ * @access public * @see \phpseclib\File\X509::getDN() */ /** * Return internal array representation */ const DN_ARRAY = 0; /** * Return string */ const DN_STRING = 1; /** * Return ASN.1 name string */ const DN_ASN1 = 2; /** * Return OpenSSL compatible array */ const DN_OPENSSL = 3; /** * Return canonical ASN.1 RDNs string */ const DN_CANON = 4; /** * Return name hash for file indexing */ const DN_HASH = 5; /**#@-*/ /**#@+ * @access public * @see \phpseclib\File\X509::saveX509() * @see \phpseclib\File\X509::saveCSR() * @see \phpseclib\File\X509::saveCRL() */ /** * Save as PEM * * ie. a base64-encoded PEM with a header and a footer */ const FORMAT_PEM = 0; /** * Save as DER */ const FORMAT_DER = 1; /** * Save as a SPKAC * * Only works on CSRs. Not currently supported. */ const FORMAT_SPKAC = 2; /** * Auto-detect the format * * Used only by the load*() functions */ const FORMAT_AUTO_DETECT = 3; /**#@-*/ /** * Attribute value disposition. * If disposition is >= 0, this is the index of the target value. */ const ATTR_ALL = -1; // All attribute values (array). const ATTR_APPEND = -2; // Add a value. const ATTR_REPLACE = -3; // Clear first, then add a value. /** * ASN.1 syntax for X.509 certificates * * @var array * @access private */ var $Certificate; /**#@+ * ASN.1 syntax for various extensions * * @access private */ var $DirectoryString; var $PKCS9String; var $AttributeValue; var $Extensions; var $KeyUsage; var $ExtKeyUsageSyntax; var $BasicConstraints; var $KeyIdentifier; var $CRLDistributionPoints; var $AuthorityKeyIdentifier; var $CertificatePolicies; var $AuthorityInfoAccessSyntax; var $SubjectAltName; var $SubjectDirectoryAttributes; var $PrivateKeyUsagePeriod; var $IssuerAltName; var $PolicyMappings; var $NameConstraints; var $CPSuri; var $UserNotice; var $netscape_cert_type; var $netscape_comment; var $netscape_ca_policy_url; var $Name; var $RelativeDistinguishedName; var $CRLNumber; var $CRLReason; var $IssuingDistributionPoint; var $InvalidityDate; var $CertificateIssuer; var $HoldInstructionCode; var $SignedPublicKeyAndChallenge; /**#@-*/ /**#@+ * ASN.1 syntax for various DN attributes * * @access private */ var $PostalAddress; /**#@-*/ /** * ASN.1 syntax for Certificate Signing Requests (RFC2986) * * @var array * @access private */ var $CertificationRequest; /** * ASN.1 syntax for Certificate Revocation Lists (RFC5280) * * @var array * @access private */ var $CertificateList; /** * Distinguished Name * * @var array * @access private */ var $dn; /** * Public key * * @var string * @access private */ var $publicKey; /** * Private key * * @var string * @access private */ var $privateKey; /** * Object identifiers for X.509 certificates * * @var array * @access private * @link http://en.wikipedia.org/wiki/Object_identifier */ var $oids; /** * The certificate authorities * * @var array * @access private */ var $CAs; /** * The currently loaded certificate * * @var array * @access private */ var $currentCert; /** * The signature subject * * There's no guarantee \phpseclib\File\X509 is going to re-encode an X.509 cert in the same way it was originally * encoded so we take save the portion of the original cert that the signature would have made for. * * @var string * @access private */ var $signatureSubject; /** * Certificate Start Date * * @var string * @access private */ var $startDate; /** * Certificate End Date * * @var string * @access private */ var $endDate; /** * Serial Number * * @var string * @access private */ var $serialNumber; /** * Key Identifier * * See {@link http://tools.ietf.org/html/rfc5280#section-4.2.1.1 RFC5280#section-4.2.1.1} and * {@link http://tools.ietf.org/html/rfc5280#section-4.2.1.2 RFC5280#section-4.2.1.2}. * * @var string * @access private */ var $currentKeyIdentifier; /** * CA Flag * * @var bool * @access private */ var $caFlag = false; /** * SPKAC Challenge * * @var string * @access private */ var $challenge; /** * Recursion Limit * * @var int * @access private */ static $recur_limit = 5; /** * URL fetch flag * * @var bool * @access private */ static $disable_url_fetch = false; /** * Default Constructor. * * @return \phpseclib\File\X509 * @access public */ function __construct() { // Explicitly Tagged Module, 1988 Syntax // http://tools.ietf.org/html/rfc5280#appendix-A.1 $this->DirectoryString = array( 'type' => ASN1::TYPE_CHOICE, 'children' => array( 'teletexString' => array('type' => ASN1::TYPE_TELETEX_STRING), 'printableString' => array('type' => ASN1::TYPE_PRINTABLE_STRING), 'universalString' => array('type' => ASN1::TYPE_UNIVERSAL_STRING), 'utf8String' => array('type' => ASN1::TYPE_UTF8_STRING), 'bmpString' => array('type' => ASN1::TYPE_BMP_STRING) ) ); $this->PKCS9String = array( 'type' => ASN1::TYPE_CHOICE, 'children' => array( 'ia5String' => array('type' => ASN1::TYPE_IA5_STRING), 'directoryString' => $this->DirectoryString ) ); $this->AttributeValue = array('type' => ASN1::TYPE_ANY); $AttributeType = array('type' => ASN1::TYPE_OBJECT_IDENTIFIER); $AttributeTypeAndValue = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'type' => $AttributeType, 'value'=> $this->AttributeValue ) ); /* In practice, RDNs containing multiple name-value pairs (called "multivalued RDNs") are rare, but they can be useful at times when either there is no unique attribute in the entry or you want to ensure that the entry's DN contains some useful identifying information. - https://www.opends.org/wiki/page/DefinitionRelativeDistinguishedName */ $this->RelativeDistinguishedName = array( 'type' => ASN1::TYPE_SET, 'min' => 1, 'max' => -1, 'children' => $AttributeTypeAndValue ); // http://tools.ietf.org/html/rfc5280#section-4.1.2.4 $RDNSequence = array( 'type' => ASN1::TYPE_SEQUENCE, // RDNSequence does not define a min or a max, which means it doesn't have one 'min' => 0, 'max' => -1, 'children' => $this->RelativeDistinguishedName ); $this->Name = array( 'type' => ASN1::TYPE_CHOICE, 'children' => array( 'rdnSequence' => $RDNSequence ) ); // http://tools.ietf.org/html/rfc5280#section-4.1.1.2 $AlgorithmIdentifier = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'algorithm' => array('type' => ASN1::TYPE_OBJECT_IDENTIFIER), 'parameters' => array( 'type' => ASN1::TYPE_ANY, 'optional' => true ) ) ); /* A certificate using system MUST reject the certificate if it encounters a critical extension it does not recognize; however, a non-critical extension may be ignored if it is not recognized. http://tools.ietf.org/html/rfc5280#section-4.2 */ $Extension = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'extnId' => array('type' => ASN1::TYPE_OBJECT_IDENTIFIER), 'critical' => array( 'type' => ASN1::TYPE_BOOLEAN, 'optional' => true, 'default' => false ), 'extnValue' => array('type' => ASN1::TYPE_OCTET_STRING) ) ); $this->Extensions = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, // technically, it's MAX, but we'll assume anything < 0 is MAX 'max' => -1, // if 'children' isn't an array then 'min' and 'max' must be defined 'children' => $Extension ); $SubjectPublicKeyInfo = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'algorithm' => $AlgorithmIdentifier, 'subjectPublicKey' => array('type' => ASN1::TYPE_BIT_STRING) ) ); $UniqueIdentifier = array('type' => ASN1::TYPE_BIT_STRING); $Time = array( 'type' => ASN1::TYPE_CHOICE, 'children' => array( 'utcTime' => array('type' => ASN1::TYPE_UTC_TIME), 'generalTime' => array('type' => ASN1::TYPE_GENERALIZED_TIME) ) ); // http://tools.ietf.org/html/rfc5280#section-4.1.2.5 $Validity = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'notBefore' => $Time, 'notAfter' => $Time ) ); $CertificateSerialNumber = array('type' => ASN1::TYPE_INTEGER); $Version = array( 'type' => ASN1::TYPE_INTEGER, 'mapping' => array('v1', 'v2', 'v3') ); // assert($TBSCertificate['children']['signature'] == $Certificate['children']['signatureAlgorithm']) $TBSCertificate = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( // technically, default implies optional, but we'll define it as being optional, none-the-less, just to // reenforce that fact 'version' => array( 'constant' => 0, 'optional' => true, 'explicit' => true, 'default' => 'v1' ) + $Version, 'serialNumber' => $CertificateSerialNumber, 'signature' => $AlgorithmIdentifier, 'issuer' => $this->Name, 'validity' => $Validity, 'subject' => $this->Name, 'subjectPublicKeyInfo' => $SubjectPublicKeyInfo, // implicit means that the T in the TLV structure is to be rewritten, regardless of the type 'issuerUniqueID' => array( 'constant' => 1, 'optional' => true, 'implicit' => true ) + $UniqueIdentifier, 'subjectUniqueID' => array( 'constant' => 2, 'optional' => true, 'implicit' => true ) + $UniqueIdentifier, // <http://tools.ietf.org/html/rfc2459#page-74> doesn't use the EXPLICIT keyword but if // it's not IMPLICIT, it's EXPLICIT 'extensions' => array( 'constant' => 3, 'optional' => true, 'explicit' => true ) + $this->Extensions ) ); $this->Certificate = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'tbsCertificate' => $TBSCertificate, 'signatureAlgorithm' => $AlgorithmIdentifier, 'signature' => array('type' => ASN1::TYPE_BIT_STRING) ) ); $this->KeyUsage = array( 'type' => ASN1::TYPE_BIT_STRING, 'mapping' => array( 'digitalSignature', 'nonRepudiation', 'keyEncipherment', 'dataEncipherment', 'keyAgreement', 'keyCertSign', 'cRLSign', 'encipherOnly', 'decipherOnly' ) ); $this->BasicConstraints = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'cA' => array( 'type' => ASN1::TYPE_BOOLEAN, 'optional' => true, 'default' => false ), 'pathLenConstraint' => array( 'type' => ASN1::TYPE_INTEGER, 'optional' => true ) ) ); $this->KeyIdentifier = array('type' => ASN1::TYPE_OCTET_STRING); $OrganizationalUnitNames = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => 4, // ub-organizational-units 'children' => array('type' => ASN1::TYPE_PRINTABLE_STRING) ); $PersonalName = array( 'type' => ASN1::TYPE_SET, 'children' => array( 'surname' => array( 'type' => ASN1::TYPE_PRINTABLE_STRING, 'constant' => 0, 'optional' => true, 'implicit' => true ), 'given-name' => array( 'type' => ASN1::TYPE_PRINTABLE_STRING, 'constant' => 1, 'optional' => true, 'implicit' => true ), 'initials' => array( 'type' => ASN1::TYPE_PRINTABLE_STRING, 'constant' => 2, 'optional' => true, 'implicit' => true ), 'generation-qualifier' => array( 'type' => ASN1::TYPE_PRINTABLE_STRING, 'constant' => 3, 'optional' => true, 'implicit' => true ) ) ); $NumericUserIdentifier = array('type' => ASN1::TYPE_NUMERIC_STRING); $OrganizationName = array('type' => ASN1::TYPE_PRINTABLE_STRING); $PrivateDomainName = array( 'type' => ASN1::TYPE_CHOICE, 'children' => array( 'numeric' => array('type' => ASN1::TYPE_NUMERIC_STRING), 'printable' => array('type' => ASN1::TYPE_PRINTABLE_STRING) ) ); $TerminalIdentifier = array('type' => ASN1::TYPE_PRINTABLE_STRING); $NetworkAddress = array('type' => ASN1::TYPE_NUMERIC_STRING); $AdministrationDomainName = array( 'type' => ASN1::TYPE_CHOICE, // if class isn't present it's assumed to be \phpseclib\File\ASN1::CLASS_UNIVERSAL or // (if constant is present) \phpseclib\File\ASN1::CLASS_CONTEXT_SPECIFIC 'class' => ASN1::CLASS_APPLICATION, 'cast' => 2, 'children' => array( 'numeric' => array('type' => ASN1::TYPE_NUMERIC_STRING), 'printable' => array('type' => ASN1::TYPE_PRINTABLE_STRING) ) ); $CountryName = array( 'type' => ASN1::TYPE_CHOICE, // if class isn't present it's assumed to be \phpseclib\File\ASN1::CLASS_UNIVERSAL or // (if constant is present) \phpseclib\File\ASN1::CLASS_CONTEXT_SPECIFIC 'class' => ASN1::CLASS_APPLICATION, 'cast' => 1, 'children' => array( 'x121-dcc-code' => array('type' => ASN1::TYPE_NUMERIC_STRING), 'iso-3166-alpha2-code' => array('type' => ASN1::TYPE_PRINTABLE_STRING) ) ); $AnotherName = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'type-id' => array('type' => ASN1::TYPE_OBJECT_IDENTIFIER), 'value' => array( 'type' => ASN1::TYPE_ANY, 'constant' => 0, 'optional' => true, 'explicit' => true ) ) ); $ExtensionAttribute = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'extension-attribute-type' => array( 'type' => ASN1::TYPE_PRINTABLE_STRING, 'constant' => 0, 'optional' => true, 'implicit' => true ), 'extension-attribute-value' => array( 'type' => ASN1::TYPE_ANY, 'constant' => 1, 'optional' => true, 'explicit' => true ) ) ); $ExtensionAttributes = array( 'type' => ASN1::TYPE_SET, 'min' => 1, 'max' => 256, // ub-extension-attributes 'children' => $ExtensionAttribute ); $BuiltInDomainDefinedAttribute = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'type' => array('type' => ASN1::TYPE_PRINTABLE_STRING), 'value' => array('type' => ASN1::TYPE_PRINTABLE_STRING) ) ); $BuiltInDomainDefinedAttributes = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => 4, // ub-domain-defined-attributes 'children' => $BuiltInDomainDefinedAttribute ); $BuiltInStandardAttributes = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'country-name' => array('optional' => true) + $CountryName, 'administration-domain-name' => array('optional' => true) + $AdministrationDomainName, 'network-address' => array( 'constant' => 0, 'optional' => true, 'implicit' => true ) + $NetworkAddress, 'terminal-identifier' => array( 'constant' => 1, 'optional' => true, 'implicit' => true ) + $TerminalIdentifier, 'private-domain-name' => array( 'constant' => 2, 'optional' => true, 'explicit' => true ) + $PrivateDomainName, 'organization-name' => array( 'constant' => 3, 'optional' => true, 'implicit' => true ) + $OrganizationName, 'numeric-user-identifier' => array( 'constant' => 4, 'optional' => true, 'implicit' => true ) + $NumericUserIdentifier, 'personal-name' => array( 'constant' => 5, 'optional' => true, 'implicit' => true ) + $PersonalName, 'organizational-unit-names' => array( 'constant' => 6, 'optional' => true, 'implicit' => true ) + $OrganizationalUnitNames ) ); $ORAddress = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'built-in-standard-attributes' => $BuiltInStandardAttributes, 'built-in-domain-defined-attributes' => array('optional' => true) + $BuiltInDomainDefinedAttributes, 'extension-attributes' => array('optional' => true) + $ExtensionAttributes ) ); $EDIPartyName = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'nameAssigner' => array( 'constant' => 0, 'optional' => true, 'implicit' => true ) + $this->DirectoryString, // partyName is technically required but \phpseclib\File\ASN1 doesn't currently support non-optional constants and // setting it to optional gets the job done in any event. 'partyName' => array( 'constant' => 1, 'optional' => true, 'implicit' => true ) + $this->DirectoryString ) ); $GeneralName = array( 'type' => ASN1::TYPE_CHOICE, 'children' => array( 'otherName' => array( 'constant' => 0, 'optional' => true, 'implicit' => true ) + $AnotherName, 'rfc822Name' => array( 'type' => ASN1::TYPE_IA5_STRING, 'constant' => 1, 'optional' => true, 'implicit' => true ), 'dNSName' => array( 'type' => ASN1::TYPE_IA5_STRING, 'constant' => 2, 'optional' => true, 'implicit' => true ), 'x400Address' => array( 'constant' => 3, 'optional' => true, 'implicit' => true ) + $ORAddress, 'directoryName' => array( 'constant' => 4, 'optional' => true, 'explicit' => true ) + $this->Name, 'ediPartyName' => array( 'constant' => 5, 'optional' => true, 'implicit' => true ) + $EDIPartyName, 'uniformResourceIdentifier' => array( 'type' => ASN1::TYPE_IA5_STRING, 'constant' => 6, 'optional' => true, 'implicit' => true ), 'iPAddress' => array( 'type' => ASN1::TYPE_OCTET_STRING, 'constant' => 7, 'optional' => true, 'implicit' => true ), 'registeredID' => array( 'type' => ASN1::TYPE_OBJECT_IDENTIFIER, 'constant' => 8, 'optional' => true, 'implicit' => true ) ) ); $GeneralNames = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => -1, 'children' => $GeneralName ); $this->IssuerAltName = $GeneralNames; $ReasonFlags = array( 'type' => ASN1::TYPE_BIT_STRING, 'mapping' => array( 'unused', 'keyCompromise', 'cACompromise', 'affiliationChanged', 'superseded', 'cessationOfOperation', 'certificateHold', 'privilegeWithdrawn', 'aACompromise' ) ); $DistributionPointName = array( 'type' => ASN1::TYPE_CHOICE, 'children' => array( 'fullName' => array( 'constant' => 0, 'optional' => true, 'implicit' => true ) + $GeneralNames, 'nameRelativeToCRLIssuer' => array( 'constant' => 1, 'optional' => true, 'implicit' => true ) + $this->RelativeDistinguishedName ) ); $DistributionPoint = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'distributionPoint' => array( 'constant' => 0, 'optional' => true, 'explicit' => true ) + $DistributionPointName, 'reasons' => array( 'constant' => 1, 'optional' => true, 'implicit' => true ) + $ReasonFlags, 'cRLIssuer' => array( 'constant' => 2, 'optional' => true, 'implicit' => true ) + $GeneralNames ) ); $this->CRLDistributionPoints = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => -1, 'children' => $DistributionPoint ); $this->AuthorityKeyIdentifier = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'keyIdentifier' => array( 'constant' => 0, 'optional' => true, 'implicit' => true ) + $this->KeyIdentifier, 'authorityCertIssuer' => array( 'constant' => 1, 'optional' => true, 'implicit' => true ) + $GeneralNames, 'authorityCertSerialNumber' => array( 'constant' => 2, 'optional' => true, 'implicit' => true ) + $CertificateSerialNumber ) ); $PolicyQualifierId = array('type' => ASN1::TYPE_OBJECT_IDENTIFIER); $PolicyQualifierInfo = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'policyQualifierId' => $PolicyQualifierId, 'qualifier' => array('type' => ASN1::TYPE_ANY) ) ); $CertPolicyId = array('type' => ASN1::TYPE_OBJECT_IDENTIFIER); $PolicyInformation = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'policyIdentifier' => $CertPolicyId, 'policyQualifiers' => array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 0, 'max' => -1, 'optional' => true, 'children' => $PolicyQualifierInfo ) ) ); $this->CertificatePolicies = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => -1, 'children' => $PolicyInformation ); $this->PolicyMappings = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => -1, 'children' => array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'issuerDomainPolicy' => $CertPolicyId, 'subjectDomainPolicy' => $CertPolicyId ) ) ); $KeyPurposeId = array('type' => ASN1::TYPE_OBJECT_IDENTIFIER); $this->ExtKeyUsageSyntax = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => -1, 'children' => $KeyPurposeId ); $AccessDescription = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'accessMethod' => array('type' => ASN1::TYPE_OBJECT_IDENTIFIER), 'accessLocation' => $GeneralName ) ); $this->AuthorityInfoAccessSyntax = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => -1, 'children' => $AccessDescription ); $this->SubjectInfoAccessSyntax = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => -1, 'children' => $AccessDescription ); $this->SubjectAltName = $GeneralNames; $this->PrivateKeyUsagePeriod = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'notBefore' => array( 'constant' => 0, 'optional' => true, 'implicit' => true, 'type' => ASN1::TYPE_GENERALIZED_TIME), 'notAfter' => array( 'constant' => 1, 'optional' => true, 'implicit' => true, 'type' => ASN1::TYPE_GENERALIZED_TIME) ) ); $BaseDistance = array('type' => ASN1::TYPE_INTEGER); $GeneralSubtree = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'base' => $GeneralName, 'minimum' => array( 'constant' => 0, 'optional' => true, 'implicit' => true, 'default' => new BigInteger(0) ) + $BaseDistance, 'maximum' => array( 'constant' => 1, 'optional' => true, 'implicit' => true, ) + $BaseDistance ) ); $GeneralSubtrees = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => -1, 'children' => $GeneralSubtree ); $this->NameConstraints = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'permittedSubtrees' => array( 'constant' => 0, 'optional' => true, 'implicit' => true ) + $GeneralSubtrees, 'excludedSubtrees' => array( 'constant' => 1, 'optional' => true, 'implicit' => true ) + $GeneralSubtrees ) ); $this->CPSuri = array('type' => ASN1::TYPE_IA5_STRING); $DisplayText = array( 'type' => ASN1::TYPE_CHOICE, 'children' => array( 'ia5String' => array('type' => ASN1::TYPE_IA5_STRING), 'visibleString' => array('type' => ASN1::TYPE_VISIBLE_STRING), 'bmpString' => array('type' => ASN1::TYPE_BMP_STRING), 'utf8String' => array('type' => ASN1::TYPE_UTF8_STRING) ) ); $NoticeReference = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'organization' => $DisplayText, 'noticeNumbers' => array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => 200, 'children' => array('type' => ASN1::TYPE_INTEGER) ) ) ); $this->UserNotice = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'noticeRef' => array( 'optional' => true, 'implicit' => true ) + $NoticeReference, 'explicitText' => array( 'optional' => true, 'implicit' => true ) + $DisplayText ) ); // mapping is from <http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html> $this->netscape_cert_type = array( 'type' => ASN1::TYPE_BIT_STRING, 'mapping' => array( 'SSLClient', 'SSLServer', 'Email', 'ObjectSigning', 'Reserved', 'SSLCA', 'EmailCA', 'ObjectSigningCA' ) ); $this->netscape_comment = array('type' => ASN1::TYPE_IA5_STRING); $this->netscape_ca_policy_url = array('type' => ASN1::TYPE_IA5_STRING); // attribute is used in RFC2986 but we're using the RFC5280 definition $Attribute = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'type' => $AttributeType, 'value'=> array( 'type' => ASN1::TYPE_SET, 'min' => 1, 'max' => -1, 'children' => $this->AttributeValue ) ) ); $this->SubjectDirectoryAttributes = array( 'type' => ASN1::TYPE_SEQUENCE, 'min' => 1, 'max' => -1, 'children' => $Attribute ); // adapted from <http://tools.ietf.org/html/rfc2986> $Attributes = array( 'type' => ASN1::TYPE_SET, 'min' => 1, 'max' => -1, 'children' => $Attribute ); $CertificationRequestInfo = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'version' => array( 'type' => ASN1::TYPE_INTEGER, 'mapping' => array('v1') ), 'subject' => $this->Name, 'subjectPKInfo' => $SubjectPublicKeyInfo, 'attributes' => array( 'constant' => 0, 'optional' => true, 'implicit' => true ) + $Attributes, ) ); $this->CertificationRequest = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'certificationRequestInfo' => $CertificationRequestInfo, 'signatureAlgorithm' => $AlgorithmIdentifier, 'signature' => array('type' => ASN1::TYPE_BIT_STRING) ) ); $RevokedCertificate = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'userCertificate' => $CertificateSerialNumber, 'revocationDate' => $Time, 'crlEntryExtensions' => array( 'optional' => true ) + $this->Extensions ) ); $TBSCertList = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'version' => array( 'optional' => true, 'default' => 'v1' ) + $Version, 'signature' => $AlgorithmIdentifier, 'issuer' => $this->Name, 'thisUpdate' => $Time, 'nextUpdate' => array( 'optional' => true ) + $Time, 'revokedCertificates' => array( 'type' => ASN1::TYPE_SEQUENCE, 'optional' => true, 'min' => 0, 'max' => -1, 'children' => $RevokedCertificate ), 'crlExtensions' => array( 'constant' => 0, 'optional' => true, 'explicit' => true ) + $this->Extensions ) ); $this->CertificateList = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'tbsCertList' => $TBSCertList, 'signatureAlgorithm' => $AlgorithmIdentifier, 'signature' => array('type' => ASN1::TYPE_BIT_STRING) ) ); $this->CRLNumber = array('type' => ASN1::TYPE_INTEGER); $this->CRLReason = array('type' => ASN1::TYPE_ENUMERATED, 'mapping' => array( 'unspecified', 'keyCompromise', 'cACompromise', 'affiliationChanged', 'superseded', 'cessationOfOperation', 'certificateHold', // Value 7 is not used. 8 => 'removeFromCRL', 'privilegeWithdrawn', 'aACompromise' ) ); $this->IssuingDistributionPoint = array('type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'distributionPoint' => array( 'constant' => 0, 'optional' => true, 'explicit' => true ) + $DistributionPointName, 'onlyContainsUserCerts' => array( 'type' => ASN1::TYPE_BOOLEAN, 'constant' => 1, 'optional' => true, 'default' => false, 'implicit' => true ), 'onlyContainsCACerts' => array( 'type' => ASN1::TYPE_BOOLEAN, 'constant' => 2, 'optional' => true, 'default' => false, 'implicit' => true ), 'onlySomeReasons' => array( 'constant' => 3, 'optional' => true, 'implicit' => true ) + $ReasonFlags, 'indirectCRL' => array( 'type' => ASN1::TYPE_BOOLEAN, 'constant' => 4, 'optional' => true, 'default' => false, 'implicit' => true ), 'onlyContainsAttributeCerts' => array( 'type' => ASN1::TYPE_BOOLEAN, 'constant' => 5, 'optional' => true, 'default' => false, 'implicit' => true ) ) ); $this->InvalidityDate = array('type' => ASN1::TYPE_GENERALIZED_TIME); $this->CertificateIssuer = $GeneralNames; $this->HoldInstructionCode = array('type' => ASN1::TYPE_OBJECT_IDENTIFIER); $PublicKeyAndChallenge = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'spki' => $SubjectPublicKeyInfo, 'challenge' => array('type' => ASN1::TYPE_IA5_STRING) ) ); $this->SignedPublicKeyAndChallenge = array( 'type' => ASN1::TYPE_SEQUENCE, 'children' => array( 'publicKeyAndChallenge' => $PublicKeyAndChallenge, 'signatureAlgorithm' => $AlgorithmIdentifier, 'signature' => array('type' => ASN1::TYPE_BIT_STRING) ) ); $this->PostalAddress = array( 'type' => ASN1::TYPE_SEQUENCE, 'optional' => true, 'min' => 1, 'max' => -1, 'children' => $this->DirectoryString ); // OIDs from RFC5280 and those RFCs mentioned in RFC5280#section-4.1.1.2 $this->oids = array( '1.3.6.1.5.5.7' => 'id-pkix', '1.3.6.1.5.5.7.1' => 'id-pe', '1.3.6.1.5.5.7.2' => 'id-qt', '1.3.6.1.5.5.7.3' => 'id-kp', '1.3.6.1.5.5.7.48' => 'id-ad', '1.3.6.1.5.5.7.2.1' => 'id-qt-cps', '1.3.6.1.5.5.7.2.2' => 'id-qt-unotice', '1.3.6.1.5.5.7.48.1' =>'id-ad-ocsp', '1.3.6.1.5.5.7.48.2' => 'id-ad-caIssuers', '1.3.6.1.5.5.7.48.3' => 'id-ad-timeStamping', '1.3.6.1.5.5.7.48.5' => 'id-ad-caRepository', '2.5.4' => 'id-at', '2.5.4.41' => 'id-at-name', '2.5.4.4' => 'id-at-surname', '2.5.4.42' => 'id-at-givenName', '2.5.4.43' => 'id-at-initials', '2.5.4.44' => 'id-at-generationQualifier', '2.5.4.3' => 'id-at-commonName', '2.5.4.7' => 'id-at-localityName', '2.5.4.8' => 'id-at-stateOrProvinceName', '2.5.4.10' => 'id-at-organizationName', '2.5.4.11' => 'id-at-organizationalUnitName', '2.5.4.12' => 'id-at-title', '2.5.4.13' => 'id-at-description', '2.5.4.46' => 'id-at-dnQualifier', '2.5.4.6' => 'id-at-countryName', '2.5.4.5' => 'id-at-serialNumber', '2.5.4.65' => 'id-at-pseudonym', '2.5.4.17' => 'id-at-postalCode', '2.5.4.9' => 'id-at-streetAddress', '2.5.4.45' => 'id-at-uniqueIdentifier', '2.5.4.72' => 'id-at-role', '2.5.4.16' => 'id-at-postalAddress', '0.9.2342.19200300.100.1.25' => 'id-domainComponent', '1.2.840.113549.1.9' => 'pkcs-9', '1.2.840.113549.1.9.1' => 'pkcs-9-at-emailAddress', '2.5.29' => 'id-ce', '2.5.29.35' => 'id-ce-authorityKeyIdentifier', '2.5.29.14' => 'id-ce-subjectKeyIdentifier', '2.5.29.15' => 'id-ce-keyUsage', '2.5.29.16' => 'id-ce-privateKeyUsagePeriod', '2.5.29.32' => 'id-ce-certificatePolicies', '2.5.29.32.0' => 'anyPolicy', '2.5.29.33' => 'id-ce-policyMappings', '2.5.29.17' => 'id-ce-subjectAltName', '2.5.29.18' => 'id-ce-issuerAltName', '2.5.29.9' => 'id-ce-subjectDirectoryAttributes', '2.5.29.19' => 'id-ce-basicConstraints', '2.5.29.30' => 'id-ce-nameConstraints', '2.5.29.36' => 'id-ce-policyConstraints', '2.5.29.31' => 'id-ce-cRLDistributionPoints', '2.5.29.37' => 'id-ce-extKeyUsage', '2.5.29.37.0' => 'anyExtendedKeyUsage', '1.3.6.1.5.5.7.3.1' => 'id-kp-serverAuth', '1.3.6.1.5.5.7.3.2' => 'id-kp-clientAuth', '1.3.6.1.5.5.7.3.3' => 'id-kp-codeSigning', '1.3.6.1.5.5.7.3.4' => 'id-kp-emailProtection', '1.3.6.1.5.5.7.3.8' => 'id-kp-timeStamping', '1.3.6.1.5.5.7.3.9' => 'id-kp-OCSPSigning', '2.5.29.54' => 'id-ce-inhibitAnyPolicy', '2.5.29.46' => 'id-ce-freshestCRL', '1.3.6.1.5.5.7.1.1' => 'id-pe-authorityInfoAccess', '1.3.6.1.5.5.7.1.11' => 'id-pe-subjectInfoAccess', '2.5.29.20' => 'id-ce-cRLNumber', '2.5.29.28' => 'id-ce-issuingDistributionPoint', '2.5.29.27' => 'id-ce-deltaCRLIndicator', '2.5.29.21' => 'id-ce-cRLReasons', '2.5.29.29' => 'id-ce-certificateIssuer', '2.5.29.23' => 'id-ce-holdInstructionCode', '1.2.840.10040.2' => 'holdInstruction', '1.2.840.10040.2.1' => 'id-holdinstruction-none', '1.2.840.10040.2.2' => 'id-holdinstruction-callissuer', '1.2.840.10040.2.3' => 'id-holdinstruction-reject', '2.5.29.24' => 'id-ce-invalidityDate', '1.2.840.113549.2.2' => 'md2', '1.2.840.113549.2.5' => 'md5', '1.3.14.3.2.26' => 'id-sha1', '1.2.840.10040.4.1' => 'id-dsa', '1.2.840.10040.4.3' => 'id-dsa-with-sha1', '1.2.840.113549.1.1' => 'pkcs-1', '1.2.840.113549.1.1.1' => 'rsaEncryption', '1.2.840.113549.1.1.2' => 'md2WithRSAEncryption', '1.2.840.113549.1.1.4' => 'md5WithRSAEncryption', '1.2.840.113549.1.1.5' => 'sha1WithRSAEncryption', '1.2.840.10046.2.1' => 'dhpublicnumber', '2.16.840.1.101.2.1.1.22' => 'id-keyExchangeAlgorithm', '1.2.840.10045' => 'ansi-X9-62', '1.2.840.10045.4' => 'id-ecSigType', '1.2.840.10045.4.1' => 'ecdsa-with-SHA1', '1.2.840.10045.1' => 'id-fieldType', '1.2.840.10045.1.1' => 'prime-field', '1.2.840.10045.1.2' => 'characteristic-two-field', '1.2.840.10045.1.2.3' => 'id-characteristic-two-basis', '1.2.840.10045.1.2.3.1' => 'gnBasis', '1.2.840.10045.1.2.3.2' => 'tpBasis', '1.2.840.10045.1.2.3.3' => 'ppBasis', '1.2.840.10045.2' => 'id-publicKeyType', '1.2.840.10045.2.1' => 'id-ecPublicKey', '1.2.840.10045.3' => 'ellipticCurve', '1.2.840.10045.3.0' => 'c-TwoCurve', '1.2.840.10045.3.0.1' => 'c2pnb163v1', '1.2.840.10045.3.0.2' => 'c2pnb163v2', '1.2.840.10045.3.0.3' => 'c2pnb163v3', '1.2.840.10045.3.0.4' => 'c2pnb176w1', '1.2.840.10045.3.0.5' => 'c2pnb191v1', '1.2.840.10045.3.0.6' => 'c2pnb191v2', '1.2.840.10045.3.0.7' => 'c2pnb191v3', '1.2.840.10045.3.0.8' => 'c2pnb191v4', '1.2.840.10045.3.0.9' => 'c2pnb191v5', '1.2.840.10045.3.0.10' => 'c2pnb208w1', '1.2.840.10045.3.0.11' => 'c2pnb239v1', '1.2.840.10045.3.0.12' => 'c2pnb239v2', '1.2.840.10045.3.0.13' => 'c2pnb239v3', '1.2.840.10045.3.0.14' => 'c2pnb239v4', '1.2.840.10045.3.0.15' => 'c2pnb239v5', '1.2.840.10045.3.0.16' => 'c2pnb272w1', '1.2.840.10045.3.0.17' => 'c2pnb304w1', '1.2.840.10045.3.0.18' => 'c2pnb359v1', '1.2.840.10045.3.0.19' => 'c2pnb368w1', '1.2.840.10045.3.0.20' => 'c2pnb431r1', '1.2.840.10045.3.1' => 'primeCurve', '1.2.840.10045.3.1.1' => 'prime192v1', '1.2.840.10045.3.1.2' => 'prime192v2', '1.2.840.10045.3.1.3' => 'prime192v3', '1.2.840.10045.3.1.4' => 'prime239v1', '1.2.840.10045.3.1.5' => 'prime239v2', '1.2.840.10045.3.1.6' => 'prime239v3', '1.2.840.10045.3.1.7' => 'prime256v1', '1.2.840.113549.1.1.7' => 'id-RSAES-OAEP', '1.2.840.113549.1.1.9' => 'id-pSpecified', '1.2.840.113549.1.1.10' => 'id-RSASSA-PSS', '1.2.840.113549.1.1.8' => 'id-mgf1', '1.2.840.113549.1.1.14' => 'sha224WithRSAEncryption', '1.2.840.113549.1.1.11' => 'sha256WithRSAEncryption', '1.2.840.113549.1.1.12' => 'sha384WithRSAEncryption', '1.2.840.113549.1.1.13' => 'sha512WithRSAEncryption', '2.16.840.1.101.3.4.2.4' => 'id-sha224', '2.16.840.1.101.3.4.2.1' => 'id-sha256', '2.16.840.1.101.3.4.2.2' => 'id-sha384', '2.16.840.1.101.3.4.2.3' => 'id-sha512', '1.2.643.2.2.4' => 'id-GostR3411-94-with-GostR3410-94', '1.2.643.2.2.3' => 'id-GostR3411-94-with-GostR3410-2001', '1.2.643.2.2.20' => 'id-GostR3410-2001', '1.2.643.2.2.19' => 'id-GostR3410-94', // Netscape Object Identifiers from "Netscape Certificate Extensions" '2.16.840.1.113730' => 'netscape', '2.16.840.1.113730.1' => 'netscape-cert-extension', '2.16.840.1.113730.1.1' => 'netscape-cert-type', '2.16.840.1.113730.1.13' => 'netscape-comment', '2.16.840.1.113730.1.8' => 'netscape-ca-policy-url', // the following are X.509 extensions not supported by phpseclib '1.3.6.1.5.5.7.1.12' => 'id-pe-logotype', '1.2.840.113533.7.65.0' => 'entrustVersInfo', '2.16.840.1.113733.1.6.9' => 'verisignPrivate', // for Certificate Signing Requests // see http://tools.ietf.org/html/rfc2985 '1.2.840.113549.1.9.2' => 'pkcs-9-at-unstructuredName', // PKCS #9 unstructured name '1.2.840.113549.1.9.7' => 'pkcs-9-at-challengePassword', // Challenge password for certificate revocations '1.2.840.113549.1.9.14' => 'pkcs-9-at-extensionRequest' // Certificate extension request ); } /** * Load X.509 certificate * * Returns an associative array describing the X.509 cert or a false if the cert failed to load * * @param string $cert * @param int $mode * @access public * @return mixed */ function loadX509($cert, $mode = self::FORMAT_AUTO_DETECT) { if (is_array($cert) && isset($cert['tbsCertificate'])) { unset($this->currentCert); unset($this->currentKeyIdentifier); $this->dn = $cert['tbsCertificate']['subject']; if (!isset($this->dn)) { return false; } $this->currentCert = $cert; $currentKeyIdentifier = $this->getExtension('id-ce-subjectKeyIdentifier'); $this->currentKeyIdentifier = is_string($currentKeyIdentifier) ? $currentKeyIdentifier : null; unset($this->signatureSubject); return $cert; } $asn1 = new ASN1(); if ($mode != self::FORMAT_DER) { $newcert = $this->_extractBER($cert); if ($mode == self::FORMAT_PEM && $cert == $newcert) { return false; } $cert = $newcert; } if ($cert === false) { $this->currentCert = false; return false; } $asn1->loadOIDs($this->oids); $decoded = $asn1->decodeBER($cert); if (!empty($decoded)) { $x509 = $asn1->asn1map($decoded[0], $this->Certificate); } if (!isset($x509) || $x509 === false) { $this->currentCert = false; return false; } $this->signatureSubject = substr($cert, $decoded[0]['content'][0]['start'], $decoded[0]['content'][0]['length']); if ($this->_isSubArrayValid($x509, 'tbsCertificate/extensions')) { $this->_mapInExtensions($x509, 'tbsCertificate/extensions', $asn1); } $this->_mapInDNs($x509, 'tbsCertificate/issuer/rdnSequence', $asn1); $this->_mapInDNs($x509, 'tbsCertificate/subject/rdnSequence', $asn1); $key = &$x509['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey']; $key = $this->_reformatKey($x509['tbsCertificate']['subjectPublicKeyInfo']['algorithm']['algorithm'], $key); $this->currentCert = $x509; $this->dn = $x509['tbsCertificate']['subject']; $currentKeyIdentifier = $this->getExtension('id-ce-subjectKeyIdentifier'); $this->currentKeyIdentifier = is_string($currentKeyIdentifier) ? $currentKeyIdentifier : null; return $x509; } /** * Save X.509 certificate * * @param array $cert * @param int $format optional * @access public * @return string */ function saveX509($cert, $format = self::FORMAT_PEM) { if (!is_array($cert) || !isset($cert['tbsCertificate'])) { return false; } switch (true) { // "case !$a: case !$b: break; default: whatever();" is the same thing as "if ($a && $b) whatever()" case !($algorithm = $this->_subArray($cert, 'tbsCertificate/subjectPublicKeyInfo/algorithm/algorithm')): case is_object($cert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey']): break; default: switch ($algorithm) { case 'rsaEncryption': $cert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'] = base64_encode("\0" . base64_decode(preg_replace('#-.+-|[\r\n]#', '', $cert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey']))); /* "[For RSA keys] the parameters field MUST have ASN.1 type NULL for this algorithm identifier." -- https://tools.ietf.org/html/rfc3279#section-2.3.1 given that and the fact that RSA keys appear ot be the only key type for which the parameters field can be blank, it seems like perhaps the ASN.1 description ought not say the parameters field is OPTIONAL, but whatever. */ $cert['tbsCertificate']['subjectPublicKeyInfo']['algorithm']['parameters'] = null; // https://tools.ietf.org/html/rfc3279#section-2.2.1 $cert['signatureAlgorithm']['parameters'] = null; $cert['tbsCertificate']['signature']['parameters'] = null; } } $asn1 = new ASN1(); $asn1->loadOIDs($this->oids); $filters = array(); $type_utf8_string = array('type' => ASN1::TYPE_UTF8_STRING); $filters['tbsCertificate']['signature']['parameters'] = $type_utf8_string; $filters['tbsCertificate']['signature']['issuer']['rdnSequence']['value'] = $type_utf8_string; $filters['tbsCertificate']['issuer']['rdnSequence']['value'] = $type_utf8_string; $filters['tbsCertificate']['subject']['rdnSequence']['value'] = $type_utf8_string; $filters['tbsCertificate']['subjectPublicKeyInfo']['algorithm']['parameters'] = $type_utf8_string; $filters['signatureAlgorithm']['parameters'] = $type_utf8_string; $filters['authorityCertIssuer']['directoryName']['rdnSequence']['value'] = $type_utf8_string; //$filters['policyQualifiers']['qualifier'] = $type_utf8_string; $filters['distributionPoint']['fullName']['directoryName']['rdnSequence']['value'] = $type_utf8_string; $filters['directoryName']['rdnSequence']['value'] = $type_utf8_string; /* in the case of policyQualifiers/qualifier, the type has to be \phpseclib\File\ASN1::TYPE_IA5_STRING. \phpseclib\File\ASN1::TYPE_PRINTABLE_STRING will cause OpenSSL's X.509 parser to spit out random characters. */ $filters['policyQualifiers']['qualifier'] = array('type' => ASN1::TYPE_IA5_STRING); $asn1->loadFilters($filters); $this->_mapOutExtensions($cert, 'tbsCertificate/extensions', $asn1); $this->_mapOutDNs($cert, 'tbsCertificate/issuer/rdnSequence', $asn1); $this->_mapOutDNs($cert, 'tbsCertificate/subject/rdnSequence', $asn1); $cert = $asn1->encodeDER($cert, $this->Certificate); switch ($format) { case self::FORMAT_DER: return $cert; // case self::FORMAT_PEM: default: return "-----BEGIN CERTIFICATE-----\r\n" . chunk_split(base64_encode($cert), 64) . '-----END CERTIFICATE-----'; } } /** * Map extension values from octet string to extension-specific internal * format. * * @param array ref $root * @param string $path * @param object $asn1 * @access private */ function _mapInExtensions(&$root, $path, $asn1) { $extensions = &$this->_subArrayUnchecked($root, $path); if ($extensions) { for ($i = 0; $i < count($extensions); $i++) { $id = $extensions[$i]['extnId']; $value = &$extensions[$i]['extnValue']; $value = base64_decode($value); $decoded = $asn1->decodeBER($value); /* [extnValue] contains the DER encoding of an ASN.1 value corresponding to the extension type identified by extnID */ $map = $this->_getMapping($id); if (!is_bool($map)) { $decoder = $id == 'id-ce-nameConstraints' ? array($this, '_decodeNameConstraintIP') : array($this, '_decodeIP'); $mapped = $asn1->asn1map($decoded[0], $map, array('iPAddress' => $decoder)); $value = $mapped === false ? $decoded[0] : $mapped; if ($id == 'id-ce-certificatePolicies') { for ($j = 0; $j < count($value); $j++) { if (!isset($value[$j]['policyQualifiers'])) { continue; } for ($k = 0; $k < count($value[$j]['policyQualifiers']); $k++) { $subid = $value[$j]['policyQualifiers'][$k]['policyQualifierId']; $map = $this->_getMapping($subid); $subvalue = &$value[$j]['policyQualifiers'][$k]['qualifier']; if ($map !== false) { $decoded = $asn1->decodeBER($subvalue); $mapped = $asn1->asn1map($decoded[0], $map); $subvalue = $mapped === false ? $decoded[0] : $mapped; } } } } } else { $value = base64_encode($value); } } } } /** * Map extension values from extension-specific internal format to * octet string. * * @param array ref $root * @param string $path * @param object $asn1 * @access private */ function _mapOutExtensions(&$root, $path, $asn1) { $extensions = &$this->_subArray($root, $path); if (is_array($extensions)) { $size = count($extensions); for ($i = 0; $i < $size; $i++) { if ($extensions[$i] instanceof Element) { continue; } $id = $extensions[$i]['extnId']; $value = &$extensions[$i]['extnValue']; switch ($id) { case 'id-ce-certificatePolicies': for ($j = 0; $j < count($value); $j++) { if (!isset($value[$j]['policyQualifiers'])) { continue; } for ($k = 0; $k < count($value[$j]['policyQualifiers']); $k++) { $subid = $value[$j]['policyQualifiers'][$k]['policyQualifierId']; $map = $this->_getMapping($subid); $subvalue = &$value[$j]['policyQualifiers'][$k]['qualifier']; if ($map !== false) { // by default \phpseclib\File\ASN1 will try to render qualifier as a \phpseclib\File\ASN1::TYPE_IA5_STRING since it's // actual type is \phpseclib\File\ASN1::TYPE_ANY $subvalue = new Element($asn1->encodeDER($subvalue, $map)); } } } break; case 'id-ce-authorityKeyIdentifier': // use 00 as the serial number instead of an empty string if (isset($value['authorityCertSerialNumber'])) { if ($value['authorityCertSerialNumber']->toBytes() == '') { $temp = chr((ASN1::CLASS_CONTEXT_SPECIFIC << 6) | 2) . "\1\0"; $value['authorityCertSerialNumber'] = new Element($temp); } } } /* [extnValue] contains the DER encoding of an ASN.1 value corresponding to the extension type identified by extnID */ $map = $this->_getMapping($id); if (is_bool($map)) { if (!$map) { user_error($id . ' is not a currently supported extension'); unset($extensions[$i]); } } else { $temp = $asn1->encodeDER($value, $map, array('iPAddress' => array($this, '_encodeIP'))); $value = base64_encode($temp); } } } } /** * Map attribute values from ANY type to attribute-specific internal * format. * * @param array ref $root * @param string $path * @param object $asn1 * @access private */ function _mapInAttributes(&$root, $path, $asn1) { $attributes = &$this->_subArray($root, $path); if (is_array($attributes)) { for ($i = 0; $i < count($attributes); $i++) { $id = $attributes[$i]['type']; /* $value contains the DER encoding of an ASN.1 value corresponding to the attribute type identified by type */ $map = $this->_getMapping($id); if (is_array($attributes[$i]['value'])) { $values = &$attributes[$i]['value']; for ($j = 0; $j < count($values); $j++) { $value = $asn1->encodeDER($values[$j], $this->AttributeValue); $decoded = $asn1->decodeBER($value); if (!is_bool($map)) { $mapped = $asn1->asn1map($decoded[0], $map); if ($mapped !== false) { $values[$j] = $mapped; } if ($id == 'pkcs-9-at-extensionRequest' && $this->_isSubArrayValid($values, $j)) { $this->_mapInExtensions($values, $j, $asn1); } } elseif ($map) { $values[$j] = base64_encode($value); } } } } } } /** * Map attribute values from attribute-specific internal format to * ANY type. * * @param array ref $root * @param string $path * @param object $asn1 * @access private */ function _mapOutAttributes(&$root, $path, $asn1) { $attributes = &$this->_subArray($root, $path); if (is_array($attributes)) { $size = count($attributes); for ($i = 0; $i < $size; $i++) { /* [value] contains the DER encoding of an ASN.1 value corresponding to the attribute type identified by type */ $id = $attributes[$i]['type']; $map = $this->_getMapping($id); if ($map === false) { user_error($id . ' is not a currently supported attribute', E_USER_NOTICE); unset($attributes[$i]); } elseif (is_array($attributes[$i]['value'])) { $values = &$attributes[$i]['value']; for ($j = 0; $j < count($values); $j++) { switch ($id) { case 'pkcs-9-at-extensionRequest': $this->_mapOutExtensions($values, $j, $asn1); break; } if (!is_bool($map)) { $temp = $asn1->encodeDER($values[$j], $map); $decoded = $asn1->decodeBER($temp); $values[$j] = $asn1->asn1map($decoded[0], $this->AttributeValue); } } } } } } /** * Map DN values from ANY type to DN-specific internal * format. * * @param array ref $root * @param string $path * @param object $asn1 * @access private */ function _mapInDNs(&$root, $path, $asn1) { $dns = &$this->_subArray($root, $path); if (is_array($dns)) { for ($i = 0; $i < count($dns); $i++) { for ($j = 0; $j < count($dns[$i]); $j++) { $type = $dns[$i][$j]['type']; $value = &$dns[$i][$j]['value']; if (is_object($value) && $value instanceof Element) { $map = $this->_getMapping($type); if (!is_bool($map)) { $decoded = $asn1->decodeBER($value); $value = $asn1->asn1map($decoded[0], $map); } } } } } } /** * Map DN values from DN-specific internal format to * ANY type. * * @param array ref $root * @param string $path * @param object $asn1 * @access private */ function _mapOutDNs(&$root, $path, $asn1) { $dns = &$this->_subArray($root, $path); if (is_array($dns)) { $size = count($dns); for ($i = 0; $i < $size; $i++) { for ($j = 0; $j < count($dns[$i]); $j++) { $type = $dns[$i][$j]['type']; $value = &$dns[$i][$j]['value']; if (is_object($value) && $value instanceof Element) { continue; } $map = $this->_getMapping($type); if (!is_bool($map)) { $value = new Element($asn1->encodeDER($value, $map)); } } } } } /** * Associate an extension ID to an extension mapping * * @param string $extnId * @access private * @return mixed */ function _getMapping($extnId) { if (!is_string($extnId)) { // eg. if it's a \phpseclib\File\ASN1\Element object return true; } switch ($extnId) { case 'id-ce-keyUsage': return $this->KeyUsage; case 'id-ce-basicConstraints': return $this->BasicConstraints; case 'id-ce-subjectKeyIdentifier': return $this->KeyIdentifier; case 'id-ce-cRLDistributionPoints': return $this->CRLDistributionPoints; case 'id-ce-authorityKeyIdentifier': return $this->AuthorityKeyIdentifier; case 'id-ce-certificatePolicies': return $this->CertificatePolicies; case 'id-ce-extKeyUsage': return $this->ExtKeyUsageSyntax; case 'id-pe-authorityInfoAccess': return $this->AuthorityInfoAccessSyntax; case 'id-pe-subjectInfoAccess': return $this->SubjectInfoAccessSyntax; case 'id-ce-subjectAltName': return $this->SubjectAltName; case 'id-ce-subjectDirectoryAttributes': return $this->SubjectDirectoryAttributes; case 'id-ce-privateKeyUsagePeriod': return $this->PrivateKeyUsagePeriod; case 'id-ce-issuerAltName': return $this->IssuerAltName; case 'id-ce-policyMappings': return $this->PolicyMappings; case 'id-ce-nameConstraints': return $this->NameConstraints; case 'netscape-cert-type': return $this->netscape_cert_type; case 'netscape-comment': return $this->netscape_comment; case 'netscape-ca-policy-url': return $this->netscape_ca_policy_url; // since id-qt-cps isn't a constructed type it will have already been decoded as a string by the time it gets // back around to asn1map() and we don't want it decoded again. //case 'id-qt-cps': // return $this->CPSuri; case 'id-qt-unotice': return $this->UserNotice; // the following OIDs are unsupported but we don't want them to give notices when calling saveX509(). case 'id-pe-logotype': // http://www.ietf.org/rfc/rfc3709.txt case 'entrustVersInfo': // http://support.microsoft.com/kb/287547 case '1.3.6.1.4.1.311.20.2': // szOID_ENROLL_CERTTYPE_EXTENSION case '1.3.6.1.4.1.311.21.1': // szOID_CERTSRV_CA_VERSION // "SET Secure Electronic Transaction Specification" // http://www.maithean.com/docs/set_bk3.pdf case '2.23.42.7.0': // id-set-hashedRootKey // "Certificate Transparency" // https://tools.ietf.org/html/rfc6962 case '1.3.6.1.4.1.11129.2.4.2': // "Qualified Certificate statements" // https://tools.ietf.org/html/rfc3739#section-3.2.6 case '1.3.6.1.5.5.7.1.3': return true; // CSR attributes case 'pkcs-9-at-unstructuredName': return $this->PKCS9String; case 'pkcs-9-at-challengePassword': return $this->DirectoryString; case 'pkcs-9-at-extensionRequest': return $this->Extensions; // CRL extensions. case 'id-ce-cRLNumber': return $this->CRLNumber; case 'id-ce-deltaCRLIndicator': return $this->CRLNumber; case 'id-ce-issuingDistributionPoint': return $this->IssuingDistributionPoint; case 'id-ce-freshestCRL': return $this->CRLDistributionPoints; case 'id-ce-cRLReasons': return $this->CRLReason; case 'id-ce-invalidityDate': return $this->InvalidityDate; case 'id-ce-certificateIssuer': return $this->CertificateIssuer; case 'id-ce-holdInstructionCode': return $this->HoldInstructionCode; case 'id-at-postalAddress': return $this->PostalAddress; } return false; } /** * Load an X.509 certificate as a certificate authority * * @param string $cert * @access public * @return bool */ function loadCA($cert) { $olddn = $this->dn; $oldcert = $this->currentCert; $oldsigsubj = $this->signatureSubject; $oldkeyid = $this->currentKeyIdentifier; $cert = $this->loadX509($cert); if (!$cert) { $this->dn = $olddn; $this->currentCert = $oldcert; $this->signatureSubject = $oldsigsubj; $this->currentKeyIdentifier = $oldkeyid; return false; } /* From RFC5280 "PKIX Certificate and CRL Profile": If the keyUsage extension is present, then the subject public key MUST NOT be used to verify signatures on certificates or CRLs unless the corresponding keyCertSign or cRLSign bit is set. */ //$keyUsage = $this->getExtension('id-ce-keyUsage'); //if ($keyUsage && !in_array('keyCertSign', $keyUsage)) { // return false; //} /* From RFC5280 "PKIX Certificate and CRL Profile": The cA boolean indicates whether the certified public key may be used to verify certificate signatures. If the cA boolean is not asserted, then the keyCertSign bit in the key usage extension MUST NOT be asserted. If the basic constraints extension is not present in a version 3 certificate, or the extension is present but the cA boolean is not asserted, then the certified public key MUST NOT be used to verify certificate signatures. */ //$basicConstraints = $this->getExtension('id-ce-basicConstraints'); //if (!$basicConstraints || !$basicConstraints['cA']) { // return false; //} $this->CAs[] = $cert; $this->dn = $olddn; $this->currentCert = $oldcert; $this->signatureSubject = $oldsigsubj; return true; } /** * Validate an X.509 certificate against a URL * * From RFC2818 "HTTP over TLS": * * Matching is performed using the matching rules specified by * [RFC2459]. If more than one identity of a given type is present in * the certificate (e.g., more than one dNSName name, a match in any one * of the set is considered acceptable.) Names may contain the wildcard * character * which is considered to match any single domain name * component or component fragment. E.g., *.a.com matches foo.a.com but * not bar.foo.a.com. f*.com matches foo.com but not bar.com. * * @param string $url * @access public * @return bool */ function validateURL($url) { if (!is_array($this->currentCert) || !isset($this->currentCert['tbsCertificate'])) { return false; } $components = parse_url($url); if (!isset($components['host'])) { return false; } if ($names = $this->getExtension('id-ce-subjectAltName')) { foreach ($names as $name) { foreach ($name as $key => $value) { $value = str_replace(array('.', '*'), array('\.', '[^.]*'), $value); switch ($key) { case 'dNSName': /* From RFC2818 "HTTP over TLS": If a subjectAltName extension of type dNSName is present, that MUST be used as the identity. Otherwise, the (most specific) Common Name field in the Subject field of the certificate MUST be used. Although the use of the Common Name is existing practice, it is deprecated and Certification Authorities are encouraged to use the dNSName instead. */ if (preg_match('#^' . $value . '$#', $components['host'])) { return true; } break; case 'iPAddress': /* From RFC2818 "HTTP over TLS": In some cases, the URI is specified as an IP address rather than a hostname. In this case, the iPAddress subjectAltName must be present in the certificate and must exactly match the IP in the URI. */ if (preg_match('#(?:\d{1-3}\.){4}#', $components['host'] . '.') && preg_match('#^' . $value . '$#', $components['host'])) { return true; } } } } return false; } if ($value = $this->getDNProp('id-at-commonName')) { $value = str_replace(array('.', '*'), array('\.', '[^.]*'), $value[0]); return preg_match('#^' . $value . '$#', $components['host']); } return false; } /** * Validate a date * * If $date isn't defined it is assumed to be the current date. * * @param \DateTime|string $date optional * @access public */ function validateDate($date = null) { if (!is_array($this->currentCert) || !isset($this->currentCert['tbsCertificate'])) { return false; } if (!isset($date)) { $date = new DateTime(null, new DateTimeZone(@date_default_timezone_get())); } $notBefore = $this->currentCert['tbsCertificate']['validity']['notBefore']; $notBefore = isset($notBefore['generalTime']) ? $notBefore['generalTime'] : $notBefore['utcTime']; $notAfter = $this->currentCert['tbsCertificate']['validity']['notAfter']; $notAfter = isset($notAfter['generalTime']) ? $notAfter['generalTime'] : $notAfter['utcTime']; if (is_string($date)) { $date = new DateTime($date, new DateTimeZone(@date_default_timezone_get())); } $notBefore = new DateTime($notBefore, new DateTimeZone(@date_default_timezone_get())); $notAfter = new DateTime($notAfter, new DateTimeZone(@date_default_timezone_get())); switch (true) { case $date < $notBefore: case $date > $notAfter: return false; } return true; } /** * Fetches a URL * * @param string $url * @access private * @return bool|string */ static function _fetchURL($url) { if (self::$disable_url_fetch) { return false; } $parts = parse_url($url); $data = ''; switch ($parts['scheme']) { case 'http': $fsock = @fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80); if (!$fsock) { return false; } fputs($fsock, "GET $parts[path] HTTP/1.0\r\n"); fputs($fsock, "Host: $parts[host]\r\n\r\n"); $line = fgets($fsock, 1024); if (strlen($line) < 3) { return false; } preg_match('#HTTP/1.\d (\d{3})#', $line, $temp); if ($temp[1] != '200') { return false; } // skip the rest of the headers in the http response while (!feof($fsock) && fgets($fsock, 1024) != "\r\n") { } while (!feof($fsock)) { $data.= fread($fsock, 1024); } break; //case 'ftp': //case 'ldap': //default: } return $data; } /** * Validates an intermediate cert as identified via authority info access extension * * See https://tools.ietf.org/html/rfc4325 for more info * * @param bool $caonly * @param int $count * @access private * @return bool */ function _testForIntermediate($caonly, $count) { $opts = $this->getExtension('id-pe-authorityInfoAccess'); if (!is_array($opts)) { return false; } foreach ($opts as $opt) { if ($opt['accessMethod'] == 'id-ad-caIssuers') { // accessLocation is a GeneralName. GeneralName fields support stuff like email addresses, IP addresses, LDAP, // etc, but we're only supporting URI's. URI's and LDAP are the only thing https://tools.ietf.org/html/rfc4325 // discusses if (isset($opt['accessLocation']['uniformResourceIdentifier'])) { $url = $opt['accessLocation']['uniformResourceIdentifier']; break; } } } if (!isset($url)) { return false; } $cert = static::_fetchURL($url); if (!is_string($cert)) { return false; } $parent = new static(); $parent->CAs = $this->CAs; /* "Conforming applications that support HTTP or FTP for accessing certificates MUST be able to accept .cer files and SHOULD be able to accept .p7c files." -- https://tools.ietf.org/html/rfc4325 A .p7c file is 'a "certs-only" CMS message as specified in RFC 2797" These are currently unsupported */ if (!is_array($parent->loadX509($cert))) { return false; } if (!$parent->_validateSignatureCountable($caonly, ++$count)) { return false; } $this->CAs[] = $parent->currentCert; //$this->loadCA($cert); return true; } /** * Validate a signature * * Works on X.509 certs, CSR's and CRL's. * Returns true if the signature is verified, false if it is not correct or null on error * * By default returns false for self-signed certs. Call validateSignature(false) to make this support * self-signed. * * The behavior of this function is inspired by {@link http://php.net/openssl-verify openssl_verify}. * * @param bool $caonly optional * @access public * @return mixed */ function validateSignature($caonly = true) { return $this->_validateSignatureCountable($caonly, 0); } /** * Validate a signature * * Performs said validation whilst keeping track of how many times validation method is called * * @param bool $caonly * @param int $count * @access private * @return mixed */ function _validateSignatureCountable($caonly, $count) { if (!is_array($this->currentCert) || !isset($this->signatureSubject)) { return null; } if ($count == self::$recur_limit) { return false; } /* TODO: "emailAddress attribute values are not case-sensitive (e.g., "subscriber@example.com" is the same as "SUBSCRIBER@EXAMPLE.COM")." -- http://tools.ietf.org/html/rfc5280#section-4.1.2.6 implement pathLenConstraint in the id-ce-basicConstraints extension */ switch (true) { case isset($this->currentCert['tbsCertificate']): // self-signed cert switch (true) { case !defined('FILE_X509_IGNORE_TYPE') && $this->currentCert['tbsCertificate']['issuer'] === $this->currentCert['tbsCertificate']['subject']: case defined('FILE_X509_IGNORE_TYPE') && $this->getIssuerDN(self::DN_STRING) === $this->getDN(self::DN_STRING): $authorityKey = $this->getExtension('id-ce-authorityKeyIdentifier'); $subjectKeyID = $this->getExtension('id-ce-subjectKeyIdentifier'); switch (true) { case !is_array($authorityKey): case !$subjectKeyID: case isset($authorityKey['keyIdentifier']) && $authorityKey['keyIdentifier'] === $subjectKeyID: $signingCert = $this->currentCert; // working cert } } if (!empty($this->CAs)) { for ($i = 0; $i < count($this->CAs); $i++) { // even if the cert is a self-signed one we still want to see if it's a CA; // if not, we'll conditionally return an error $ca = $this->CAs[$i]; switch (true) { case !defined('FILE_X509_IGNORE_TYPE') && $this->currentCert['tbsCertificate']['issuer'] === $ca['tbsCertificate']['subject']: case defined('FILE_X509_IGNORE_TYPE') && $this->getDN(self::DN_STRING, $this->currentCert['tbsCertificate']['issuer']) === $this->getDN(self::DN_STRING, $ca['tbsCertificate']['subject']): $authorityKey = $this->getExtension('id-ce-authorityKeyIdentifier'); $subjectKeyID = $this->getExtension('id-ce-subjectKeyIdentifier', $ca); switch (true) { case !is_array($authorityKey): case !$subjectKeyID: case isset($authorityKey['keyIdentifier']) && $authorityKey['keyIdentifier'] === $subjectKeyID: if (is_array($authorityKey) && isset($authorityKey['authorityCertSerialNumber']) && !$authorityKey['authorityCertSerialNumber']->equals($ca['tbsCertificate']['serialNumber'])) { break 2; // serial mismatch - check other ca } $signingCert = $ca; // working cert break 3; } } } if (count($this->CAs) == $i && $caonly) { return $this->_testForIntermediate($caonly, $count) && $this->validateSignature($caonly); } } elseif (!isset($signingCert) || $caonly) { return $this->_testForIntermediate($caonly, $count) && $this->validateSignature($caonly); } return $this->_validateSignature( $signingCert['tbsCertificate']['subjectPublicKeyInfo']['algorithm']['algorithm'], $signingCert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'], $this->currentCert['signatureAlgorithm']['algorithm'], substr(base64_decode($this->currentCert['signature']), 1), $this->signatureSubject ); case isset($this->currentCert['certificationRequestInfo']): return $this->_validateSignature( $this->currentCert['certificationRequestInfo']['subjectPKInfo']['algorithm']['algorithm'], $this->currentCert['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey'], $this->currentCert['signatureAlgorithm']['algorithm'], substr(base64_decode($this->currentCert['signature']), 1), $this->signatureSubject ); case isset($this->currentCert['publicKeyAndChallenge']): return $this->_validateSignature( $this->currentCert['publicKeyAndChallenge']['spki']['algorithm']['algorithm'], $this->currentCert['publicKeyAndChallenge']['spki']['subjectPublicKey'], $this->currentCert['signatureAlgorithm']['algorithm'], substr(base64_decode($this->currentCert['signature']), 1), $this->signatureSubject ); case isset($this->currentCert['tbsCertList']): if (!empty($this->CAs)) { for ($i = 0; $i < count($this->CAs); $i++) { $ca = $this->CAs[$i]; switch (true) { case !defined('FILE_X509_IGNORE_TYPE') && $this->currentCert['tbsCertList']['issuer'] === $ca['tbsCertificate']['subject']: case defined('FILE_X509_IGNORE_TYPE') && $this->getDN(self::DN_STRING, $this->currentCert['tbsCertList']['issuer']) === $this->getDN(self::DN_STRING, $ca['tbsCertificate']['subject']): $authorityKey = $this->getExtension('id-ce-authorityKeyIdentifier'); $subjectKeyID = $this->getExtension('id-ce-subjectKeyIdentifier', $ca); switch (true) { case !is_array($authorityKey): case !$subjectKeyID: case isset($authorityKey['keyIdentifier']) && $authorityKey['keyIdentifier'] === $subjectKeyID: if (is_array($authorityKey) && isset($authorityKey['authorityCertSerialNumber']) && !$authorityKey['authorityCertSerialNumber']->equals($ca['tbsCertificate']['serialNumber'])) { break 2; // serial mismatch - check other ca } $signingCert = $ca; // working cert break 3; } } } } if (!isset($signingCert)) { return false; } return $this->_validateSignature( $signingCert['tbsCertificate']['subjectPublicKeyInfo']['algorithm']['algorithm'], $signingCert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'], $this->currentCert['signatureAlgorithm']['algorithm'], substr(base64_decode($this->currentCert['signature']), 1), $this->signatureSubject ); default: return false; } } /** * Validates a signature * * Returns true if the signature is verified, false if it is not correct or null on error * * @param string $publicKeyAlgorithm * @param string $publicKey * @param string $signatureAlgorithm * @param string $signature * @param string $signatureSubject * @access private * @return int */ function _validateSignature($publicKeyAlgorithm, $publicKey, $signatureAlgorithm, $signature, $signatureSubject) { switch ($publicKeyAlgorithm) { case 'rsaEncryption': $rsa = new RSA(); $rsa->loadKey($publicKey); switch ($signatureAlgorithm) { case 'md2WithRSAEncryption': case 'md5WithRSAEncryption': case 'sha1WithRSAEncryption': case 'sha224WithRSAEncryption': case 'sha256WithRSAEncryption': case 'sha384WithRSAEncryption': case 'sha512WithRSAEncryption': $rsa->setHash(preg_replace('#WithRSAEncryption$#', '', $signatureAlgorithm)); $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); if (!@$rsa->verify($signatureSubject, $signature)) { return false; } break; default: return null; } break; default: return null; } return true; } /** * Sets the recursion limit * * When validating a signature it may be necessary to download intermediate certs from URI's. * An intermediate cert that linked to itself would result in an infinite loop so to prevent * that we set a recursion limit. A negative number means that there is no recursion limit. * * @param int $count * @access public */ static function setRecurLimit($count) { self::$recur_limit = $count; } /** * Prevents URIs from being automatically retrieved * * @access public */ static function disableURLFetch() { self::$disable_url_fetch = true; } /** * Allows URIs to be automatically retrieved * * @access public */ static function enableURLFetch() { self::$disable_url_fetch = false; } /** * Reformat public keys * * Reformats a public key to a format supported by phpseclib (if applicable) * * @param string $algorithm * @param string $key * @access private * @return string */ function _reformatKey($algorithm, $key) { switch ($algorithm) { case 'rsaEncryption': return "-----BEGIN RSA PUBLIC KEY-----\r\n" . // subjectPublicKey is stored as a bit string in X.509 certs. the first byte of a bit string represents how many bits // in the last byte should be ignored. the following only supports non-zero stuff but as none of the X.509 certs Firefox // uses as a cert authority actually use a non-zero bit I think it's safe to assume that none do. chunk_split(base64_encode(substr(base64_decode($key), 1)), 64) . '-----END RSA PUBLIC KEY-----'; default: return $key; } } /** * Decodes an IP address * * Takes in a base64 encoded "blob" and returns a human readable IP address * * @param string $ip * @access private * @return string */ function _decodeIP($ip) { return inet_ntop(base64_decode($ip)); } /** * Decodes an IP address in a name constraints extension * * Takes in a base64 encoded "blob" and returns a human readable IP address / mask * * @param string $ip * @access private * @return array */ function _decodeNameConstraintIP($ip) { $ip = base64_decode($ip); $size = strlen($ip) >> 1; $mask = substr($ip, $size); $ip = substr($ip, 0, $size); return array(inet_ntop($ip), inet_ntop($mask)); } /** * Encodes an IP address * * Takes a human readable IP address into a base64-encoded "blob" * * @param string|array $ip * @access private * @return string */ function _encodeIP($ip) { return is_string($ip) ? base64_encode(inet_pton($ip)) : base64_encode(inet_pton($ip[0]) . inet_pton($ip[1])); } /** * "Normalizes" a Distinguished Name property * * @param string $propName * @access private * @return mixed */ function _translateDNProp($propName) { switch (strtolower($propName)) { case 'id-at-countryname': case 'countryname': case 'c': return 'id-at-countryName'; case 'id-at-organizationname': case 'organizationname': case 'o': return 'id-at-organizationName'; case 'id-at-dnqualifier': case 'dnqualifier': return 'id-at-dnQualifier'; case 'id-at-commonname': case 'commonname': case 'cn': return 'id-at-commonName'; case 'id-at-stateorprovincename': case 'stateorprovincename': case 'state': case 'province': case 'provincename': case 'st': return 'id-at-stateOrProvinceName'; case 'id-at-localityname': case 'localityname': case 'l': return 'id-at-localityName'; case 'id-emailaddress': case 'emailaddress': return 'pkcs-9-at-emailAddress'; case 'id-at-serialnumber': case 'serialnumber': return 'id-at-serialNumber'; case 'id-at-postalcode': case 'postalcode': return 'id-at-postalCode'; case 'id-at-streetaddress': case 'streetaddress': return 'id-at-streetAddress'; case 'id-at-name': case 'name': return 'id-at-name'; case 'id-at-givenname': case 'givenname': return 'id-at-givenName'; case 'id-at-surname': case 'surname': case 'sn': return 'id-at-surname'; case 'id-at-initials': case 'initials': return 'id-at-initials'; case 'id-at-generationqualifier': case 'generationqualifier': return 'id-at-generationQualifier'; case 'id-at-organizationalunitname': case 'organizationalunitname': case 'ou': return 'id-at-organizationalUnitName'; case 'id-at-pseudonym': case 'pseudonym': return 'id-at-pseudonym'; case 'id-at-title': case 'title': return 'id-at-title'; case 'id-at-description': case 'description': return 'id-at-description'; case 'id-at-role': case 'role': return 'id-at-role'; case 'id-at-uniqueidentifier': case 'uniqueidentifier': case 'x500uniqueidentifier': return 'id-at-uniqueIdentifier'; case 'postaladdress': case 'id-at-postaladdress': return 'id-at-postalAddress'; default: return false; } } /** * Set a Distinguished Name property * * @param string $propName * @param mixed $propValue * @param string $type optional * @access public * @return bool */ function setDNProp($propName, $propValue, $type = 'utf8String') { if (empty($this->dn)) { $this->dn = array('rdnSequence' => array()); } if (($propName = $this->_translateDNProp($propName)) === false) { return false; } foreach ((array) $propValue as $v) { if (!is_array($v) && isset($type)) { $v = array($type => $v); } $this->dn['rdnSequence'][] = array( array( 'type' => $propName, 'value'=> $v ) ); } return true; } /** * Remove Distinguished Name properties * * @param string $propName * @access public */ function removeDNProp($propName) { if (empty($this->dn)) { return; } if (($propName = $this->_translateDNProp($propName)) === false) { return; } $dn = &$this->dn['rdnSequence']; $size = count($dn); for ($i = 0; $i < $size; $i++) { if ($dn[$i][0]['type'] == $propName) { unset($dn[$i]); } } $dn = array_values($dn); // fix for https://bugs.php.net/75433 affecting PHP 7.2 if (!isset($dn[0])) { $dn = array_splice($dn, 0, 0); } } /** * Get Distinguished Name properties * * @param string $propName * @param array $dn optional * @param bool $withType optional * @return mixed * @access public */ function getDNProp($propName, $dn = null, $withType = false) { if (!isset($dn)) { $dn = $this->dn; } if (empty($dn)) { return false; } if (($propName = $this->_translateDNProp($propName)) === false) { return false; } $asn1 = new ASN1(); $asn1->loadOIDs($this->oids); $filters = array(); $filters['value'] = array('type' => ASN1::TYPE_UTF8_STRING); $asn1->loadFilters($filters); $this->_mapOutDNs($dn, 'rdnSequence', $asn1); $dn = $dn['rdnSequence']; $result = array(); for ($i = 0; $i < count($dn); $i++) { if ($dn[$i][0]['type'] == $propName) { $v = $dn[$i][0]['value']; if (!$withType) { if (is_array($v)) { foreach ($v as $type => $s) { $type = array_search($type, $asn1->ANYmap, true); if ($type !== false && isset($asn1->stringTypeSize[$type])) { $s = $asn1->convert($s, $type); if ($s !== false) { $v = $s; break; } } } if (is_array($v)) { $v = array_pop($v); // Always strip data type. } } elseif (is_object($v) && $v instanceof Element) { $map = $this->_getMapping($propName); if (!is_bool($map)) { $decoded = $asn1->decodeBER($v); $v = $asn1->asn1map($decoded[0], $map); } } } $result[] = $v; } } return $result; } /** * Set a Distinguished Name * * @param mixed $dn * @param bool $merge optional * @param string $type optional * @access public * @return bool */ function setDN($dn, $merge = false, $type = 'utf8String') { if (!$merge) { $this->dn = null; } if (is_array($dn)) { if (isset($dn['rdnSequence'])) { $this->dn = $dn; // No merge here. return true; } // handles stuff generated by openssl_x509_parse() foreach ($dn as $prop => $value) { if (!$this->setDNProp($prop, $value, $type)) { return false; } } return true; } // handles everything else $results = preg_split('#((?:^|, *|/)(?:C=|O=|OU=|CN=|L=|ST=|SN=|postalCode=|streetAddress=|emailAddress=|serialNumber=|organizationalUnitName=|title=|description=|role=|x500UniqueIdentifier=|postalAddress=))#', $dn, -1, PREG_SPLIT_DELIM_CAPTURE); for ($i = 1; $i < count($results); $i+=2) { $prop = trim($results[$i], ', =/'); $value = $results[$i + 1]; if (!$this->setDNProp($prop, $value, $type)) { return false; } } return true; } /** * Get the Distinguished Name for a certificates subject * * @param mixed $format optional * @param array $dn optional * @access public * @return bool */ function getDN($format = self::DN_ARRAY, $dn = null) { if (!isset($dn)) { $dn = isset($this->currentCert['tbsCertList']) ? $this->currentCert['tbsCertList']['issuer'] : $this->dn; } switch ((int) $format) { case self::DN_ARRAY: return $dn; case self::DN_ASN1: $asn1 = new ASN1(); $asn1->loadOIDs($this->oids); $filters = array(); $filters['rdnSequence']['value'] = array('type' => ASN1::TYPE_UTF8_STRING); $asn1->loadFilters($filters); $this->_mapOutDNs($dn, 'rdnSequence', $asn1); return $asn1->encodeDER($dn, $this->Name); case self::DN_CANON: // No SEQUENCE around RDNs and all string values normalized as // trimmed lowercase UTF-8 with all spacing as one blank. // constructed RDNs will not be canonicalized $asn1 = new ASN1(); $asn1->loadOIDs($this->oids); $filters = array(); $filters['value'] = array('type' => ASN1::TYPE_UTF8_STRING); $asn1->loadFilters($filters); $result = ''; $this->_mapOutDNs($dn, 'rdnSequence', $asn1); foreach ($dn['rdnSequence'] as $rdn) { foreach ($rdn as $i => $attr) { $attr = &$rdn[$i]; if (is_array($attr['value'])) { foreach ($attr['value'] as $type => $v) { $type = array_search($type, $asn1->ANYmap, true); if ($type !== false && isset($asn1->stringTypeSize[$type])) { $v = $asn1->convert($v, $type); if ($v !== false) { $v = preg_replace('/\s+/', ' ', $v); $attr['value'] = strtolower(trim($v)); break; } } } } } $result .= $asn1->encodeDER($rdn, $this->RelativeDistinguishedName); } return $result; case self::DN_HASH: $dn = $this->getDN(self::DN_CANON, $dn); $hash = new Hash('sha1'); $hash = $hash->hash($dn); extract(unpack('Vhash', $hash)); return strtolower(bin2hex(pack('N', $hash))); } // Default is to return a string. $start = true; $output = ''; $result = array(); $asn1 = new ASN1(); $asn1->loadOIDs($this->oids); $filters = array(); $filters['rdnSequence']['value'] = array('type' => ASN1::TYPE_UTF8_STRING); $asn1->loadFilters($filters); $this->_mapOutDNs($dn, 'rdnSequence', $asn1); foreach ($dn['rdnSequence'] as $field) { $prop = $field[0]['type']; $value = $field[0]['value']; $delim = ', '; switch ($prop) { case 'id-at-countryName': $desc = 'C'; break; case 'id-at-stateOrProvinceName': $desc = 'ST'; break; case 'id-at-organizationName': $desc = 'O'; break; case 'id-at-organizationalUnitName': $desc = 'OU'; break; case 'id-at-commonName': $desc = 'CN'; break; case 'id-at-localityName': $desc = 'L'; break; case 'id-at-surname': $desc = 'SN'; break; case 'id-at-uniqueIdentifier': $delim = '/'; $desc = 'x500UniqueIdentifier'; break; case 'id-at-postalAddress': $delim = '/'; $desc = 'postalAddress'; break; default: $delim = '/'; $desc = preg_replace('#.+-([^-]+)$#', '$1', $prop); } if (!$start) { $output.= $delim; } if (is_array($value)) { foreach ($value as $type => $v) { $type = array_search($type, $asn1->ANYmap, true); if ($type !== false && isset($asn1->stringTypeSize[$type])) { $v = $asn1->convert($v, $type); if ($v !== false) { $value = $v; break; } } } if (is_array($value)) { $value = array_pop($value); // Always strip data type. } } elseif (is_object($value) && $value instanceof Element) { $callback = function ($x) { return "\x" . bin2hex($x[0]); }; $value = strtoupper(preg_replace_callback('#[^\x20-\x7E]#', $callback, $value->element)); } $output.= $desc . '=' . $value; $result[$desc] = isset($result[$desc]) ? array_merge((array) $result[$desc], array($value)) : $value; $start = false; } return $format == self::DN_OPENSSL ? $result : $output; } /** * Get the Distinguished Name for a certificate/crl issuer * * @param int $format optional * @access public * @return mixed */ function getIssuerDN($format = self::DN_ARRAY) { switch (true) { case !isset($this->currentCert) || !is_array($this->currentCert): break; case isset($this->currentCert['tbsCertificate']): return $this->getDN($format, $this->currentCert['tbsCertificate']['issuer']); case isset($this->currentCert['tbsCertList']): return $this->getDN($format, $this->currentCert['tbsCertList']['issuer']); } return false; } /** * Get the Distinguished Name for a certificate/csr subject * Alias of getDN() * * @param int $format optional * @access public * @return mixed */ function getSubjectDN($format = self::DN_ARRAY) { switch (true) { case !empty($this->dn): return $this->getDN($format); case !isset($this->currentCert) || !is_array($this->currentCert): break; case isset($this->currentCert['tbsCertificate']): return $this->getDN($format, $this->currentCert['tbsCertificate']['subject']); case isset($this->currentCert['certificationRequestInfo']): return $this->getDN($format, $this->currentCert['certificationRequestInfo']['subject']); } return false; } /** * Get an individual Distinguished Name property for a certificate/crl issuer * * @param string $propName * @param bool $withType optional * @access public * @return mixed */ function getIssuerDNProp($propName, $withType = false) { switch (true) { case !isset($this->currentCert) || !is_array($this->currentCert): break; case isset($this->currentCert['tbsCertificate']): return $this->getDNProp($propName, $this->currentCert['tbsCertificate']['issuer'], $withType); case isset($this->currentCert['tbsCertList']): return $this->getDNProp($propName, $this->currentCert['tbsCertList']['issuer'], $withType); } return false; } /** * Get an individual Distinguished Name property for a certificate/csr subject * * @param string $propName * @param bool $withType optional * @access public * @return mixed */ function getSubjectDNProp($propName, $withType = false) { switch (true) { case !empty($this->dn): return $this->getDNProp($propName, null, $withType); case !isset($this->currentCert) || !is_array($this->currentCert): break; case isset($this->currentCert['tbsCertificate']): return $this->getDNProp($propName, $this->currentCert['tbsCertificate']['subject'], $withType); case isset($this->currentCert['certificationRequestInfo']): return $this->getDNProp($propName, $this->currentCert['certificationRequestInfo']['subject'], $withType); } return false; } /** * Get the certificate chain for the current cert * * @access public * @return mixed */ function getChain() { $chain = array($this->currentCert); if (!is_array($this->currentCert) || !isset($this->currentCert['tbsCertificate'])) { return false; } if (empty($this->CAs)) { return $chain; } while (true) { $currentCert = $chain[count($chain) - 1]; for ($i = 0; $i < count($this->CAs); $i++) { $ca = $this->CAs[$i]; if ($currentCert['tbsCertificate']['issuer'] === $ca['tbsCertificate']['subject']) { $authorityKey = $this->getExtension('id-ce-authorityKeyIdentifier', $currentCert); $subjectKeyID = $this->getExtension('id-ce-subjectKeyIdentifier', $ca); switch (true) { case !is_array($authorityKey): case is_array($authorityKey) && isset($authorityKey['keyIdentifier']) && $authorityKey['keyIdentifier'] === $subjectKeyID: if ($currentCert === $ca) { break 3; } $chain[] = $ca; break 2; } } } if ($i == count($this->CAs)) { break; } } foreach ($chain as $key => $value) { $chain[$key] = new X509(); $chain[$key]->loadX509($value); } return $chain; } /** * Set public key * * Key needs to be a \phpseclib\Crypt\RSA object * * @param object $key * @access public * @return bool */ function setPublicKey($key) { $key->setPublicKey(); $this->publicKey = $key; } /** * Set private key * * Key needs to be a \phpseclib\Crypt\RSA object * * @param object $key * @access public */ function setPrivateKey($key) { $this->privateKey = $key; } /** * Set challenge * * Used for SPKAC CSR's * * @param string $challenge * @access public */ function setChallenge($challenge) { $this->challenge = $challenge; } /** * Gets the public key * * Returns a \phpseclib\Crypt\RSA object or a false. * * @access public * @return mixed */ function getPublicKey() { if (isset($this->publicKey)) { return $this->publicKey; } if (isset($this->currentCert) && is_array($this->currentCert)) { foreach (array('tbsCertificate/subjectPublicKeyInfo', 'certificationRequestInfo/subjectPKInfo') as $path) { $keyinfo = $this->_subArray($this->currentCert, $path); if (!empty($keyinfo)) { break; } } } if (empty($keyinfo)) { return false; } $key = $keyinfo['subjectPublicKey']; switch ($keyinfo['algorithm']['algorithm']) { case 'rsaEncryption': $publicKey = new RSA(); $publicKey->loadKey($key); $publicKey->setPublicKey(); break; default: return false; } return $publicKey; } /** * Load a Certificate Signing Request * * @param string $csr * @access public * @return mixed */ function loadCSR($csr, $mode = self::FORMAT_AUTO_DETECT) { if (is_array($csr) && isset($csr['certificationRequestInfo'])) { unset($this->currentCert); unset($this->currentKeyIdentifier); unset($this->signatureSubject); $this->dn = $csr['certificationRequestInfo']['subject']; if (!isset($this->dn)) { return false; } $this->currentCert = $csr; return $csr; } // see http://tools.ietf.org/html/rfc2986 $asn1 = new ASN1(); if ($mode != self::FORMAT_DER) { $newcsr = $this->_extractBER($csr); if ($mode == self::FORMAT_PEM && $csr == $newcsr) { return false; } $csr = $newcsr; } $orig = $csr; if ($csr === false) { $this->currentCert = false; return false; } $asn1->loadOIDs($this->oids); $decoded = $asn1->decodeBER($csr); if (empty($decoded)) { $this->currentCert = false; return false; } $csr = $asn1->asn1map($decoded[0], $this->CertificationRequest); if (!isset($csr) || $csr === false) { $this->currentCert = false; return false; } $this->_mapInAttributes($csr, 'certificationRequestInfo/attributes', $asn1); $this->_mapInDNs($csr, 'certificationRequestInfo/subject/rdnSequence', $asn1); $this->dn = $csr['certificationRequestInfo']['subject']; $this->signatureSubject = substr($orig, $decoded[0]['content'][0]['start'], $decoded[0]['content'][0]['length']); $algorithm = &$csr['certificationRequestInfo']['subjectPKInfo']['algorithm']['algorithm']; $key = &$csr['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey']; $key = $this->_reformatKey($algorithm, $key); switch ($algorithm) { case 'rsaEncryption': $this->publicKey = new RSA(); $this->publicKey->loadKey($key); $this->publicKey->setPublicKey(); break; default: $this->publicKey = null; } $this->currentKeyIdentifier = null; $this->currentCert = $csr; return $csr; } /** * Save CSR request * * @param array $csr * @param int $format optional * @access public * @return string */ function saveCSR($csr, $format = self::FORMAT_PEM) { if (!is_array($csr) || !isset($csr['certificationRequestInfo'])) { return false; } switch (true) { case !($algorithm = $this->_subArray($csr, 'certificationRequestInfo/subjectPKInfo/algorithm/algorithm')): case is_object($csr['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey']): break; default: switch ($algorithm) { case 'rsaEncryption': $csr['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey'] = base64_encode("\0" . base64_decode(preg_replace('#-.+-|[\r\n]#', '', $csr['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey']))); $csr['certificationRequestInfo']['subjectPKInfo']['algorithm']['parameters'] = null; $csr['signatureAlgorithm']['parameters'] = null; $csr['certificationRequestInfo']['signature']['parameters'] = null; } } $asn1 = new ASN1(); $asn1->loadOIDs($this->oids); $filters = array(); $filters['certificationRequestInfo']['subject']['rdnSequence']['value'] = array('type' => ASN1::TYPE_UTF8_STRING); $asn1->loadFilters($filters); $this->_mapOutDNs($csr, 'certificationRequestInfo/subject/rdnSequence', $asn1); $this->_mapOutAttributes($csr, 'certificationRequestInfo/attributes', $asn1); $csr = $asn1->encodeDER($csr, $this->CertificationRequest); switch ($format) { case self::FORMAT_DER: return $csr; // case self::FORMAT_PEM: default: return "-----BEGIN CERTIFICATE REQUEST-----\r\n" . chunk_split(base64_encode($csr), 64) . '-----END CERTIFICATE REQUEST-----'; } } /** * Load a SPKAC CSR * * SPKAC's are produced by the HTML5 keygen element: * * https://developer.mozilla.org/en-US/docs/HTML/Element/keygen * * @param string $csr * @access public * @return mixed */ function loadSPKAC($spkac) { if (is_array($spkac) && isset($spkac['publicKeyAndChallenge'])) { unset($this->currentCert); unset($this->currentKeyIdentifier); unset($this->signatureSubject); $this->currentCert = $spkac; return $spkac; } // see http://www.w3.org/html/wg/drafts/html/master/forms.html#signedpublickeyandchallenge $asn1 = new ASN1(); // OpenSSL produces SPKAC's that are preceded by the string SPKAC= $temp = preg_replace('#(?:SPKAC=)|[ \r\n\\\]#', '', $spkac); $temp = preg_match('#^[a-zA-Z\d/+]*={0,2}$#', $temp) ? base64_decode($temp) : false; if ($temp != false) { $spkac = $temp; } $orig = $spkac; if ($spkac === false) { $this->currentCert = false; return false; } $asn1->loadOIDs($this->oids); $decoded = $asn1->decodeBER($spkac); if (empty($decoded)) { $this->currentCert = false; return false; } $spkac = $asn1->asn1map($decoded[0], $this->SignedPublicKeyAndChallenge); if (!isset($spkac) || $spkac === false) { $this->currentCert = false; return false; } $this->signatureSubject = substr($orig, $decoded[0]['content'][0]['start'], $decoded[0]['content'][0]['length']); $algorithm = &$spkac['publicKeyAndChallenge']['spki']['algorithm']['algorithm']; $key = &$spkac['publicKeyAndChallenge']['spki']['subjectPublicKey']; $key = $this->_reformatKey($algorithm, $key); switch ($algorithm) { case 'rsaEncryption': $this->publicKey = new RSA(); $this->publicKey->loadKey($key); $this->publicKey->setPublicKey(); break; default: $this->publicKey = null; } $this->currentKeyIdentifier = null; $this->currentCert = $spkac; return $spkac; } /** * Save a SPKAC CSR request * * @param array $csr * @param int $format optional * @access public * @return string */ function saveSPKAC($spkac, $format = self::FORMAT_PEM) { if (!is_array($spkac) || !isset($spkac['publicKeyAndChallenge'])) { return false; } $algorithm = $this->_subArray($spkac, 'publicKeyAndChallenge/spki/algorithm/algorithm'); switch (true) { case !$algorithm: case is_object($spkac['publicKeyAndChallenge']['spki']['subjectPublicKey']): break; default: switch ($algorithm) { case 'rsaEncryption': $spkac['publicKeyAndChallenge']['spki']['subjectPublicKey'] = base64_encode("\0" . base64_decode(preg_replace('#-.+-|[\r\n]#', '', $spkac['publicKeyAndChallenge']['spki']['subjectPublicKey']))); } } $asn1 = new ASN1(); $asn1->loadOIDs($this->oids); $spkac = $asn1->encodeDER($spkac, $this->SignedPublicKeyAndChallenge); switch ($format) { case self::FORMAT_DER: return $spkac; // case self::FORMAT_PEM: default: // OpenSSL's implementation of SPKAC requires the SPKAC be preceded by SPKAC= and since there are pretty much // no other SPKAC decoders phpseclib will use that same format return 'SPKAC=' . base64_encode($spkac); } } /** * Load a Certificate Revocation List * * @param string $crl * @access public * @return mixed */ function loadCRL($crl, $mode = self::FORMAT_AUTO_DETECT) { if (is_array($crl) && isset($crl['tbsCertList'])) { $this->currentCert = $crl; unset($this->signatureSubject); return $crl; } $asn1 = new ASN1(); if ($mode != self::FORMAT_DER) { $newcrl = $this->_extractBER($crl); if ($mode == self::FORMAT_PEM && $crl == $newcrl) { return false; } $crl = $newcrl; } $orig = $crl; if ($crl === false) { $this->currentCert = false; return false; } $asn1->loadOIDs($this->oids); $decoded = $asn1->decodeBER($crl); if (empty($decoded)) { $this->currentCert = false; return false; } $crl = $asn1->asn1map($decoded[0], $this->CertificateList); if (!isset($crl) || $crl === false) { $this->currentCert = false; return false; } $this->signatureSubject = substr($orig, $decoded[0]['content'][0]['start'], $decoded[0]['content'][0]['length']); $this->_mapInDNs($crl, 'tbsCertList/issuer/rdnSequence', $asn1); if ($this->_isSubArrayValid($crl, 'tbsCertList/crlExtensions')) { $this->_mapInExtensions($crl, 'tbsCertList/crlExtensions', $asn1); } if ($this->_isSubArrayValid($crl, 'tbsCertList/revokedCertificates')) { $rclist_ref = &$this->_subArrayUnchecked($crl, 'tbsCertList/revokedCertificates'); if ($rclist_ref) { $rclist = $crl['tbsCertList']['revokedCertificates']; foreach ($rclist as $i => $extension) { if ($this->_isSubArrayValid($rclist, "$i/crlEntryExtensions", $asn1)) { $this->_mapInExtensions($rclist_ref, "$i/crlEntryExtensions", $asn1); } } } } $this->currentKeyIdentifier = null; $this->currentCert = $crl; return $crl; } /** * Save Certificate Revocation List. * * @param array $crl * @param int $format optional * @access public * @return string */ function saveCRL($crl, $format = self::FORMAT_PEM) { if (!is_array($crl) || !isset($crl['tbsCertList'])) { return false; } $asn1 = new ASN1(); $asn1->loadOIDs($this->oids); $filters = array(); $filters['tbsCertList']['issuer']['rdnSequence']['value'] = array('type' => ASN1::TYPE_UTF8_STRING); $filters['tbsCertList']['signature']['parameters'] = array('type' => ASN1::TYPE_UTF8_STRING); $filters['signatureAlgorithm']['parameters'] = array('type' => ASN1::TYPE_UTF8_STRING); if (empty($crl['tbsCertList']['signature']['parameters'])) { $filters['tbsCertList']['signature']['parameters'] = array('type' => ASN1::TYPE_NULL); } if (empty($crl['signatureAlgorithm']['parameters'])) { $filters['signatureAlgorithm']['parameters'] = array('type' => ASN1::TYPE_NULL); } $asn1->loadFilters($filters); $this->_mapOutDNs($crl, 'tbsCertList/issuer/rdnSequence', $asn1); $this->_mapOutExtensions($crl, 'tbsCertList/crlExtensions', $asn1); $rclist = &$this->_subArray($crl, 'tbsCertList/revokedCertificates'); if (is_array($rclist)) { foreach ($rclist as $i => $extension) { $this->_mapOutExtensions($rclist, "$i/crlEntryExtensions", $asn1); } } $crl = $asn1->encodeDER($crl, $this->CertificateList); switch ($format) { case self::FORMAT_DER: return $crl; // case self::FORMAT_PEM: default: return "-----BEGIN X509 CRL-----\r\n" . chunk_split(base64_encode($crl), 64) . '-----END X509 CRL-----'; } } /** * Helper function to build a time field according to RFC 3280 section * - 4.1.2.5 Validity * - 5.1.2.4 This Update * - 5.1.2.5 Next Update * - 5.1.2.6 Revoked Certificates * by choosing utcTime iff year of date given is before 2050 and generalTime else. * * @param string $date in format date('D, d M Y H:i:s O') * @access private * @return array */ function _timeField($date) { if ($date instanceof Element) { return $date; } $dateObj = new DateTime($date, new DateTimeZone('GMT')); $year = $dateObj->format('Y'); // the same way ASN1.php parses this if ($year < 2050) { return array('utcTime' => $date); } else { return array('generalTime' => $date); } } /** * Sign an X.509 certificate * * $issuer's private key needs to be loaded. * $subject can be either an existing X.509 cert (if you want to resign it), * a CSR or something with the DN and public key explicitly set. * * @param \phpseclib\File\X509 $issuer * @param \phpseclib\File\X509 $subject * @param string $signatureAlgorithm optional * @access public * @return mixed */ function sign($issuer, $subject, $signatureAlgorithm = 'sha1WithRSAEncryption') { if (!is_object($issuer->privateKey) || empty($issuer->dn)) { return false; } if (isset($subject->publicKey) && !($subjectPublicKey = $subject->_formatSubjectPublicKey())) { return false; } $currentCert = isset($this->currentCert) ? $this->currentCert : null; $signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject: null; if (isset($subject->currentCert) && is_array($subject->currentCert) && isset($subject->currentCert['tbsCertificate'])) { $this->currentCert = $subject->currentCert; $this->currentCert['tbsCertificate']['signature']['algorithm'] = $signatureAlgorithm; $this->currentCert['signatureAlgorithm']['algorithm'] = $signatureAlgorithm; if (!empty($this->startDate)) { $this->currentCert['tbsCertificate']['validity']['notBefore'] = $this->_timeField($this->startDate); } if (!empty($this->endDate)) { $this->currentCert['tbsCertificate']['validity']['notAfter'] = $this->_timeField($this->endDate); } if (!empty($this->serialNumber)) { $this->currentCert['tbsCertificate']['serialNumber'] = $this->serialNumber; } if (!empty($subject->dn)) { $this->currentCert['tbsCertificate']['subject'] = $subject->dn; } if (!empty($subject->publicKey)) { $this->currentCert['tbsCertificate']['subjectPublicKeyInfo'] = $subjectPublicKey; } $this->removeExtension('id-ce-authorityKeyIdentifier'); if (isset($subject->domains)) { $this->removeExtension('id-ce-subjectAltName'); } } elseif (isset($subject->currentCert) && is_array($subject->currentCert) && isset($subject->currentCert['tbsCertList'])) { return false; } else { if (!isset($subject->publicKey)) { return false; } $startDate = new DateTime('now', new DateTimeZone(@date_default_timezone_get())); $startDate = !empty($this->startDate) ? $this->startDate : $startDate->format('D, d M Y H:i:s O'); $endDate = new DateTime('+1 year', new DateTimeZone(@date_default_timezone_get())); $endDate = !empty($this->endDate) ? $this->endDate : $endDate->format('D, d M Y H:i:s O'); /* "The serial number MUST be a positive integer" "Conforming CAs MUST NOT use serialNumber values longer than 20 octets." -- https://tools.ietf.org/html/rfc5280#section-4.1.2.2 for the integer to be positive the leading bit needs to be 0 hence the application of a bitmap */ $serialNumber = !empty($this->serialNumber) ? $this->serialNumber : new BigInteger(Random::string(20) & ("\x7F" . str_repeat("\xFF", 19)), 256); $this->currentCert = array( 'tbsCertificate' => array( 'version' => 'v3', 'serialNumber' => $serialNumber, // $this->setSerialNumber() 'signature' => array('algorithm' => $signatureAlgorithm), 'issuer' => false, // this is going to be overwritten later 'validity' => array( 'notBefore' => $this->_timeField($startDate), // $this->setStartDate() 'notAfter' => $this->_timeField($endDate) // $this->setEndDate() ), 'subject' => $subject->dn, 'subjectPublicKeyInfo' => $subjectPublicKey ), 'signatureAlgorithm' => array('algorithm' => $signatureAlgorithm), 'signature' => false // this is going to be overwritten later ); // Copy extensions from CSR. $csrexts = $subject->getAttribute('pkcs-9-at-extensionRequest', 0); if (!empty($csrexts)) { $this->currentCert['tbsCertificate']['extensions'] = $csrexts; } } $this->currentCert['tbsCertificate']['issuer'] = $issuer->dn; if (isset($issuer->currentKeyIdentifier)) { $this->setExtension('id-ce-authorityKeyIdentifier', array( //'authorityCertIssuer' => array( // array( // 'directoryName' => $issuer->dn // ) //), 'keyIdentifier' => $issuer->currentKeyIdentifier )); //$extensions = &$this->currentCert['tbsCertificate']['extensions']; //if (isset($issuer->serialNumber)) { // $extensions[count($extensions) - 1]['authorityCertSerialNumber'] = $issuer->serialNumber; //} //unset($extensions); } if (isset($subject->currentKeyIdentifier)) { $this->setExtension('id-ce-subjectKeyIdentifier', $subject->currentKeyIdentifier); } $altName = array(); if (isset($subject->domains) && count($subject->domains)) { $altName = array_map(array('\phpseclib\File\X509', '_dnsName'), $subject->domains); } if (isset($subject->ipAddresses) && count($subject->ipAddresses)) { // should an IP address appear as the CN if no domain name is specified? idk //$ips = count($subject->domains) ? $subject->ipAddresses : array_slice($subject->ipAddresses, 1); $ipAddresses = array(); foreach ($subject->ipAddresses as $ipAddress) { $encoded = $subject->_ipAddress($ipAddress); if ($encoded !== false) { $ipAddresses[] = $encoded; } } if (count($ipAddresses)) { $altName = array_merge($altName, $ipAddresses); } } if (!empty($altName)) { $this->setExtension('id-ce-subjectAltName', $altName); } if ($this->caFlag) { $keyUsage = $this->getExtension('id-ce-keyUsage'); if (!$keyUsage) { $keyUsage = array(); } $this->setExtension( 'id-ce-keyUsage', array_values(array_unique(array_merge($keyUsage, array('cRLSign', 'keyCertSign')))) ); $basicConstraints = $this->getExtension('id-ce-basicConstraints'); if (!$basicConstraints) { $basicConstraints = array(); } $this->setExtension( 'id-ce-basicConstraints', array_unique(array_merge(array('cA' => true), $basicConstraints)), true ); if (!isset($subject->currentKeyIdentifier)) { $this->setExtension('id-ce-subjectKeyIdentifier', base64_encode($this->computeKeyIdentifier($this->currentCert)), false, false); } } // resync $this->signatureSubject // save $tbsCertificate in case there are any \phpseclib\File\ASN1\Element objects in it $tbsCertificate = $this->currentCert['tbsCertificate']; $this->loadX509($this->saveX509($this->currentCert)); $result = $this->_sign($issuer->privateKey, $signatureAlgorithm); $result['tbsCertificate'] = $tbsCertificate; $this->currentCert = $currentCert; $this->signatureSubject = $signatureSubject; return $result; } /** * Sign a CSR * * @access public * @return mixed */ function signCSR($signatureAlgorithm = 'sha1WithRSAEncryption') { if (!is_object($this->privateKey) || empty($this->dn)) { return false; } $origPublicKey = $this->publicKey; $class = get_class($this->privateKey); $this->publicKey = new $class(); $this->publicKey->loadKey($this->privateKey->getPublicKey()); $this->publicKey->setPublicKey(); if (!($publicKey = $this->_formatSubjectPublicKey())) { return false; } $this->publicKey = $origPublicKey; $currentCert = isset($this->currentCert) ? $this->currentCert : null; $signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject: null; if (isset($this->currentCert) && is_array($this->currentCert) && isset($this->currentCert['certificationRequestInfo'])) { $this->currentCert['signatureAlgorithm']['algorithm'] = $signatureAlgorithm; if (!empty($this->dn)) { $this->currentCert['certificationRequestInfo']['subject'] = $this->dn; } $this->currentCert['certificationRequestInfo']['subjectPKInfo'] = $publicKey; } else { $this->currentCert = array( 'certificationRequestInfo' => array( 'version' => 'v1', 'subject' => $this->dn, 'subjectPKInfo' => $publicKey ), 'signatureAlgorithm' => array('algorithm' => $signatureAlgorithm), 'signature' => false // this is going to be overwritten later ); } // resync $this->signatureSubject // save $certificationRequestInfo in case there are any \phpseclib\File\ASN1\Element objects in it $certificationRequestInfo = $this->currentCert['certificationRequestInfo']; $this->loadCSR($this->saveCSR($this->currentCert)); $result = $this->_sign($this->privateKey, $signatureAlgorithm); $result['certificationRequestInfo'] = $certificationRequestInfo; $this->currentCert = $currentCert; $this->signatureSubject = $signatureSubject; return $result; } /** * Sign a SPKAC * * @access public * @return mixed */ function signSPKAC($signatureAlgorithm = 'sha1WithRSAEncryption') { if (!is_object($this->privateKey)) { return false; } $origPublicKey = $this->publicKey; $class = get_class($this->privateKey); $this->publicKey = new $class(); $this->publicKey->loadKey($this->privateKey->getPublicKey()); $this->publicKey->setPublicKey(); $publicKey = $this->_formatSubjectPublicKey(); if (!$publicKey) { return false; } $this->publicKey = $origPublicKey; $currentCert = isset($this->currentCert) ? $this->currentCert : null; $signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject: null; // re-signing a SPKAC seems silly but since everything else supports re-signing why not? if (isset($this->currentCert) && is_array($this->currentCert) && isset($this->currentCert['publicKeyAndChallenge'])) { $this->currentCert['signatureAlgorithm']['algorithm'] = $signatureAlgorithm; $this->currentCert['publicKeyAndChallenge']['spki'] = $publicKey; if (!empty($this->challenge)) { // the bitwise AND ensures that the output is a valid IA5String $this->currentCert['publicKeyAndChallenge']['challenge'] = $this->challenge & str_repeat("\x7F", strlen($this->challenge)); } } else { $this->currentCert = array( 'publicKeyAndChallenge' => array( 'spki' => $publicKey, // quoting <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/keygen>, // "A challenge string that is submitted along with the public key. Defaults to an empty string if not specified." // both Firefox and OpenSSL ("openssl spkac -key private.key") behave this way // we could alternatively do this instead if we ignored the specs: // Random::string(8) & str_repeat("\x7F", 8) 'challenge' => !empty($this->challenge) ? $this->challenge : '' ), 'signatureAlgorithm' => array('algorithm' => $signatureAlgorithm), 'signature' => false // this is going to be overwritten later ); } // resync $this->signatureSubject // save $publicKeyAndChallenge in case there are any \phpseclib\File\ASN1\Element objects in it $publicKeyAndChallenge = $this->currentCert['publicKeyAndChallenge']; $this->loadSPKAC($this->saveSPKAC($this->currentCert)); $result = $this->_sign($this->privateKey, $signatureAlgorithm); $result['publicKeyAndChallenge'] = $publicKeyAndChallenge; $this->currentCert = $currentCert; $this->signatureSubject = $signatureSubject; return $result; } /** * Sign a CRL * * $issuer's private key needs to be loaded. * * @param \phpseclib\File\X509 $issuer * @param \phpseclib\File\X509 $crl * @param string $signatureAlgorithm optional * @access public * @return mixed */ function signCRL($issuer, $crl, $signatureAlgorithm = 'sha1WithRSAEncryption') { if (!is_object($issuer->privateKey) || empty($issuer->dn)) { return false; } $currentCert = isset($this->currentCert) ? $this->currentCert : null; $signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject : null; $thisUpdate = new DateTime('now', new DateTimeZone(@date_default_timezone_get())); $thisUpdate = !empty($this->startDate) ? $this->startDate : $thisUpdate->format('D, d M Y H:i:s O'); if (isset($crl->currentCert) && is_array($crl->currentCert) && isset($crl->currentCert['tbsCertList'])) { $this->currentCert = $crl->currentCert; $this->currentCert['tbsCertList']['signature']['algorithm'] = $signatureAlgorithm; $this->currentCert['signatureAlgorithm']['algorithm'] = $signatureAlgorithm; } else { $this->currentCert = array( 'tbsCertList' => array( 'version' => 'v2', 'signature' => array('algorithm' => $signatureAlgorithm), 'issuer' => false, // this is going to be overwritten later 'thisUpdate' => $this->_timeField($thisUpdate) // $this->setStartDate() ), 'signatureAlgorithm' => array('algorithm' => $signatureAlgorithm), 'signature' => false // this is going to be overwritten later ); } $tbsCertList = &$this->currentCert['tbsCertList']; $tbsCertList['issuer'] = $issuer->dn; $tbsCertList['thisUpdate'] = $this->_timeField($thisUpdate); if (!empty($this->endDate)) { $tbsCertList['nextUpdate'] = $this->_timeField($this->endDate); // $this->setEndDate() } else { unset($tbsCertList['nextUpdate']); } if (!empty($this->serialNumber)) { $crlNumber = $this->serialNumber; } else { $crlNumber = $this->getExtension('id-ce-cRLNumber'); // "The CRL number is a non-critical CRL extension that conveys a // monotonically increasing sequence number for a given CRL scope and // CRL issuer. This extension allows users to easily determine when a // particular CRL supersedes another CRL." // -- https://tools.ietf.org/html/rfc5280#section-5.2.3 $crlNumber = $crlNumber !== false ? $crlNumber->add(new BigInteger(1)) : null; } $this->removeExtension('id-ce-authorityKeyIdentifier'); $this->removeExtension('id-ce-issuerAltName'); // Be sure version >= v2 if some extension found. $version = isset($tbsCertList['version']) ? $tbsCertList['version'] : 0; if (!$version) { if (!empty($tbsCertList['crlExtensions'])) { $version = 1; // v2. } elseif (!empty($tbsCertList['revokedCertificates'])) { foreach ($tbsCertList['revokedCertificates'] as $cert) { if (!empty($cert['crlEntryExtensions'])) { $version = 1; // v2. } } } if ($version) { $tbsCertList['version'] = $version; } } // Store additional extensions. if (!empty($tbsCertList['version'])) { // At least v2. if (!empty($crlNumber)) { $this->setExtension('id-ce-cRLNumber', $crlNumber); } if (isset($issuer->currentKeyIdentifier)) { $this->setExtension('id-ce-authorityKeyIdentifier', array( //'authorityCertIssuer' => array( // array( // 'directoryName' => $issuer->dn // ) //), 'keyIdentifier' => $issuer->currentKeyIdentifier )); //$extensions = &$tbsCertList['crlExtensions']; //if (isset($issuer->serialNumber)) { // $extensions[count($extensions) - 1]['authorityCertSerialNumber'] = $issuer->serialNumber; //} //unset($extensions); } $issuerAltName = $this->getExtension('id-ce-subjectAltName', $issuer->currentCert); if ($issuerAltName !== false) { $this->setExtension('id-ce-issuerAltName', $issuerAltName); } } if (empty($tbsCertList['revokedCertificates'])) { unset($tbsCertList['revokedCertificates']); } unset($tbsCertList); // resync $this->signatureSubject // save $tbsCertList in case there are any \phpseclib\File\ASN1\Element objects in it $tbsCertList = $this->currentCert['tbsCertList']; $this->loadCRL($this->saveCRL($this->currentCert)); $result = $this->_sign($issuer->privateKey, $signatureAlgorithm); $result['tbsCertList'] = $tbsCertList; $this->currentCert = $currentCert; $this->signatureSubject = $signatureSubject; return $result; } /** * X.509 certificate signing helper function. * * @param object $key * @param \phpseclib\File\X509 $subject * @param string $signatureAlgorithm * @access public * @return mixed */ function _sign($key, $signatureAlgorithm) { if ($key instanceof RSA) { switch ($signatureAlgorithm) { case 'md2WithRSAEncryption': case 'md5WithRSAEncryption': case 'sha1WithRSAEncryption': case 'sha224WithRSAEncryption': case 'sha256WithRSAEncryption': case 'sha384WithRSAEncryption': case 'sha512WithRSAEncryption': $key->setHash(preg_replace('#WithRSAEncryption$#', '', $signatureAlgorithm)); $key->setSignatureMode(RSA::SIGNATURE_PKCS1); $this->currentCert['signature'] = base64_encode("\0" . $key->sign($this->signatureSubject)); return $this->currentCert; } } return false; } /** * Set certificate start date * * @param string $date * @access public */ function setStartDate($date) { if (!is_object($date) || !is_a($date, 'DateTime')) { $date = new DateTime($date, new DateTimeZone(@date_default_timezone_get())); } $this->startDate = $date->format('D, d M Y H:i:s O'); } /** * Set certificate end date * * @param string $date * @access public */ function setEndDate($date) { /* To indicate that a certificate has no well-defined expiration date, the notAfter SHOULD be assigned the GeneralizedTime value of 99991231235959Z. -- http://tools.ietf.org/html/rfc5280#section-4.1.2.5 */ if (strtolower($date) == 'lifetime') { $temp = '99991231235959Z'; $asn1 = new ASN1(); $temp = chr(ASN1::TYPE_GENERALIZED_TIME) . $asn1->_encodeLength(strlen($temp)) . $temp; $this->endDate = new Element($temp); } else { if (!is_object($date) || !is_a($date, 'DateTime')) { $date = new DateTime($date, new DateTimeZone(@date_default_timezone_get())); } $this->endDate = $date->format('D, d M Y H:i:s O'); } } /** * Set Serial Number * * @param string $serial * @param $base optional * @access public */ function setSerialNumber($serial, $base = -256) { $this->serialNumber = new BigInteger($serial, $base); } /** * Turns the certificate into a certificate authority * * @access public */ function makeCA() { $this->caFlag = true; } /** * Check for validity of subarray * * This is intended for use in conjunction with _subArrayUnchecked(), * implementing the checks included in _subArray() but without copying * a potentially large array by passing its reference by-value to is_array(). * * @param array $root * @param string $path * @return boolean * @access private */ function _isSubArrayValid($root, $path) { if (!is_array($root)) { return false; } foreach (explode('/', $path) as $i) { if (!is_array($root)) { return false; } if (!isset($root[$i])) { return true; } $root = $root[$i]; } return true; } /** * Get a reference to a subarray * * This variant of _subArray() does no is_array() checking, * so $root should be checked with _isSubArrayValid() first. * * This is here for performance reasons: * Passing a reference (i.e. $root) by-value (i.e. to is_array()) * creates a copy. If $root is an especially large array, this is expensive. * * @param array $root * @param string $path absolute path with / as component separator * @param bool $create optional * @access private * @return array|false */ function &_subArrayUnchecked(&$root, $path, $create = false) { $false = false; foreach (explode('/', $path) as $i) { if (!isset($root[$i])) { if (!$create) { return $false; } $root[$i] = array(); } $root = &$root[$i]; } return $root; } /** * Get a reference to a subarray * * @param array $root * @param string $path absolute path with / as component separator * @param bool $create optional * @access private * @return array|false */ function &_subArray(&$root, $path, $create = false) { $false = false; if (!is_array($root)) { return $false; } foreach (explode('/', $path) as $i) { if (!is_array($root)) { return $false; } if (!isset($root[$i])) { if (!$create) { return $false; } $root[$i] = array(); } $root = &$root[$i]; } return $root; } /** * Get a reference to an extension subarray * * @param array $root * @param string $path optional absolute path with / as component separator * @param bool $create optional * @access private * @return array|false */ function &_extensions(&$root, $path = null, $create = false) { if (!isset($root)) { $root = $this->currentCert; } switch (true) { case !empty($path): case !is_array($root): break; case isset($root['tbsCertificate']): $path = 'tbsCertificate/extensions'; break; case isset($root['tbsCertList']): $path = 'tbsCertList/crlExtensions'; break; case isset($root['certificationRequestInfo']): $pth = 'certificationRequestInfo/attributes'; $attributes = &$this->_subArray($root, $pth, $create); if (is_array($attributes)) { foreach ($attributes as $key => $value) { if ($value['type'] == 'pkcs-9-at-extensionRequest') { $path = "$pth/$key/value/0"; break 2; } } if ($create) { $key = count($attributes); $attributes[] = array('type' => 'pkcs-9-at-extensionRequest', 'value' => array()); $path = "$pth/$key/value/0"; } } break; } $extensions = &$this->_subArray($root, $path, $create); if (!is_array($extensions)) { $false = false; return $false; } return $extensions; } /** * Remove an Extension * * @param string $id * @param string $path optional * @access private * @return bool */ function _removeExtension($id, $path = null) { $extensions = &$this->_extensions($this->currentCert, $path); if (!is_array($extensions)) { return false; } $result = false; foreach ($extensions as $key => $value) { if ($value['extnId'] == $id) { unset($extensions[$key]); $result = true; } } $extensions = array_values($extensions); // fix for https://bugs.php.net/75433 affecting PHP 7.2 if (!isset($extensions[0])) { $extensions = array_splice($extensions, 0, 0); } return $result; } /** * Get an Extension * * Returns the extension if it exists and false if not * * @param string $id * @param array $cert optional * @param string $path optional * @access private * @return mixed */ function _getExtension($id, $cert = null, $path = null) { $extensions = $this->_extensions($cert, $path); if (!is_array($extensions)) { return false; } foreach ($extensions as $key => $value) { if ($value['extnId'] == $id) { return $value['extnValue']; } } return false; } /** * Returns a list of all extensions in use * * @param array $cert optional * @param string $path optional * @access private * @return array */ function _getExtensions($cert = null, $path = null) { $exts = $this->_extensions($cert, $path); $extensions = array(); if (is_array($exts)) { foreach ($exts as $extension) { $extensions[] = $extension['extnId']; } } return $extensions; } /** * Set an Extension * * @param string $id * @param mixed $value * @param bool $critical optional * @param bool $replace optional * @param string $path optional * @access private * @return bool */ function _setExtension($id, $value, $critical = false, $replace = true, $path = null) { $extensions = &$this->_extensions($this->currentCert, $path, true); if (!is_array($extensions)) { return false; } $newext = array('extnId' => $id, 'critical' => $critical, 'extnValue' => $value); foreach ($extensions as $key => $value) { if ($value['extnId'] == $id) { if (!$replace) { return false; } $extensions[$key] = $newext; return true; } } $extensions[] = $newext; return true; } /** * Remove a certificate, CSR or CRL Extension * * @param string $id * @access public * @return bool */ function removeExtension($id) { return $this->_removeExtension($id); } /** * Get a certificate, CSR or CRL Extension * * Returns the extension if it exists and false if not * * @param string $id * @param array $cert optional * @access public * @return mixed */ function getExtension($id, $cert = null) { return $this->_getExtension($id, $cert); } /** * Returns a list of all extensions in use in certificate, CSR or CRL * * @param array $cert optional * @access public * @return array */ function getExtensions($cert = null) { return $this->_getExtensions($cert); } /** * Set a certificate, CSR or CRL Extension * * @param string $id * @param mixed $value * @param bool $critical optional * @param bool $replace optional * @access public * @return bool */ function setExtension($id, $value, $critical = false, $replace = true) { return $this->_setExtension($id, $value, $critical, $replace); } /** * Remove a CSR attribute. * * @param string $id * @param int $disposition optional * @access public * @return bool */ function removeAttribute($id, $disposition = self::ATTR_ALL) { $attributes = &$this->_subArray($this->currentCert, 'certificationRequestInfo/attributes'); if (!is_array($attributes)) { return false; } $result = false; foreach ($attributes as $key => $attribute) { if ($attribute['type'] == $id) { $n = count($attribute['value']); switch (true) { case $disposition == self::ATTR_APPEND: case $disposition == self::ATTR_REPLACE: return false; case $disposition >= $n: $disposition -= $n; break; case $disposition == self::ATTR_ALL: case $n == 1: unset($attributes[$key]); $result = true; break; default: unset($attributes[$key]['value'][$disposition]); $attributes[$key]['value'] = array_values($attributes[$key]['value']); $result = true; break; } if ($result && $disposition != self::ATTR_ALL) { break; } } } $attributes = array_values($attributes); return $result; } /** * Get a CSR attribute * * Returns the attribute if it exists and false if not * * @param string $id * @param int $disposition optional * @param array $csr optional * @access public * @return mixed */ function getAttribute($id, $disposition = self::ATTR_ALL, $csr = null) { if (empty($csr)) { $csr = $this->currentCert; } $attributes = $this->_subArray($csr, 'certificationRequestInfo/attributes'); if (!is_array($attributes)) { return false; } foreach ($attributes as $key => $attribute) { if ($attribute['type'] == $id) { $n = count($attribute['value']); switch (true) { case $disposition == self::ATTR_APPEND: case $disposition == self::ATTR_REPLACE: return false; case $disposition == self::ATTR_ALL: return $attribute['value']; case $disposition >= $n: $disposition -= $n; break; default: return $attribute['value'][$disposition]; } } } return false; } /** * Returns a list of all CSR attributes in use * * @param array $csr optional * @access public * @return array */ function getAttributes($csr = null) { if (empty($csr)) { $csr = $this->currentCert; } $attributes = $this->_subArray($csr, 'certificationRequestInfo/attributes'); $attrs = array(); if (is_array($attributes)) { foreach ($attributes as $attribute) { $attrs[] = $attribute['type']; } } return $attrs; } /** * Set a CSR attribute * * @param string $id * @param mixed $value * @param bool $disposition optional * @access public * @return bool */ function setAttribute($id, $value, $disposition = self::ATTR_ALL) { $attributes = &$this->_subArray($this->currentCert, 'certificationRequestInfo/attributes', true); if (!is_array($attributes)) { return false; } switch ($disposition) { case self::ATTR_REPLACE: $disposition = self::ATTR_APPEND; case self::ATTR_ALL: $this->removeAttribute($id); break; } foreach ($attributes as $key => $attribute) { if ($attribute['type'] == $id) { $n = count($attribute['value']); switch (true) { case $disposition == self::ATTR_APPEND: $last = $key; break; case $disposition >= $n: $disposition -= $n; break; default: $attributes[$key]['value'][$disposition] = $value; return true; } } } switch (true) { case $disposition >= 0: return false; case isset($last): $attributes[$last]['value'][] = $value; break; default: $attributes[] = array('type' => $id, 'value' => $disposition == self::ATTR_ALL ? $value: array($value)); break; } return true; } /** * Sets the subject key identifier * * This is used by the id-ce-authorityKeyIdentifier and the id-ce-subjectKeyIdentifier extensions. * * @param string $value * @access public */ function setKeyIdentifier($value) { if (empty($value)) { unset($this->currentKeyIdentifier); } else { $this->currentKeyIdentifier = base64_encode($value); } } /** * Compute a public key identifier. * * Although key identifiers may be set to any unique value, this function * computes key identifiers from public key according to the two * recommended methods (4.2.1.2 RFC 3280). * Highly polymorphic: try to accept all possible forms of key: * - Key object * - \phpseclib\File\X509 object with public or private key defined * - Certificate or CSR array * - \phpseclib\File\ASN1\Element object * - PEM or DER string * * @param mixed $key optional * @param int $method optional * @access public * @return string binary key identifier */ function computeKeyIdentifier($key = null, $method = 1) { if (is_null($key)) { $key = $this; } switch (true) { case is_string($key): break; case is_array($key) && isset($key['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey']): return $this->computeKeyIdentifier($key['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'], $method); case is_array($key) && isset($key['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey']): return $this->computeKeyIdentifier($key['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey'], $method); case !is_object($key): return false; case $key instanceof Element: // Assume the element is a bitstring-packed key. $asn1 = new ASN1(); $decoded = $asn1->decodeBER($key->element); if (empty($decoded)) { return false; } $raw = $asn1->asn1map($decoded[0], array('type' => ASN1::TYPE_BIT_STRING)); if (empty($raw)) { return false; } $raw = base64_decode($raw); // If the key is private, compute identifier from its corresponding public key. $key = new RSA(); if (!$key->loadKey($raw)) { return false; // Not an unencrypted RSA key. } if ($key->getPrivateKey() !== false) { // If private. return $this->computeKeyIdentifier($key, $method); } $key = $raw; // Is a public key. break; case $key instanceof X509: if (isset($key->publicKey)) { return $this->computeKeyIdentifier($key->publicKey, $method); } if (isset($key->privateKey)) { return $this->computeKeyIdentifier($key->privateKey, $method); } if (isset($key->currentCert['tbsCertificate']) || isset($key->currentCert['certificationRequestInfo'])) { return $this->computeKeyIdentifier($key->currentCert, $method); } return false; default: // Should be a key object (i.e.: \phpseclib\Crypt\RSA). $key = $key->getPublicKey(RSA::PUBLIC_FORMAT_PKCS1); break; } // If in PEM format, convert to binary. $key = $this->_extractBER($key); // Now we have the key string: compute its sha-1 sum. $hash = new Hash('sha1'); $hash = $hash->hash($key); if ($method == 2) { $hash = substr($hash, -8); $hash[0] = chr((ord($hash[0]) & 0x0F) | 0x40); } return $hash; } /** * Format a public key as appropriate * * @access private * @return array */ function _formatSubjectPublicKey() { if ($this->publicKey instanceof RSA) { // the following two return statements do the same thing. i dunno.. i just prefer the later for some reason. // the former is a good example of how to do fuzzing on the public key //return new Element(base64_decode(preg_replace('#-.+-|[\r\n]#', '', $this->publicKey->getPublicKey()))); return array( 'algorithm' => array('algorithm' => 'rsaEncryption'), 'subjectPublicKey' => $this->publicKey->getPublicKey(RSA::PUBLIC_FORMAT_PKCS1) ); } return false; } /** * Set the domain name's which the cert is to be valid for * * @access public * @return array */ function setDomain() { $this->domains = func_get_args(); $this->removeDNProp('id-at-commonName'); $this->setDNProp('id-at-commonName', $this->domains[0]); } /** * Set the IP Addresses's which the cert is to be valid for * * @access public * @param string $ipAddress optional */ function setIPAddress() { $this->ipAddresses = func_get_args(); /* if (!isset($this->domains)) { $this->removeDNProp('id-at-commonName'); $this->setDNProp('id-at-commonName', $this->ipAddresses[0]); } */ } /** * Helper function to build domain array * * @access private * @param string $domain * @return array */ function _dnsName($domain) { return array('dNSName' => $domain); } /** * Helper function to build IP Address array * * (IPv6 is not currently supported) * * @access private * @param string $address * @return array */ function _iPAddress($address) { return array('iPAddress' => $address); } /** * Get the index of a revoked certificate. * * @param array $rclist * @param string $serial * @param bool $create optional * @access private * @return int|false */ function _revokedCertificate(&$rclist, $serial, $create = false) { $serial = new BigInteger($serial); foreach ($rclist as $i => $rc) { if (!($serial->compare($rc['userCertificate']))) { return $i; } } if (!$create) { return false; } $i = count($rclist); $revocationDate = new DateTime('now', new DateTimeZone(@date_default_timezone_get())); $rclist[] = array('userCertificate' => $serial, 'revocationDate' => $this->_timeField($revocationDate->format('D, d M Y H:i:s O'))); return $i; } /** * Revoke a certificate. * * @param string $serial * @param string $date optional * @access public * @return bool */ function revoke($serial, $date = null) { if (isset($this->currentCert['tbsCertList'])) { if (is_array($rclist = &$this->_subArray($this->currentCert, 'tbsCertList/revokedCertificates', true))) { if ($this->_revokedCertificate($rclist, $serial) === false) { // If not yet revoked if (($i = $this->_revokedCertificate($rclist, $serial, true)) !== false) { if (!empty($date)) { $rclist[$i]['revocationDate'] = $this->_timeField($date); } return true; } } } } return false; } /** * Unrevoke a certificate. * * @param string $serial * @access public * @return bool */ function unrevoke($serial) { if (is_array($rclist = &$this->_subArray($this->currentCert, 'tbsCertList/revokedCertificates'))) { if (($i = $this->_revokedCertificate($rclist, $serial)) !== false) { unset($rclist[$i]); $rclist = array_values($rclist); return true; } } return false; } /** * Get a revoked certificate. * * @param string $serial * @access public * @return mixed */ function getRevoked($serial) { if (is_array($rclist = $this->_subArray($this->currentCert, 'tbsCertList/revokedCertificates'))) { if (($i = $this->_revokedCertificate($rclist, $serial)) !== false) { return $rclist[$i]; } } return false; } /** * List revoked certificates * * @param array $crl optional * @access public * @return array */ function listRevoked($crl = null) { if (!isset($crl)) { $crl = $this->currentCert; } if (!isset($crl['tbsCertList'])) { return false; } $result = array(); if (is_array($rclist = $this->_subArray($crl, 'tbsCertList/revokedCertificates'))) { foreach ($rclist as $rc) { $result[] = $rc['userCertificate']->toString(); } } return $result; } /** * Remove a Revoked Certificate Extension * * @param string $serial * @param string $id * @access public * @return bool */ function removeRevokedCertificateExtension($serial, $id) { if (is_array($rclist = &$this->_subArray($this->currentCert, 'tbsCertList/revokedCertificates'))) { if (($i = $this->_revokedCertificate($rclist, $serial)) !== false) { return $this->_removeExtension($id, "tbsCertList/revokedCertificates/$i/crlEntryExtensions"); } } return false; } /** * Get a Revoked Certificate Extension * * Returns the extension if it exists and false if not * * @param string $serial * @param string $id * @param array $crl optional * @access public * @return mixed */ function getRevokedCertificateExtension($serial, $id, $crl = null) { if (!isset($crl)) { $crl = $this->currentCert; } if (is_array($rclist = $this->_subArray($crl, 'tbsCertList/revokedCertificates'))) { if (($i = $this->_revokedCertificate($rclist, $serial)) !== false) { return $this->_getExtension($id, $crl, "tbsCertList/revokedCertificates/$i/crlEntryExtensions"); } } return false; } /** * Returns a list of all extensions in use for a given revoked certificate * * @param string $serial * @param array $crl optional * @access public * @return array */ function getRevokedCertificateExtensions($serial, $crl = null) { if (!isset($crl)) { $crl = $this->currentCert; } if (is_array($rclist = $this->_subArray($crl, 'tbsCertList/revokedCertificates'))) { if (($i = $this->_revokedCertificate($rclist, $serial)) !== false) { return $this->_getExtensions($crl, "tbsCertList/revokedCertificates/$i/crlEntryExtensions"); } } return false; } /** * Set a Revoked Certificate Extension * * @param string $serial * @param string $id * @param mixed $value * @param bool $critical optional * @param bool $replace optional * @access public * @return bool */ function setRevokedCertificateExtension($serial, $id, $value, $critical = false, $replace = true) { if (isset($this->currentCert['tbsCertList'])) { if (is_array($rclist = &$this->_subArray($this->currentCert, 'tbsCertList/revokedCertificates', true))) { if (($i = $this->_revokedCertificate($rclist, $serial, true)) !== false) { return $this->_setExtension($id, $value, $critical, $replace, "tbsCertList/revokedCertificates/$i/crlEntryExtensions"); } } } return false; } /** * Extract raw BER from Base64 encoding * * @access private * @param string $str * @return string */ function _extractBER($str) { /* X.509 certs are assumed to be base64 encoded but sometimes they'll have additional things in them * above and beyond the ceritificate. * ie. some may have the following preceding the -----BEGIN CERTIFICATE----- line: * * Bag Attributes * localKeyID: 01 00 00 00 * subject=/O=organization/OU=org unit/CN=common name * issuer=/O=organization/CN=common name */ $temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1); // remove the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- stuff $temp = preg_replace('#-+[^-]+-+#', '', $temp); // remove new lines $temp = str_replace(array("\r", "\n", ' '), '', $temp); $temp = preg_match('#^[a-zA-Z\d/+]*={0,2}$#', $temp) ? base64_decode($temp) : false; return $temp != false ? $temp : $str; } /** * Returns the OID corresponding to a name * * What's returned in the associative array returned by loadX509() (or load*()) is either a name or an OID if * no OID to name mapping is available. The problem with this is that what may be an unmapped OID in one version * of phpseclib may not be unmapped in the next version, so apps that are looking at this OID may not be able * to work from version to version. * * This method will return the OID if a name is passed to it and if no mapping is avialable it'll assume that * what's being passed to it already is an OID and return that instead. A few examples. * * getOID('2.16.840.1.101.3.4.2.1') == '2.16.840.1.101.3.4.2.1' * getOID('id-sha256') == '2.16.840.1.101.3.4.2.1' * getOID('zzz') == 'zzz' * * @access public * @return string */ function getOID($name) { static $reverseMap; if (!isset($reverseMap)) { $reverseMap = array_flip($this->oids); } return isset($reverseMap[$name]) ? $reverseMap[$name] : $name; } } <?php /** * Pure-PHP ASN.1 Parser * * PHP version 5 * * ASN.1 provides the semantics for data encoded using various schemes. The most commonly * utilized scheme is DER or the "Distinguished Encoding Rules". PEM's are base64 encoded * DER blobs. * * \phpseclib\File\ASN1 decodes and encodes DER formatted messages and places them in a semantic context. * * Uses the 1988 ASN.1 syntax. * * @category File * @package ASN1 * @author Jim Wigginton <terrafrost@php.net> * @copyright 2012 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\File; use phpseclib\File\ASN1\Element; use phpseclib\Math\BigInteger; use DateTime; use DateTimeZone; /** * Pure-PHP ASN.1 Parser * * @package ASN1 * @author Jim Wigginton <terrafrost@php.net> * @access public */ class ASN1 { /**#@+ * Tag Classes * * @access private * @link http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#page=12 */ const CLASS_UNIVERSAL = 0; const CLASS_APPLICATION = 1; const CLASS_CONTEXT_SPECIFIC = 2; const CLASS_PRIVATE = 3; /**#@-*/ /**#@+ * Tag Classes * * @access private * @link http://www.obj-sys.com/asn1tutorial/node124.html */ const TYPE_BOOLEAN = 1; const TYPE_INTEGER = 2; const TYPE_BIT_STRING = 3; const TYPE_OCTET_STRING = 4; const TYPE_NULL = 5; const TYPE_OBJECT_IDENTIFIER = 6; //const TYPE_OBJECT_DESCRIPTOR = 7; //const TYPE_INSTANCE_OF = 8; // EXTERNAL const TYPE_REAL = 9; const TYPE_ENUMERATED = 10; //const TYPE_EMBEDDED = 11; const TYPE_UTF8_STRING = 12; //const TYPE_RELATIVE_OID = 13; const TYPE_SEQUENCE = 16; // SEQUENCE OF const TYPE_SET = 17; // SET OF /**#@-*/ /**#@+ * More Tag Classes * * @access private * @link http://www.obj-sys.com/asn1tutorial/node10.html */ const TYPE_NUMERIC_STRING = 18; const TYPE_PRINTABLE_STRING = 19; const TYPE_TELETEX_STRING = 20; // T61String const TYPE_VIDEOTEX_STRING = 21; const TYPE_IA5_STRING = 22; const TYPE_UTC_TIME = 23; const TYPE_GENERALIZED_TIME = 24; const TYPE_GRAPHIC_STRING = 25; const TYPE_VISIBLE_STRING = 26; // ISO646String const TYPE_GENERAL_STRING = 27; const TYPE_UNIVERSAL_STRING = 28; //const TYPE_CHARACTER_STRING = 29; const TYPE_BMP_STRING = 30; /**#@-*/ /**#@+ * Tag Aliases * * These tags are kinda place holders for other tags. * * @access private */ const TYPE_CHOICE = -1; const TYPE_ANY = -2; /**#@-*/ /** * ASN.1 object identifier * * @var array * @access private * @link http://en.wikipedia.org/wiki/Object_identifier */ var $oids = array(); /** * Default date format * * @var string * @access private * @link http://php.net/class.datetime */ var $format = 'D, d M Y H:i:s O'; /** * Default date format * * @var array * @access private * @see self::setTimeFormat() * @see self::asn1map() * @link http://php.net/class.datetime */ var $encoded; /** * Filters * * If the mapping type is self::TYPE_ANY what do we actually encode it as? * * @var array * @access private * @see self::_encode_der() */ var $filters; /** * Type mapping table for the ANY type. * * Structured or unknown types are mapped to a \phpseclib\File\ASN1\Element. * Unambiguous types get the direct mapping (int/real/bool). * Others are mapped as a choice, with an extra indexing level. * * @var array * @access public */ var $ANYmap = array( self::TYPE_BOOLEAN => true, self::TYPE_INTEGER => true, self::TYPE_BIT_STRING => 'bitString', self::TYPE_OCTET_STRING => 'octetString', self::TYPE_NULL => 'null', self::TYPE_OBJECT_IDENTIFIER => 'objectIdentifier', self::TYPE_REAL => true, self::TYPE_ENUMERATED => 'enumerated', self::TYPE_UTF8_STRING => 'utf8String', self::TYPE_NUMERIC_STRING => 'numericString', self::TYPE_PRINTABLE_STRING => 'printableString', self::TYPE_TELETEX_STRING => 'teletexString', self::TYPE_VIDEOTEX_STRING => 'videotexString', self::TYPE_IA5_STRING => 'ia5String', self::TYPE_UTC_TIME => 'utcTime', self::TYPE_GENERALIZED_TIME => 'generalTime', self::TYPE_GRAPHIC_STRING => 'graphicString', self::TYPE_VISIBLE_STRING => 'visibleString', self::TYPE_GENERAL_STRING => 'generalString', self::TYPE_UNIVERSAL_STRING => 'universalString', //self::TYPE_CHARACTER_STRING => 'characterString', self::TYPE_BMP_STRING => 'bmpString' ); /** * String type to character size mapping table. * * Non-convertable types are absent from this table. * size == 0 indicates variable length encoding. * * @var array * @access public */ var $stringTypeSize = array( self::TYPE_UTF8_STRING => 0, self::TYPE_BMP_STRING => 2, self::TYPE_UNIVERSAL_STRING => 4, self::TYPE_PRINTABLE_STRING => 1, self::TYPE_TELETEX_STRING => 1, self::TYPE_IA5_STRING => 1, self::TYPE_VISIBLE_STRING => 1, ); /** * Parse BER-encoding * * Serves a similar purpose to openssl's asn1parse * * @param string $encoded * @return array * @access public */ function decodeBER($encoded) { if ($encoded instanceof Element) { $encoded = $encoded->element; } $this->encoded = $encoded; // encapsulate in an array for BC with the old decodeBER return array($this->_decode_ber($encoded)); } /** * Parse BER-encoding (Helper function) * * Sometimes we want to get the BER encoding of a particular tag. $start lets us do that without having to reencode. * $encoded is passed by reference for the recursive calls done for self::TYPE_BIT_STRING and * self::TYPE_OCTET_STRING. In those cases, the indefinite length is used. * * @param string $encoded * @param int $start * @param int $encoded_pos * @return array * @access private */ function _decode_ber($encoded, $start = 0, $encoded_pos = 0) { $current = array('start' => $start); $type = ord($encoded[$encoded_pos++]); $start++; $constructed = ($type >> 5) & 1; $tag = $type & 0x1F; if ($tag == 0x1F) { $tag = 0; // process septets (since the eighth bit is ignored, it's not an octet) do { $temp = ord($encoded[$encoded_pos++]); $loop = $temp >> 7; $tag <<= 7; $tag |= $temp & 0x7F; $start++; } while ($loop); } // Length, as discussed in paragraph 8.1.3 of X.690-0207.pdf#page=13 $length = ord($encoded[$encoded_pos++]); $start++; if ($length == 0x80) { // indefinite length // "[A sender shall] use the indefinite form (see 8.1.3.6) if the encoding is constructed and is not all // immediately available." -- paragraph 8.1.3.2.c $length = strlen($encoded) - $encoded_pos; } elseif ($length & 0x80) { // definite length, long form // technically, the long form of the length can be represented by up to 126 octets (bytes), but we'll only // support it up to four. $length&= 0x7F; $temp = substr($encoded, $encoded_pos, $length); $encoded_pos += $length; // tags of indefinte length don't really have a header length; this length includes the tag $current+= array('headerlength' => $length + 2); $start+= $length; extract(unpack('Nlength', substr(str_pad($temp, 4, chr(0), STR_PAD_LEFT), -4))); } else { $current+= array('headerlength' => 2); } if ($length > (strlen($encoded) - $encoded_pos)) { return false; } $content = substr($encoded, $encoded_pos, $length); $content_pos = 0; // at this point $length can be overwritten. it's only accurate for definite length things as is /* Class is UNIVERSAL, APPLICATION, PRIVATE, or CONTEXT-SPECIFIC. The UNIVERSAL class is restricted to the ASN.1 built-in types. It defines an application-independent data type that must be distinguishable from all other data types. The other three classes are user defined. The APPLICATION class distinguishes data types that have a wide, scattered use within a particular presentation context. PRIVATE distinguishes data types within a particular organization or country. CONTEXT-SPECIFIC distinguishes members of a sequence or set, the alternatives of a CHOICE, or universally tagged set members. Only the class number appears in braces for this data type; the term CONTEXT-SPECIFIC does not appear. -- http://www.obj-sys.com/asn1tutorial/node12.html */ $class = ($type >> 6) & 3; switch ($class) { case self::CLASS_APPLICATION: case self::CLASS_PRIVATE: case self::CLASS_CONTEXT_SPECIFIC: if (!$constructed) { return array( 'type' => $class, 'constant' => $tag, 'content' => $content, 'length' => $length + $start - $current['start'] ); } $newcontent = array(); $remainingLength = $length; while ($remainingLength > 0) { $temp = $this->_decode_ber($content, $start, $content_pos); if ($temp === false) { break; } $length = $temp['length']; // end-of-content octets - see paragraph 8.1.5 if (substr($content, $content_pos + $length, 2) == "\0\0") { $length+= 2; $start+= $length; $newcontent[] = $temp; break; } $start+= $length; $remainingLength-= $length; $newcontent[] = $temp; $content_pos += $length; } return array( 'type' => $class, 'constant' => $tag, // the array encapsulation is for BC with the old format 'content' => $newcontent, // the only time when $content['headerlength'] isn't defined is when the length is indefinite. // the absence of $content['headerlength'] is how we know if something is indefinite or not. // technically, it could be defined to be 2 and then another indicator could be used but whatever. 'length' => $start - $current['start'] ) + $current; } $current+= array('type' => $tag); // decode UNIVERSAL tags switch ($tag) { case self::TYPE_BOOLEAN: // "The contents octets shall consist of a single octet." -- paragraph 8.2.1 //if (strlen($content) != 1) { // return false; //} $current['content'] = (bool) ord($content[$content_pos]); break; case self::TYPE_INTEGER: case self::TYPE_ENUMERATED: $current['content'] = new BigInteger(substr($content, $content_pos), -256); break; case self::TYPE_REAL: // not currently supported return false; case self::TYPE_BIT_STRING: // The initial octet shall encode, as an unsigned binary integer with bit 1 as the least significant bit, // the number of unused bits in the final subsequent octet. The number shall be in the range zero to // seven. if (!$constructed) { $current['content'] = substr($content, $content_pos); } else { $temp = $this->_decode_ber($content, $start, $content_pos); if ($temp === false) { return false; } $length-= (strlen($content) - $content_pos); $last = count($temp) - 1; for ($i = 0; $i < $last; $i++) { // all subtags should be bit strings //if ($temp[$i]['type'] != self::TYPE_BIT_STRING) { // return false; //} $current['content'].= substr($temp[$i]['content'], 1); } // all subtags should be bit strings //if ($temp[$last]['type'] != self::TYPE_BIT_STRING) { // return false; //} $current['content'] = $temp[$last]['content'][0] . $current['content'] . substr($temp[$i]['content'], 1); } break; case self::TYPE_OCTET_STRING: if (!$constructed) { $current['content'] = substr($content, $content_pos); } else { $current['content'] = ''; $length = 0; while (substr($content, $content_pos, 2) != "\0\0") { $temp = $this->_decode_ber($content, $length + $start, $content_pos); if ($temp === false) { return false; } $content_pos += $temp['length']; // all subtags should be octet strings //if ($temp['type'] != self::TYPE_OCTET_STRING) { // return false; //} $current['content'].= $temp['content']; $length+= $temp['length']; } if (substr($content, $content_pos, 2) == "\0\0") { $length+= 2; // +2 for the EOC } } break; case self::TYPE_NULL: // "The contents octets shall not contain any octets." -- paragraph 8.8.2 //if (strlen($content)) { // return false; //} break; case self::TYPE_SEQUENCE: case self::TYPE_SET: $offset = 0; $current['content'] = array(); $content_len = strlen($content); while ($content_pos < $content_len) { // if indefinite length construction was used and we have an end-of-content string next // see paragraphs 8.1.1.3, 8.1.3.2, 8.1.3.6, 8.1.5, and (for an example) 8.6.4.2 if (!isset($current['headerlength']) && substr($content, $content_pos, 2) == "\0\0") { $length = $offset + 2; // +2 for the EOC break 2; } $temp = $this->_decode_ber($content, $start + $offset, $content_pos); if ($temp === false) { return false; } $content_pos += $temp['length']; $current['content'][] = $temp; $offset+= $temp['length']; } break; case self::TYPE_OBJECT_IDENTIFIER: $current['content'] = $this->_decodeOID(substr($content, $content_pos)); break; /* Each character string type shall be encoded as if it had been declared: [UNIVERSAL x] IMPLICIT OCTET STRING -- X.690-0207.pdf#page=23 (paragraph 8.21.3) Per that, we're not going to do any validation. If there are any illegal characters in the string, we don't really care */ case self::TYPE_NUMERIC_STRING: // 0,1,2,3,4,5,6,7,8,9, and space case self::TYPE_PRINTABLE_STRING: // Upper and lower case letters, digits, space, apostrophe, left/right parenthesis, plus sign, comma, // hyphen, full stop, solidus, colon, equal sign, question mark case self::TYPE_TELETEX_STRING: // The Teletex character set in CCITT's T61, space, and delete // see http://en.wikipedia.org/wiki/Teletex#Character_sets case self::TYPE_VIDEOTEX_STRING: // The Videotex character set in CCITT's T.100 and T.101, space, and delete case self::TYPE_VISIBLE_STRING: // Printing character sets of international ASCII, and space case self::TYPE_IA5_STRING: // International Alphabet 5 (International ASCII) case self::TYPE_GRAPHIC_STRING: // All registered G sets, and space case self::TYPE_GENERAL_STRING: // All registered C and G sets, space and delete case self::TYPE_UTF8_STRING: // ???? case self::TYPE_BMP_STRING: $current['content'] = substr($content, $content_pos); break; case self::TYPE_UTC_TIME: case self::TYPE_GENERALIZED_TIME: $current['content'] = $this->_decodeTime(substr($content, $content_pos), $tag); default: } $start+= $length; // ie. length is the length of the full TLV encoding - it's not just the length of the value return $current + array('length' => $start - $current['start']); } /** * ASN.1 Map * * Provides an ASN.1 semantic mapping ($mapping) from a parsed BER-encoding to a human readable format. * * "Special" mappings may be applied on a per tag-name basis via $special. * * @param array $decoded * @param array $mapping * @param array $special * @return array * @access public */ function asn1map($decoded, $mapping, $special = array()) { if (isset($mapping['explicit']) && is_array($decoded['content'])) { $decoded = $decoded['content'][0]; } switch (true) { case $mapping['type'] == self::TYPE_ANY: $intype = $decoded['type']; if (isset($decoded['constant']) || !isset($this->ANYmap[$intype]) || (ord($this->encoded[$decoded['start']]) & 0x20)) { return new Element(substr($this->encoded, $decoded['start'], $decoded['length'])); } $inmap = $this->ANYmap[$intype]; if (is_string($inmap)) { return array($inmap => $this->asn1map($decoded, array('type' => $intype) + $mapping, $special)); } break; case $mapping['type'] == self::TYPE_CHOICE: foreach ($mapping['children'] as $key => $option) { switch (true) { case isset($option['constant']) && $option['constant'] == $decoded['constant']: case !isset($option['constant']) && $option['type'] == $decoded['type']: $value = $this->asn1map($decoded, $option, $special); break; case !isset($option['constant']) && $option['type'] == self::TYPE_CHOICE: $v = $this->asn1map($decoded, $option, $special); if (isset($v)) { $value = $v; } } if (isset($value)) { if (isset($special[$key])) { $value = call_user_func($special[$key], $value); } return array($key => $value); } } return null; case isset($mapping['implicit']): case isset($mapping['explicit']): case $decoded['type'] == $mapping['type']: break; default: // if $decoded['type'] and $mapping['type'] are both strings, but different types of strings, // let it through switch (true) { case $decoded['type'] < 18: // self::TYPE_NUMERIC_STRING == 18 case $decoded['type'] > 30: // self::TYPE_BMP_STRING == 30 case $mapping['type'] < 18: case $mapping['type'] > 30: return null; } } if (isset($mapping['implicit'])) { $decoded['type'] = $mapping['type']; } switch ($decoded['type']) { case self::TYPE_SEQUENCE: $map = array(); // ignore the min and max if (isset($mapping['min']) && isset($mapping['max'])) { $child = $mapping['children']; foreach ($decoded['content'] as $content) { if (($map[] = $this->asn1map($content, $child, $special)) === null) { return null; } } return $map; } $n = count($decoded['content']); $i = 0; foreach ($mapping['children'] as $key => $child) { $maymatch = $i < $n; // Match only existing input. if ($maymatch) { $temp = $decoded['content'][$i]; if ($child['type'] != self::TYPE_CHOICE) { // Get the mapping and input class & constant. $childClass = $tempClass = self::CLASS_UNIVERSAL; $constant = null; if (isset($temp['constant'])) { $tempClass = $temp['type']; } if (isset($child['class'])) { $childClass = $child['class']; $constant = $child['cast']; } elseif (isset($child['constant'])) { $childClass = self::CLASS_CONTEXT_SPECIFIC; $constant = $child['constant']; } if (isset($constant) && isset($temp['constant'])) { // Can only match if constants and class match. $maymatch = $constant == $temp['constant'] && $childClass == $tempClass; } else { // Can only match if no constant expected and type matches or is generic. $maymatch = !isset($child['constant']) && array_search($child['type'], array($temp['type'], self::TYPE_ANY, self::TYPE_CHOICE)) !== false; } } } if ($maymatch) { // Attempt submapping. $candidate = $this->asn1map($temp, $child, $special); $maymatch = $candidate !== null; } if ($maymatch) { // Got the match: use it. if (isset($special[$key])) { $candidate = call_user_func($special[$key], $candidate); } $map[$key] = $candidate; $i++; } elseif (isset($child['default'])) { $map[$key] = $child['default']; // Use default. } elseif (!isset($child['optional'])) { return null; // Syntax error. } } // Fail mapping if all input items have not been consumed. return $i < $n ? null: $map; // the main diff between sets and sequences is the encapsulation of the foreach in another for loop case self::TYPE_SET: $map = array(); // ignore the min and max if (isset($mapping['min']) && isset($mapping['max'])) { $child = $mapping['children']; foreach ($decoded['content'] as $content) { if (($map[] = $this->asn1map($content, $child, $special)) === null) { return null; } } return $map; } for ($i = 0; $i < count($decoded['content']); $i++) { $temp = $decoded['content'][$i]; $tempClass = self::CLASS_UNIVERSAL; if (isset($temp['constant'])) { $tempClass = $temp['type']; } foreach ($mapping['children'] as $key => $child) { if (isset($map[$key])) { continue; } $maymatch = true; if ($child['type'] != self::TYPE_CHOICE) { $childClass = self::CLASS_UNIVERSAL; $constant = null; if (isset($child['class'])) { $childClass = $child['class']; $constant = $child['cast']; } elseif (isset($child['constant'])) { $childClass = self::CLASS_CONTEXT_SPECIFIC; $constant = $child['constant']; } if (isset($constant) && isset($temp['constant'])) { // Can only match if constants and class match. $maymatch = $constant == $temp['constant'] && $childClass == $tempClass; } else { // Can only match if no constant expected and type matches or is generic. $maymatch = !isset($child['constant']) && array_search($child['type'], array($temp['type'], self::TYPE_ANY, self::TYPE_CHOICE)) !== false; } } if ($maymatch) { // Attempt submapping. $candidate = $this->asn1map($temp, $child, $special); $maymatch = $candidate !== null; } if (!$maymatch) { break; } // Got the match: use it. if (isset($special[$key])) { $candidate = call_user_func($special[$key], $candidate); } $map[$key] = $candidate; break; } } foreach ($mapping['children'] as $key => $child) { if (!isset($map[$key])) { if (isset($child['default'])) { $map[$key] = $child['default']; } elseif (!isset($child['optional'])) { return null; } } } return $map; case self::TYPE_OBJECT_IDENTIFIER: return isset($this->oids[$decoded['content']]) ? $this->oids[$decoded['content']] : $decoded['content']; case self::TYPE_UTC_TIME: case self::TYPE_GENERALIZED_TIME: // for explicitly tagged optional stuff if (is_array($decoded['content'])) { $decoded['content'] = $decoded['content'][0]['content']; } // for implicitly tagged optional stuff // in theory, doing isset($mapping['implicit']) would work but malformed certs do exist // in the wild that OpenSSL decodes without issue so we'll support them as well if (!is_object($decoded['content'])) { $decoded['content'] = $this->_decodeTime($decoded['content'], $decoded['type']); } return $decoded['content'] ? $decoded['content']->format($this->format) : false; case self::TYPE_BIT_STRING: if (isset($mapping['mapping'])) { $offset = ord($decoded['content'][0]); $size = (strlen($decoded['content']) - 1) * 8 - $offset; /* From X.680-0207.pdf#page=46 (21.7): "When a "NamedBitList" is used in defining a bitstring type ASN.1 encoding rules are free to add (or remove) arbitrarily any trailing 0 bits to (or from) values that are being encoded or decoded. Application designers should therefore ensure that different semantics are not associated with such values which differ only in the number of trailing 0 bits." */ $bits = count($mapping['mapping']) == $size ? array() : array_fill(0, count($mapping['mapping']) - $size, false); for ($i = strlen($decoded['content']) - 1; $i > 0; $i--) { $current = ord($decoded['content'][$i]); for ($j = $offset; $j < 8; $j++) { $bits[] = (bool) ($current & (1 << $j)); } $offset = 0; } $values = array(); $map = array_reverse($mapping['mapping']); foreach ($map as $i => $value) { if ($bits[$i]) { $values[] = $value; } } return $values; } case self::TYPE_OCTET_STRING: return base64_encode($decoded['content']); case self::TYPE_NULL: return ''; case self::TYPE_BOOLEAN: return $decoded['content']; case self::TYPE_NUMERIC_STRING: case self::TYPE_PRINTABLE_STRING: case self::TYPE_TELETEX_STRING: case self::TYPE_VIDEOTEX_STRING: case self::TYPE_IA5_STRING: case self::TYPE_GRAPHIC_STRING: case self::TYPE_VISIBLE_STRING: case self::TYPE_GENERAL_STRING: case self::TYPE_UNIVERSAL_STRING: case self::TYPE_UTF8_STRING: case self::TYPE_BMP_STRING: return $decoded['content']; case self::TYPE_INTEGER: case self::TYPE_ENUMERATED: $temp = $decoded['content']; if (isset($mapping['implicit'])) { $temp = new BigInteger($decoded['content'], -256); } if (isset($mapping['mapping'])) { $temp = (int) $temp->toString(); return isset($mapping['mapping'][$temp]) ? $mapping['mapping'][$temp] : false; } return $temp; } } /** * ASN.1 Encode * * DER-encodes an ASN.1 semantic mapping ($mapping). Some libraries would probably call this function * an ASN.1 compiler. * * "Special" mappings can be applied via $special. * * @param string $source * @param string $mapping * @param int $idx * @return string * @access public */ function encodeDER($source, $mapping, $special = array()) { $this->location = array(); return $this->_encode_der($source, $mapping, null, $special); } /** * ASN.1 Encode (Helper function) * * @param string $source * @param string $mapping * @param int $idx * @return string * @access private */ function _encode_der($source, $mapping, $idx = null, $special = array()) { if ($source instanceof Element) { return $source->element; } // do not encode (implicitly optional) fields with value set to default if (isset($mapping['default']) && $source === $mapping['default']) { return ''; } if (isset($idx)) { if (isset($special[$idx])) { $source = call_user_func($special[$idx], $source); } $this->location[] = $idx; } $tag = $mapping['type']; switch ($tag) { case self::TYPE_SET: // Children order is not important, thus process in sequence. case self::TYPE_SEQUENCE: $tag|= 0x20; // set the constructed bit // ignore the min and max if (isset($mapping['min']) && isset($mapping['max'])) { $value = array(); $child = $mapping['children']; foreach ($source as $content) { $temp = $this->_encode_der($content, $child, null, $special); if ($temp === false) { return false; } $value[]= $temp; } /* "The encodings of the component values of a set-of value shall appear in ascending order, the encodings being compared as octet strings with the shorter components being padded at their trailing end with 0-octets. NOTE - The padding octets are for comparison purposes only and do not appear in the encodings." -- sec 11.6 of http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf */ if ($mapping['type'] == self::TYPE_SET) { sort($value); } $value = implode('', $value); break; } $value = ''; foreach ($mapping['children'] as $key => $child) { if (!array_key_exists($key, $source)) { if (!isset($child['optional'])) { return false; } continue; } $temp = $this->_encode_der($source[$key], $child, $key, $special); if ($temp === false) { return false; } // An empty child encoding means it has been optimized out. // Else we should have at least one tag byte. if ($temp === '') { continue; } // if isset($child['constant']) is true then isset($child['optional']) should be true as well if (isset($child['constant'])) { /* From X.680-0207.pdf#page=58 (30.6): "The tagging construction specifies explicit tagging if any of the following holds: ... c) the "Tag Type" alternative is used and the value of "TagDefault" for the module is IMPLICIT TAGS or AUTOMATIC TAGS, but the type defined by "Type" is an untagged choice type, an untagged open type, or an untagged "DummyReference" (see ITU-T Rec. X.683 | ISO/IEC 8824-4, 8.3)." */ if (isset($child['explicit']) || $child['type'] == self::TYPE_CHOICE) { $subtag = chr((self::CLASS_CONTEXT_SPECIFIC << 6) | 0x20 | $child['constant']); $temp = $subtag . $this->_encodeLength(strlen($temp)) . $temp; } else { $subtag = chr((self::CLASS_CONTEXT_SPECIFIC << 6) | (ord($temp[0]) & 0x20) | $child['constant']); $temp = $subtag . substr($temp, 1); } } $value.= $temp; } break; case self::TYPE_CHOICE: $temp = false; foreach ($mapping['children'] as $key => $child) { if (!isset($source[$key])) { continue; } $temp = $this->_encode_der($source[$key], $child, $key, $special); if ($temp === false) { return false; } // An empty child encoding means it has been optimized out. // Else we should have at least one tag byte. if ($temp === '') { continue; } $tag = ord($temp[0]); // if isset($child['constant']) is true then isset($child['optional']) should be true as well if (isset($child['constant'])) { if (isset($child['explicit']) || $child['type'] == self::TYPE_CHOICE) { $subtag = chr((self::CLASS_CONTEXT_SPECIFIC << 6) | 0x20 | $child['constant']); $temp = $subtag . $this->_encodeLength(strlen($temp)) . $temp; } else { $subtag = chr((self::CLASS_CONTEXT_SPECIFIC << 6) | (ord($temp[0]) & 0x20) | $child['constant']); $temp = $subtag . substr($temp, 1); } } } if (isset($idx)) { array_pop($this->location); } if ($temp && isset($mapping['cast'])) { $temp[0] = chr(($mapping['class'] << 6) | ($tag & 0x20) | $mapping['cast']); } return $temp; case self::TYPE_INTEGER: case self::TYPE_ENUMERATED: if (!isset($mapping['mapping'])) { if (is_numeric($source)) { $source = new BigInteger($source); } $value = $source->toBytes(true); } else { $value = array_search($source, $mapping['mapping']); if ($value === false) { return false; } $value = new BigInteger($value); $value = $value->toBytes(true); } if (!strlen($value)) { $value = chr(0); } break; case self::TYPE_UTC_TIME: case self::TYPE_GENERALIZED_TIME: $format = $mapping['type'] == self::TYPE_UTC_TIME ? 'y' : 'Y'; $format.= 'mdHis'; $date = new DateTime($source, new DateTimeZone('GMT')); $value = $date->format($format) . 'Z'; break; case self::TYPE_BIT_STRING: if (isset($mapping['mapping'])) { $bits = array_fill(0, count($mapping['mapping']), 0); $size = 0; for ($i = 0; $i < count($mapping['mapping']); $i++) { if (in_array($mapping['mapping'][$i], $source)) { $bits[$i] = 1; $size = $i; } } if (isset($mapping['min']) && $mapping['min'] >= 1 && $size < $mapping['min']) { $size = $mapping['min'] - 1; } $offset = 8 - (($size + 1) & 7); $offset = $offset !== 8 ? $offset : 0; $value = chr($offset); for ($i = $size + 1; $i < count($mapping['mapping']); $i++) { unset($bits[$i]); } $bits = implode('', array_pad($bits, $size + $offset + 1, 0)); $bytes = explode(' ', rtrim(chunk_split($bits, 8, ' '))); foreach ($bytes as $byte) { $value.= chr(bindec($byte)); } break; } case self::TYPE_OCTET_STRING: /* The initial octet shall encode, as an unsigned binary integer with bit 1 as the least significant bit, the number of unused bits in the final subsequent octet. The number shall be in the range zero to seven. -- http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#page=16 */ $value = base64_decode($source); break; case self::TYPE_OBJECT_IDENTIFIER: $value = $this->_encodeOID($source); break; case self::TYPE_ANY: $loc = $this->location; if (isset($idx)) { array_pop($this->location); } switch (true) { case !isset($source): return $this->_encode_der(null, array('type' => self::TYPE_NULL) + $mapping, null, $special); case is_int($source): case $source instanceof BigInteger: return $this->_encode_der($source, array('type' => self::TYPE_INTEGER) + $mapping, null, $special); case is_float($source): return $this->_encode_der($source, array('type' => self::TYPE_REAL) + $mapping, null, $special); case is_bool($source): return $this->_encode_der($source, array('type' => self::TYPE_BOOLEAN) + $mapping, null, $special); case is_array($source) && count($source) == 1: $typename = implode('', array_keys($source)); $outtype = array_search($typename, $this->ANYmap, true); if ($outtype !== false) { return $this->_encode_der($source[$typename], array('type' => $outtype) + $mapping, null, $special); } } $filters = $this->filters; foreach ($loc as $part) { if (!isset($filters[$part])) { $filters = false; break; } $filters = $filters[$part]; } if ($filters === false) { user_error('No filters defined for ' . implode('/', $loc)); return false; } return $this->_encode_der($source, $filters + $mapping, null, $special); case self::TYPE_NULL: $value = ''; break; case self::TYPE_NUMERIC_STRING: case self::TYPE_TELETEX_STRING: case self::TYPE_PRINTABLE_STRING: case self::TYPE_UNIVERSAL_STRING: case self::TYPE_UTF8_STRING: case self::TYPE_BMP_STRING: case self::TYPE_IA5_STRING: case self::TYPE_VISIBLE_STRING: case self::TYPE_VIDEOTEX_STRING: case self::TYPE_GRAPHIC_STRING: case self::TYPE_GENERAL_STRING: $value = $source; break; case self::TYPE_BOOLEAN: $value = $source ? "\xFF" : "\x00"; break; default: user_error('Mapping provides no type definition for ' . implode('/', $this->location)); return false; } if (isset($idx)) { array_pop($this->location); } if (isset($mapping['cast'])) { if (isset($mapping['explicit']) || $mapping['type'] == self::TYPE_CHOICE) { $value = chr($tag) . $this->_encodeLength(strlen($value)) . $value; $tag = ($mapping['class'] << 6) | 0x20 | $mapping['cast']; } else { $tag = ($mapping['class'] << 6) | (ord($temp[0]) & 0x20) | $mapping['cast']; } } return chr($tag) . $this->_encodeLength(strlen($value)) . $value; } /** * DER-encode the length * * DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See * {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information. * * @access private * @param int $length * @return string */ function _encodeLength($length) { if ($length <= 0x7F) { return chr($length); } $temp = ltrim(pack('N', $length), chr(0)); return pack('Ca*', 0x80 | strlen($temp), $temp); } /** * BER-decode the OID * * Called by _decode_ber() * * @access private * @param string $content * @return string */ function _decodeOID($content) { static $eighty; if (!$eighty) { $eighty = new BigInteger(80); } $oid = array(); $pos = 0; $len = strlen($content); $n = new BigInteger(); while ($pos < $len) { $temp = ord($content[$pos++]); $n = $n->bitwise_leftShift(7); $n = $n->bitwise_or(new BigInteger($temp & 0x7F)); if (~$temp & 0x80) { $oid[] = $n; $n = new BigInteger(); } } $part1 = array_shift($oid); $first = floor(ord($content[0]) / 40); /* "This packing of the first two object identifier components recognizes that only three values are allocated from the root node, and at most 39 subsequent values from nodes reached by X = 0 and X = 1." -- https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#page=22 */ if ($first <= 2) { // ie. 0 <= ord($content[0]) < 120 (0x78) array_unshift($oid, ord($content[0]) % 40); array_unshift($oid, $first); } else { array_unshift($oid, $part1->subtract($eighty)); array_unshift($oid, 2); } return implode('.', $oid); } /** * DER-encode the OID * * Called by _encode_der() * * @access private * @param string $content * @return string */ function _encodeOID($source) { static $mask, $zero, $forty; if (!$mask) { $mask = new BigInteger(0x7F); $zero = new BigInteger(); $forty = new BigInteger(40); } $oid = preg_match('#(?:\d+\.)+#', $source) ? $source : array_search($source, $this->oids); if ($oid === false) { user_error('Invalid OID'); return false; } $parts = explode('.', $oid); $part1 = array_shift($parts); $part2 = array_shift($parts); $first = new BigInteger($part1); $first = $first->multiply($forty); $first = $first->add(new BigInteger($part2)); array_unshift($parts, $first->toString()); $value = ''; foreach ($parts as $part) { if (!$part) { $temp = "\0"; } else { $temp = ''; $part = new BigInteger($part); while (!$part->equals($zero)) { $submask = $part->bitwise_and($mask); $submask->setPrecision(8); $temp = (chr(0x80) | $submask->toBytes()) . $temp; $part = $part->bitwise_rightShift(7); } $temp[strlen($temp) - 1] = $temp[strlen($temp) - 1] & chr(0x7F); } $value.= $temp; } return $value; } /** * BER-decode the time * * Called by _decode_ber() and in the case of implicit tags asn1map(). * * @access private * @param string $content * @param int $tag * @return string */ function _decodeTime($content, $tag) { /* UTCTime: http://tools.ietf.org/html/rfc5280#section-4.1.2.5.1 http://www.obj-sys.com/asn1tutorial/node15.html GeneralizedTime: http://tools.ietf.org/html/rfc5280#section-4.1.2.5.2 http://www.obj-sys.com/asn1tutorial/node14.html */ $format = 'YmdHis'; if ($tag == self::TYPE_UTC_TIME) { // https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#page=28 says "the seconds // element shall always be present" but none-the-less I've seen X509 certs where it isn't and if the // browsers parse it phpseclib ought to too if (preg_match('#^(\d{10})(Z|[+-]\d{4})$#', $content, $matches)) { $content = $matches[1] . '00' . $matches[2]; } $prefix = substr($content, 0, 2) >= 50 ? '19' : '20'; $content = $prefix . $content; } elseif (strpos($content, '.') !== false) { $format.= '.u'; } if ($content[strlen($content) - 1] == 'Z') { $content = substr($content, 0, -1) . '+0000'; } if (strpos($content, '-') !== false || strpos($content, '+') !== false) { $format.= 'O'; } // error supression isn't necessary as of PHP 7.0: // http://php.net/manual/en/migration70.other-changes.php return @DateTime::createFromFormat($format, $content); } /** * Set the time format * * Sets the time / date format for asn1map(). * * @access public * @param string $format */ function setTimeFormat($format) { $this->format = $format; } /** * Load OIDs * * Load the relevant OIDs for a particular ASN.1 semantic mapping. * * @access public * @param array $oids */ function loadOIDs($oids) { $this->oids = $oids; } /** * Load filters * * See \phpseclib\File\X509, etc, for an example. * * @access public * @param array $filters */ function loadFilters($filters) { $this->filters = $filters; } /** * String Shift * * Inspired by array_shift * * @param string $string * @param int $index * @return string * @access private */ function _string_shift(&$string, $index = 1) { $substr = substr($string, 0, $index); $string = substr($string, $index); return $substr; } /** * String type conversion * * This is a lazy conversion, dealing only with character size. * No real conversion table is used. * * @param string $in * @param int $from * @param int $to * @return string * @access public */ function convert($in, $from = self::TYPE_UTF8_STRING, $to = self::TYPE_UTF8_STRING) { if (!isset($this->stringTypeSize[$from]) || !isset($this->stringTypeSize[$to])) { return false; } $insize = $this->stringTypeSize[$from]; $outsize = $this->stringTypeSize[$to]; $inlength = strlen($in); $out = ''; for ($i = 0; $i < $inlength;) { if ($inlength - $i < $insize) { return false; } // Get an input character as a 32-bit value. $c = ord($in[$i++]); switch (true) { case $insize == 4: $c = ($c << 8) | ord($in[$i++]); $c = ($c << 8) | ord($in[$i++]); case $insize == 2: $c = ($c << 8) | ord($in[$i++]); case $insize == 1: break; case ($c & 0x80) == 0x00: break; case ($c & 0x40) == 0x00: return false; default: $bit = 6; do { if ($bit > 25 || $i >= $inlength || (ord($in[$i]) & 0xC0) != 0x80) { return false; } $c = ($c << 6) | (ord($in[$i++]) & 0x3F); $bit += 5; $mask = 1 << $bit; } while ($c & $bit); $c &= $mask - 1; break; } // Convert and append the character to output string. $v = ''; switch (true) { case $outsize == 4: $v .= chr($c & 0xFF); $c >>= 8; $v .= chr($c & 0xFF); $c >>= 8; case $outsize == 2: $v .= chr($c & 0xFF); $c >>= 8; case $outsize == 1: $v .= chr($c & 0xFF); $c >>= 8; if ($c) { return false; } break; case ($c & 0x80000000) != 0: return false; case $c >= 0x04000000: $v .= chr(0x80 | ($c & 0x3F)); $c = ($c >> 6) | 0x04000000; case $c >= 0x00200000: $v .= chr(0x80 | ($c & 0x3F)); $c = ($c >> 6) | 0x00200000; case $c >= 0x00010000: $v .= chr(0x80 | ($c & 0x3F)); $c = ($c >> 6) | 0x00010000; case $c >= 0x00000800: $v .= chr(0x80 | ($c & 0x3F)); $c = ($c >> 6) | 0x00000800; case $c >= 0x00000080: $v .= chr(0x80 | ($c & 0x3F)); $c = ($c >> 6) | 0x000000C0; default: $v .= chr($c); break; } $out .= strrev($v); } return $out; } } <?php /** * Pure-PHP implementation of SFTP. * * PHP version 5 * * Currently only supports SFTPv2 and v3, which, according to wikipedia.org, "is the most widely used version, * implemented by the popular OpenSSH SFTP server". If you want SFTPv4/5/6 support, provide me with access * to an SFTPv4/5/6 server. * * The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}. * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $sftp = new \phpseclib\Net\SFTP('www.domain.tld'); * if (!$sftp->login('username', 'password')) { * exit('Login Failed'); * } * * echo $sftp->pwd() . "\r\n"; * $sftp->put('filename.ext', 'hello, world!'); * print_r($sftp->nlist()); * ?> * </code> * * @category Net * @package SFTP * @author Jim Wigginton <terrafrost@php.net> * @copyright 2009 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Net; /** * Pure-PHP implementations of SFTP. * * @package SFTP * @author Jim Wigginton <terrafrost@php.net> * @access public */ class SFTP extends SSH2 { /** * SFTP channel constant * * \phpseclib\Net\SSH2::exec() uses 0 and \phpseclib\Net\SSH2::read() / \phpseclib\Net\SSH2::write() use 1. * * @see \phpseclib\Net\SSH2::_send_channel_packet() * @see \phpseclib\Net\SSH2::_get_channel_packet() * @access private */ const CHANNEL = 0x100; /**#@+ * @access public * @see \phpseclib\Net\SFTP::put() */ /** * Reads data from a local file. */ const SOURCE_LOCAL_FILE = 1; /** * Reads data from a string. */ // this value isn't really used anymore but i'm keeping it reserved for historical reasons const SOURCE_STRING = 2; /** * Reads data from callback: * function callback($length) returns string to proceed, null for EOF */ const SOURCE_CALLBACK = 16; /** * Resumes an upload */ const RESUME = 4; /** * Append a local file to an already existing remote file */ const RESUME_START = 8; /**#@-*/ /** * Packet Types * * @see self::__construct() * @var array * @access private */ var $packet_types = array(); /** * Status Codes * * @see self::__construct() * @var array * @access private */ var $status_codes = array(); /** * The Request ID * * The request ID exists in the off chance that a packet is sent out-of-order. Of course, this library doesn't support * concurrent actions, so it's somewhat academic, here. * * @var boolean * @see self::_send_sftp_packet() * @access private */ var $use_request_id = false; /** * The Packet Type * * The request ID exists in the off chance that a packet is sent out-of-order. Of course, this library doesn't support * concurrent actions, so it's somewhat academic, here. * * @var int * @see self::_get_sftp_packet() * @access private */ var $packet_type = -1; /** * Packet Buffer * * @var string * @see self::_get_sftp_packet() * @access private */ var $packet_buffer = ''; /** * Extensions supported by the server * * @var array * @see self::_initChannel() * @access private */ var $extensions = array(); /** * Server SFTP version * * @var int * @see self::_initChannel() * @access private */ var $version; /** * Current working directory * * @var string * @see self::realpath() * @see self::chdir() * @access private */ var $pwd = false; /** * Packet Type Log * * @see self::getLog() * @var array * @access private */ var $packet_type_log = array(); /** * Packet Log * * @see self::getLog() * @var array * @access private */ var $packet_log = array(); /** * Error information * * @see self::getSFTPErrors() * @see self::getLastSFTPError() * @var array * @access private */ var $sftp_errors = array(); /** * Stat Cache * * Rather than always having to open a directory and close it immediately there after to see if a file is a directory * we'll cache the results. * * @see self::_update_stat_cache() * @see self::_remove_from_stat_cache() * @see self::_query_stat_cache() * @var array * @access private */ var $stat_cache = array(); /** * Max SFTP Packet Size * * @see self::__construct() * @see self::get() * @var array * @access private */ var $max_sftp_packet; /** * Stat Cache Flag * * @see self::disableStatCache() * @see self::enableStatCache() * @var bool * @access private */ var $use_stat_cache = true; /** * Sort Options * * @see self::_comparator() * @see self::setListOrder() * @var array * @access private */ var $sortOptions = array(); /** * Canonicalization Flag * * Determines whether or not paths should be canonicalized before being * passed on to the remote server. * * @see self::enablePathCanonicalization() * @see self::disablePathCanonicalization() * @see self::realpath() * @var bool * @access private */ var $canonicalize_paths = true; /** * Request Buffers * * @see self::_get_sftp_packet() * @var array * @access private */ var $requestBuffer = array(); /** * Default Constructor. * * Connects to an SFTP server * * @param string $host * @param int $port * @param int $timeout * @return \phpseclib\Net\SFTP * @access public */ function __construct($host, $port = 22, $timeout = 10) { parent::__construct($host, $port, $timeout); $this->max_sftp_packet = 1 << 15; $this->packet_types = array( 1 => 'NET_SFTP_INIT', 2 => 'NET_SFTP_VERSION', /* the format of SSH_FXP_OPEN changed between SFTPv4 and SFTPv5+: SFTPv5+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.1 pre-SFTPv5 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.3 */ 3 => 'NET_SFTP_OPEN', 4 => 'NET_SFTP_CLOSE', 5 => 'NET_SFTP_READ', 6 => 'NET_SFTP_WRITE', 7 => 'NET_SFTP_LSTAT', 9 => 'NET_SFTP_SETSTAT', 11 => 'NET_SFTP_OPENDIR', 12 => 'NET_SFTP_READDIR', 13 => 'NET_SFTP_REMOVE', 14 => 'NET_SFTP_MKDIR', 15 => 'NET_SFTP_RMDIR', 16 => 'NET_SFTP_REALPATH', 17 => 'NET_SFTP_STAT', /* the format of SSH_FXP_RENAME changed between SFTPv4 and SFTPv5+: SFTPv5+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3 pre-SFTPv5 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.5 */ 18 => 'NET_SFTP_RENAME', 19 => 'NET_SFTP_READLINK', 20 => 'NET_SFTP_SYMLINK', 101=> 'NET_SFTP_STATUS', 102=> 'NET_SFTP_HANDLE', /* the format of SSH_FXP_NAME changed between SFTPv3 and SFTPv4+: SFTPv4+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.4 pre-SFTPv4 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-7 */ 103=> 'NET_SFTP_DATA', 104=> 'NET_SFTP_NAME', 105=> 'NET_SFTP_ATTRS', 200=> 'NET_SFTP_EXTENDED' ); $this->status_codes = array( 0 => 'NET_SFTP_STATUS_OK', 1 => 'NET_SFTP_STATUS_EOF', 2 => 'NET_SFTP_STATUS_NO_SUCH_FILE', 3 => 'NET_SFTP_STATUS_PERMISSION_DENIED', 4 => 'NET_SFTP_STATUS_FAILURE', 5 => 'NET_SFTP_STATUS_BAD_MESSAGE', 6 => 'NET_SFTP_STATUS_NO_CONNECTION', 7 => 'NET_SFTP_STATUS_CONNECTION_LOST', 8 => 'NET_SFTP_STATUS_OP_UNSUPPORTED', 9 => 'NET_SFTP_STATUS_INVALID_HANDLE', 10 => 'NET_SFTP_STATUS_NO_SUCH_PATH', 11 => 'NET_SFTP_STATUS_FILE_ALREADY_EXISTS', 12 => 'NET_SFTP_STATUS_WRITE_PROTECT', 13 => 'NET_SFTP_STATUS_NO_MEDIA', 14 => 'NET_SFTP_STATUS_NO_SPACE_ON_FILESYSTEM', 15 => 'NET_SFTP_STATUS_QUOTA_EXCEEDED', 16 => 'NET_SFTP_STATUS_UNKNOWN_PRINCIPAL', 17 => 'NET_SFTP_STATUS_LOCK_CONFLICT', 18 => 'NET_SFTP_STATUS_DIR_NOT_EMPTY', 19 => 'NET_SFTP_STATUS_NOT_A_DIRECTORY', 20 => 'NET_SFTP_STATUS_INVALID_FILENAME', 21 => 'NET_SFTP_STATUS_LINK_LOOP', 22 => 'NET_SFTP_STATUS_CANNOT_DELETE', 23 => 'NET_SFTP_STATUS_INVALID_PARAMETER', 24 => 'NET_SFTP_STATUS_FILE_IS_A_DIRECTORY', 25 => 'NET_SFTP_STATUS_BYTE_RANGE_LOCK_CONFLICT', 26 => 'NET_SFTP_STATUS_BYTE_RANGE_LOCK_REFUSED', 27 => 'NET_SFTP_STATUS_DELETE_PENDING', 28 => 'NET_SFTP_STATUS_FILE_CORRUPT', 29 => 'NET_SFTP_STATUS_OWNER_INVALID', 30 => 'NET_SFTP_STATUS_GROUP_INVALID', 31 => 'NET_SFTP_STATUS_NO_MATCHING_BYTE_RANGE_LOCK' ); // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-7.1 // the order, in this case, matters quite a lot - see \phpseclib\Net\SFTP::_parseAttributes() to understand why $this->attributes = array( 0x00000001 => 'NET_SFTP_ATTR_SIZE', 0x00000002 => 'NET_SFTP_ATTR_UIDGID', // defined in SFTPv3, removed in SFTPv4+ 0x00000004 => 'NET_SFTP_ATTR_PERMISSIONS', 0x00000008 => 'NET_SFTP_ATTR_ACCESSTIME', // 0x80000000 will yield a floating point on 32-bit systems and converting floating points to integers // yields inconsistent behavior depending on how php is compiled. so we left shift -1 (which, in // two's compliment, consists of all 1 bits) by 31. on 64-bit systems this'll yield 0xFFFFFFFF80000000. // that's not a problem, however, and 'anded' and a 32-bit number, as all the leading 1 bits are ignored. (-1 << 31) & 0xFFFFFFFF => 'NET_SFTP_ATTR_EXTENDED' ); // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.3 // the flag definitions change somewhat in SFTPv5+. if SFTPv5+ support is added to this library, maybe name // the array for that $this->open5_flags and similarly alter the constant names. $this->open_flags = array( 0x00000001 => 'NET_SFTP_OPEN_READ', 0x00000002 => 'NET_SFTP_OPEN_WRITE', 0x00000004 => 'NET_SFTP_OPEN_APPEND', 0x00000008 => 'NET_SFTP_OPEN_CREATE', 0x00000010 => 'NET_SFTP_OPEN_TRUNCATE', 0x00000020 => 'NET_SFTP_OPEN_EXCL' ); // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-5.2 // see \phpseclib\Net\SFTP::_parseLongname() for an explanation $this->file_types = array( 1 => 'NET_SFTP_TYPE_REGULAR', 2 => 'NET_SFTP_TYPE_DIRECTORY', 3 => 'NET_SFTP_TYPE_SYMLINK', 4 => 'NET_SFTP_TYPE_SPECIAL', 5 => 'NET_SFTP_TYPE_UNKNOWN', // the followin types were first defined for use in SFTPv5+ // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-05#section-5.2 6 => 'NET_SFTP_TYPE_SOCKET', 7 => 'NET_SFTP_TYPE_CHAR_DEVICE', 8 => 'NET_SFTP_TYPE_BLOCK_DEVICE', 9 => 'NET_SFTP_TYPE_FIFO' ); $this->_define_array( $this->packet_types, $this->status_codes, $this->attributes, $this->open_flags, $this->file_types ); if (!defined('NET_SFTP_QUEUE_SIZE')) { define('NET_SFTP_QUEUE_SIZE', 32); } } /** * Login * * @param string $username * @param string $password * @return bool * @access public */ function login($username) { $args = func_get_args(); $this->auth[] = $args; if (!call_user_func_array(array(&$this, '_login'), $args)) { return false; } $this->window_size_server_to_client[self::CHANNEL] = $this->window_size; $packet = pack( 'CNa*N3', NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', self::CHANNEL, $this->window_size, 0x4000 ); if (!$this->_send_binary_packet($packet)) { return false; } $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_OPEN; $response = $this->_get_channel_packet(self::CHANNEL, true); if ($response === false) { return false; } $packet = pack( 'CNNa*CNa*', NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL], strlen('subsystem'), 'subsystem', 1, strlen('sftp'), 'sftp' ); if (!$this->_send_binary_packet($packet)) { return false; } $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST; $response = $this->_get_channel_packet(self::CHANNEL, true); if ($response === false) { // from PuTTY's psftp.exe $command = "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n" . "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n" . "exec sftp-server"; // we don't do $this->exec($command, false) because exec() operates on a different channel and plus the SSH_MSG_CHANNEL_OPEN that exec() does // is redundant $packet = pack( 'CNNa*CNa*', NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL], strlen('exec'), 'exec', 1, strlen($command), $command ); if (!$this->_send_binary_packet($packet)) { return false; } $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST; $response = $this->_get_channel_packet(self::CHANNEL, true); if ($response === false) { return false; } } $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_DATA; if (!$this->_send_sftp_packet(NET_SFTP_INIT, "\0\0\0\3")) { return false; } $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_VERSION) { user_error('Expected SSH_FXP_VERSION'); return false; } if (strlen($response) < 4) { return false; } extract(unpack('Nversion', $this->_string_shift($response, 4))); $this->version = $version; while (!empty($response)) { if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $key = $this->_string_shift($response, $length); if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $value = $this->_string_shift($response, $length); $this->extensions[$key] = $value; } /* SFTPv4+ defines a 'newline' extension. SFTPv3 seems to have unofficial support for it via 'newline@vandyke.com', however, I'm not sure what 'newline@vandyke.com' is supposed to do (the fact that it's unofficial means that it's not in the official SFTPv3 specs) and 'newline@vandyke.com' / 'newline' are likely not drop-in substitutes for one another due to the fact that 'newline' comes with a SSH_FXF_TEXT bitmask whereas it seems unlikely that 'newline@vandyke.com' would. */ /* if (isset($this->extensions['newline@vandyke.com'])) { $this->extensions['newline'] = $this->extensions['newline@vandyke.com']; unset($this->extensions['newline@vandyke.com']); } */ $this->use_request_id = true; /* A Note on SFTPv4/5/6 support: <http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-5.1> states the following: "If the client wishes to interoperate with servers that support noncontiguous version numbers it SHOULD send '3'" Given that the server only sends its version number after the client has already done so, the above seems to be suggesting that v3 should be the default version. This makes sense given that v3 is the most popular. <http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-5.5> states the following; "If the server did not send the "versions" extension, or the version-from-list was not included, the server MAY send a status response describing the failure, but MUST then close the channel without processing any further requests." So what do you do if you have a client whose initial SSH_FXP_INIT packet says it implements v3 and a server whose initial SSH_FXP_VERSION reply says it implements v4 and only v4? If it only implements v4, the "versions" extension is likely not going to have been sent so version re-negotiation as discussed in draft-ietf-secsh-filexfer-13 would be quite impossible. As such, what \phpseclib\Net\SFTP would do is close the channel and reopen it with a new and updated SSH_FXP_INIT packet. */ switch ($this->version) { case 2: case 3: break; default: return false; } $this->pwd = $this->_realpath('.'); $this->_update_stat_cache($this->pwd, array()); return true; } /** * Disable the stat cache * * @access public */ function disableStatCache() { $this->use_stat_cache = false; } /** * Enable the stat cache * * @access public */ function enableStatCache() { $this->use_stat_cache = true; } /** * Clear the stat cache * * @access public */ function clearStatCache() { $this->stat_cache = array(); } /** * Enable path canonicalization * * @access public */ function enablePathCanonicalization() { $this->canonicalize_paths = true; } /** * Enable path canonicalization * * @access public */ function disablePathCanonicalization() { $this->canonicalize_paths = false; } /** * Returns the current directory name * * @return mixed * @access public */ function pwd() { return $this->pwd; } /** * Logs errors * * @param string $response * @param int $status * @access public */ function _logError($response, $status = -1) { if ($status == -1) { if (strlen($response) < 4) { return; } extract(unpack('Nstatus', $this->_string_shift($response, 4))); } $error = $this->status_codes[$status]; if ($this->version > 2 || strlen($response) < 4) { extract(unpack('Nlength', $this->_string_shift($response, 4))); $this->sftp_errors[] = $error . ': ' . $this->_string_shift($response, $length); } else { $this->sftp_errors[] = $error; } } /** * Returns canonicalized absolute pathname * * realpath() expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input * path and returns the canonicalized absolute pathname. * * @param string $path * @return mixed * @access public */ function realpath($path) { return $this->_realpath($path); } /** * Canonicalize the Server-Side Path Name * * SFTP doesn't provide a mechanism by which the current working directory can be changed, so we'll emulate it. Returns * the absolute (canonicalized) path. * * If canonicalize_paths has been disabled using disablePathCanonicalization(), $path is returned as-is. * * @see self::chdir() * @see self::disablePathCanonicalization() * @param string $path * @return mixed * @access private */ function _realpath($path) { if (!$this->canonicalize_paths) { return $path; } if ($this->pwd === false) { // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.9 if (!$this->_send_sftp_packet(NET_SFTP_REALPATH, pack('Na*', strlen($path), $path))) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_NAME: // although SSH_FXP_NAME is implemented differently in SFTPv3 than it is in SFTPv4+, the following // should work on all SFTP versions since the only part of the SSH_FXP_NAME packet the following looks // at is the first part and that part is defined the same in SFTP versions 3 through 6. $this->_string_shift($response, 4); // skip over the count - it should be 1, anyway if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); return $this->_string_shift($response, $length); case NET_SFTP_STATUS: $this->_logError($response); return false; default: user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS'); return false; } } if ($path[0] != '/') { $path = $this->pwd . '/' . $path; } $path = explode('/', $path); $new = array(); foreach ($path as $dir) { if (!strlen($dir)) { continue; } switch ($dir) { case '..': array_pop($new); case '.': break; default: $new[] = $dir; } } return '/' . implode('/', $new); } /** * Changes the current directory * * @param string $dir * @return bool * @access public */ function chdir($dir) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } // assume current dir if $dir is empty if ($dir === '') { $dir = './'; // suffix a slash if needed } elseif ($dir[strlen($dir) - 1] != '/') { $dir.= '/'; } $dir = $this->_realpath($dir); // confirm that $dir is, in fact, a valid directory if ($this->use_stat_cache && is_array($this->_query_stat_cache($dir))) { $this->pwd = $dir; return true; } // we could do a stat on the alleged $dir to see if it's a directory but that doesn't tell us // the currently logged in user has the appropriate permissions or not. maybe you could see if // the file's uid / gid match the currently logged in user's uid / gid but how there's no easy // way to get those with SFTP if (!$this->_send_sftp_packet(NET_SFTP_OPENDIR, pack('Na*', strlen($dir), $dir))) { return false; } // see \phpseclib\Net\SFTP::nlist() for a more thorough explanation of the following $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_HANDLE: $handle = substr($response, 4); break; case NET_SFTP_STATUS: $this->_logError($response); return false; default: user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } if (!$this->_close_handle($handle)) { return false; } $this->_update_stat_cache($dir, array()); $this->pwd = $dir; return true; } /** * Returns a list of files in the given directory * * @param string $dir * @param bool $recursive * @return mixed * @access public */ function nlist($dir = '.', $recursive = false) { return $this->_nlist_helper($dir, $recursive, ''); } /** * Helper method for nlist * * @param string $dir * @param bool $recursive * @param string $relativeDir * @return mixed * @access private */ function _nlist_helper($dir, $recursive, $relativeDir) { $files = $this->_list($dir, false); if (!$recursive || $files === false) { return $files; } $result = array(); foreach ($files as $value) { if ($value == '.' || $value == '..') { if ($relativeDir == '') { $result[] = $value; } continue; } if (is_array($this->_query_stat_cache($this->_realpath($dir . '/' . $value)))) { $temp = $this->_nlist_helper($dir . '/' . $value, true, $relativeDir . $value . '/'); $temp = is_array($temp) ? $temp : array(); $result = array_merge($result, $temp); } else { $result[] = $relativeDir . $value; } } return $result; } /** * Returns a detailed list of files in the given directory * * @param string $dir * @param bool $recursive * @return mixed * @access public */ function rawlist($dir = '.', $recursive = false) { $files = $this->_list($dir, true); if (!$recursive || $files === false) { return $files; } static $depth = 0; foreach ($files as $key => $value) { if ($depth != 0 && $key == '..') { unset($files[$key]); continue; } $is_directory = false; if ($key != '.' && $key != '..') { if ($this->use_stat_cache) { $is_directory = is_array($this->_query_stat_cache($this->_realpath($dir . '/' . $key))); } else { $stat = $this->lstat($dir . '/' . $key); $is_directory = $stat && $stat['type'] === NET_SFTP_TYPE_DIRECTORY; } } if ($is_directory) { $depth++; $files[$key] = $this->rawlist($dir . '/' . $key, true); $depth--; } else { $files[$key] = (object) $value; } } return $files; } /** * Reads a list, be it detailed or not, of files in the given directory * * @param string $dir * @param bool $raw * @return mixed * @access private */ function _list($dir, $raw = true) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $dir = $this->_realpath($dir . '/'); if ($dir === false) { return false; } // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.2 if (!$this->_send_sftp_packet(NET_SFTP_OPENDIR, pack('Na*', strlen($dir), $dir))) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_HANDLE: // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.2 // since 'handle' is the last field in the SSH_FXP_HANDLE packet, we'll just remove the first four bytes that // represent the length of the string and leave it at that $handle = substr($response, 4); break; case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED $this->_logError($response); return false; default: user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } $this->_update_stat_cache($dir, array()); $contents = array(); while (true) { // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.2 // why multiple SSH_FXP_READDIR packets would be sent when the response to a single one can span arbitrarily many // SSH_MSG_CHANNEL_DATA messages is not known to me. if (!$this->_send_sftp_packet(NET_SFTP_READDIR, pack('Na*', strlen($handle), $handle))) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_NAME: if (strlen($response) < 4) { return false; } extract(unpack('Ncount', $this->_string_shift($response, 4))); for ($i = 0; $i < $count; $i++) { if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $shortname = $this->_string_shift($response, $length); if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $longname = $this->_string_shift($response, $length); $attributes = $this->_parseAttributes($response); if (!isset($attributes['type'])) { $fileType = $this->_parseLongname($longname); if ($fileType) { $attributes['type'] = $fileType; } } $contents[$shortname] = $attributes + array('filename' => $shortname); if (isset($attributes['type']) && $attributes['type'] == NET_SFTP_TYPE_DIRECTORY && ($shortname != '.' && $shortname != '..')) { $this->_update_stat_cache($dir . '/' . $shortname, array()); } else { if ($shortname == '..') { $temp = $this->_realpath($dir . '/..') . '/.'; } else { $temp = $dir . '/' . $shortname; } $this->_update_stat_cache($temp, (object) array('lstat' => $attributes)); } // SFTPv6 has an optional boolean end-of-list field, but we'll ignore that, since the // final SSH_FXP_STATUS packet should tell us that, already. } break; case NET_SFTP_STATUS: if (strlen($response) < 4) { return false; } extract(unpack('Nstatus', $this->_string_shift($response, 4))); if ($status != NET_SFTP_STATUS_EOF) { $this->_logError($response, $status); return false; } break 2; default: user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS'); return false; } } if (!$this->_close_handle($handle)) { return false; } if (count($this->sortOptions)) { uasort($contents, array(&$this, '_comparator')); } return $raw ? $contents : array_keys($contents); } /** * Compares two rawlist entries using parameters set by setListOrder() * * Intended for use with uasort() * * @param array $a * @param array $b * @return int * @access private */ function _comparator($a, $b) { switch (true) { case $a['filename'] === '.' || $b['filename'] === '.': if ($a['filename'] === $b['filename']) { return 0; } return $a['filename'] === '.' ? -1 : 1; case $a['filename'] === '..' || $b['filename'] === '..': if ($a['filename'] === $b['filename']) { return 0; } return $a['filename'] === '..' ? -1 : 1; case isset($a['type']) && $a['type'] === NET_SFTP_TYPE_DIRECTORY: if (!isset($b['type'])) { return 1; } if ($b['type'] !== $a['type']) { return -1; } break; case isset($b['type']) && $b['type'] === NET_SFTP_TYPE_DIRECTORY: return 1; } foreach ($this->sortOptions as $sort => $order) { if (!isset($a[$sort]) || !isset($b[$sort])) { if (isset($a[$sort])) { return -1; } if (isset($b[$sort])) { return 1; } return 0; } switch ($sort) { case 'filename': $result = strcasecmp($a['filename'], $b['filename']); if ($result) { return $order === SORT_DESC ? -$result : $result; } break; case 'permissions': case 'mode': $a[$sort]&= 07777; $b[$sort]&= 07777; default: if ($a[$sort] === $b[$sort]) { break; } return $order === SORT_ASC ? $a[$sort] - $b[$sort] : $b[$sort] - $a[$sort]; } } } /** * Defines how nlist() and rawlist() will be sorted - if at all. * * If sorting is enabled directories and files will be sorted independently with * directories appearing before files in the resultant array that is returned. * * Any parameter returned by stat is a valid sort parameter for this function. * Filename comparisons are case insensitive. * * Examples: * * $sftp->setListOrder('filename', SORT_ASC); * $sftp->setListOrder('size', SORT_DESC, 'filename', SORT_ASC); * $sftp->setListOrder(true); * Separates directories from files but doesn't do any sorting beyond that * $sftp->setListOrder(); * Don't do any sort of sorting * * @access public */ function setListOrder() { $this->sortOptions = array(); $args = func_get_args(); if (empty($args)) { return; } $len = count($args) & 0x7FFFFFFE; for ($i = 0; $i < $len; $i+=2) { $this->sortOptions[$args[$i]] = $args[$i + 1]; } if (!count($this->sortOptions)) { $this->sortOptions = array('bogus' => true); } } /** * Returns the file size, in bytes, or false, on failure * * Files larger than 4GB will show up as being exactly 4GB. * * @param string $filename * @return mixed * @access public */ function size($filename) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $result = $this->stat($filename); if ($result === false) { return false; } return isset($result['size']) ? $result['size'] : -1; } /** * Save files / directories to cache * * @param string $path * @param mixed $value * @access private */ function _update_stat_cache($path, $value) { if ($this->use_stat_cache === false) { return; } // preg_replace('#^/|/(?=/)|/$#', '', $dir) == str_replace('//', '/', trim($path, '/')) $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $path)); $temp = &$this->stat_cache; $max = count($dirs) - 1; foreach ($dirs as $i => $dir) { // if $temp is an object that means one of two things. // 1. a file was deleted and changed to a directory behind phpseclib's back // 2. it's a symlink. when lstat is done it's unclear what it's a symlink to if (is_object($temp)) { $temp = array(); } if (!isset($temp[$dir])) { $temp[$dir] = array(); } if ($i === $max) { if (is_object($temp[$dir]) && is_object($value)) { if (!isset($value->stat) && isset($temp[$dir]->stat)) { $value->stat = $temp[$dir]->stat; } if (!isset($value->lstat) && isset($temp[$dir]->lstat)) { $value->lstat = $temp[$dir]->lstat; } } $temp[$dir] = $value; break; } $temp = &$temp[$dir]; } } /** * Remove files / directories from cache * * @param string $path * @return bool * @access private */ function _remove_from_stat_cache($path) { $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $path)); $temp = &$this->stat_cache; $max = count($dirs) - 1; foreach ($dirs as $i => $dir) { if ($i === $max) { unset($temp[$dir]); return true; } if (!isset($temp[$dir])) { return false; } $temp = &$temp[$dir]; } } /** * Checks cache for path * * Mainly used by file_exists * * @param string $dir * @return mixed * @access private */ function _query_stat_cache($path) { $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $path)); $temp = &$this->stat_cache; foreach ($dirs as $dir) { if (!isset($temp[$dir])) { return null; } $temp = &$temp[$dir]; } return $temp; } /** * Returns general information about a file. * * Returns an array on success and false otherwise. * * @param string $filename * @return mixed * @access public */ function stat($filename) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $filename = $this->_realpath($filename); if ($filename === false) { return false; } if ($this->use_stat_cache) { $result = $this->_query_stat_cache($filename); if (is_array($result) && isset($result['.']) && isset($result['.']->stat)) { return $result['.']->stat; } if (is_object($result) && isset($result->stat)) { return $result->stat; } } $stat = $this->_stat($filename, NET_SFTP_STAT); if ($stat === false) { $this->_remove_from_stat_cache($filename); return false; } if (isset($stat['type'])) { if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) { $filename.= '/.'; } $this->_update_stat_cache($filename, (object) array('stat' => $stat)); return $stat; } $pwd = $this->pwd; $stat['type'] = $this->chdir($filename) ? NET_SFTP_TYPE_DIRECTORY : NET_SFTP_TYPE_REGULAR; $this->pwd = $pwd; if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) { $filename.= '/.'; } $this->_update_stat_cache($filename, (object) array('stat' => $stat)); return $stat; } /** * Returns general information about a file or symbolic link. * * Returns an array on success and false otherwise. * * @param string $filename * @return mixed * @access public */ function lstat($filename) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $filename = $this->_realpath($filename); if ($filename === false) { return false; } if ($this->use_stat_cache) { $result = $this->_query_stat_cache($filename); if (is_array($result) && isset($result['.']) && isset($result['.']->lstat)) { return $result['.']->lstat; } if (is_object($result) && isset($result->lstat)) { return $result->lstat; } } $lstat = $this->_stat($filename, NET_SFTP_LSTAT); if ($lstat === false) { $this->_remove_from_stat_cache($filename); return false; } if (isset($lstat['type'])) { if ($lstat['type'] == NET_SFTP_TYPE_DIRECTORY) { $filename.= '/.'; } $this->_update_stat_cache($filename, (object) array('lstat' => $lstat)); return $lstat; } $stat = $this->_stat($filename, NET_SFTP_STAT); if ($lstat != $stat) { $lstat = array_merge($lstat, array('type' => NET_SFTP_TYPE_SYMLINK)); $this->_update_stat_cache($filename, (object) array('lstat' => $lstat)); return $stat; } $pwd = $this->pwd; $lstat['type'] = $this->chdir($filename) ? NET_SFTP_TYPE_DIRECTORY : NET_SFTP_TYPE_REGULAR; $this->pwd = $pwd; if ($lstat['type'] == NET_SFTP_TYPE_DIRECTORY) { $filename.= '/.'; } $this->_update_stat_cache($filename, (object) array('lstat' => $lstat)); return $lstat; } /** * Returns general information about a file or symbolic link * * Determines information without calling \phpseclib\Net\SFTP::realpath(). * The second parameter can be either NET_SFTP_STAT or NET_SFTP_LSTAT. * * @param string $filename * @param int $type * @return mixed * @access private */ function _stat($filename, $type) { // SFTPv4+ adds an additional 32-bit integer field - flags - to the following: $packet = pack('Na*', strlen($filename), $filename); if (!$this->_send_sftp_packet($type, $packet)) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_ATTRS: return $this->_parseAttributes($response); case NET_SFTP_STATUS: $this->_logError($response); return false; } user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS'); return false; } /** * Truncates a file to a given length * * @param string $filename * @param int $new_size * @return bool * @access public */ function truncate($filename, $new_size) { $attr = pack('N3', NET_SFTP_ATTR_SIZE, $new_size / 4294967296, $new_size); // 4294967296 == 0x100000000 == 1<<32 return $this->_setstat($filename, $attr, false); } /** * Sets access and modification time of file. * * If the file does not exist, it will be created. * * @param string $filename * @param int $time * @param int $atime * @return bool * @access public */ function touch($filename, $time = null, $atime = null) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $filename = $this->_realpath($filename); if ($filename === false) { return false; } if (!isset($time)) { $time = time(); } if (!isset($atime)) { $atime = $time; } $flags = NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE | NET_SFTP_OPEN_EXCL; $attr = pack('N3', NET_SFTP_ATTR_ACCESSTIME, $time, $atime); $packet = pack('Na*Na*', strlen($filename), $filename, $flags, $attr); if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_HANDLE: return $this->_close_handle(substr($response, 4)); case NET_SFTP_STATUS: $this->_logError($response); break; default: user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } return $this->_setstat($filename, $attr, false); } /** * Changes file or directory owner * * Returns true on success or false on error. * * @param string $filename * @param int $uid * @param bool $recursive * @return bool * @access public */ function chown($filename, $uid, $recursive = false) { // quoting from <http://www.kernel.org/doc/man-pages/online/pages/man2/chown.2.html>, // "if the owner or group is specified as -1, then that ID is not changed" $attr = pack('N3', NET_SFTP_ATTR_UIDGID, $uid, -1); return $this->_setstat($filename, $attr, $recursive); } /** * Changes file or directory group * * Returns true on success or false on error. * * @param string $filename * @param int $gid * @param bool $recursive * @return bool * @access public */ function chgrp($filename, $gid, $recursive = false) { $attr = pack('N3', NET_SFTP_ATTR_UIDGID, -1, $gid); return $this->_setstat($filename, $attr, $recursive); } /** * Set permissions on a file. * * Returns the new file permissions on success or false on error. * If $recursive is true than this just returns true or false. * * @param int $mode * @param string $filename * @param bool $recursive * @return mixed * @access public */ function chmod($mode, $filename, $recursive = false) { if (is_string($mode) && is_int($filename)) { $temp = $mode; $mode = $filename; $filename = $temp; } $attr = pack('N2', NET_SFTP_ATTR_PERMISSIONS, $mode & 07777); if (!$this->_setstat($filename, $attr, $recursive)) { return false; } if ($recursive) { return true; } $filename = $this->realpath($filename); // rather than return what the permissions *should* be, we'll return what they actually are. this will also // tell us if the file actually exists. // incidentally, SFTPv4+ adds an additional 32-bit integer field - flags - to the following: $packet = pack('Na*', strlen($filename), $filename); if (!$this->_send_sftp_packet(NET_SFTP_STAT, $packet)) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_ATTRS: $attrs = $this->_parseAttributes($response); return $attrs['permissions']; case NET_SFTP_STATUS: $this->_logError($response); return false; } user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS'); return false; } /** * Sets information about a file * * @param string $filename * @param string $attr * @param bool $recursive * @return bool * @access private */ function _setstat($filename, $attr, $recursive) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $filename = $this->_realpath($filename); if ($filename === false) { return false; } $this->_remove_from_stat_cache($filename); if ($recursive) { $i = 0; $result = $this->_setstat_recursive($filename, $attr, $i); $this->_read_put_responses($i); return $result; } // SFTPv4+ has an additional byte field - type - that would need to be sent, as well. setting it to // SSH_FILEXFER_TYPE_UNKNOWN might work. if not, we'd have to do an SSH_FXP_STAT before doing an SSH_FXP_SETSTAT. if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($filename), $filename, $attr))) { return false; } /* "Because some systems must use separate system calls to set various attributes, it is possible that a failure response will be returned, but yet some of the attributes may be have been successfully modified. If possible, servers SHOULD avoid this situation; however, clients MUST be aware that this is possible." -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.6 */ $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { user_error('Expected SSH_FXP_STATUS'); return false; } if (strlen($response) < 4) { return false; } extract(unpack('Nstatus', $this->_string_shift($response, 4))); if ($status != NET_SFTP_STATUS_OK) { $this->_logError($response, $status); return false; } return true; } /** * Recursively sets information on directories on the SFTP server * * Minimizes directory lookups and SSH_FXP_STATUS requests for speed. * * @param string $path * @param string $attr * @param int $i * @return bool * @access private */ function _setstat_recursive($path, $attr, &$i) { if (!$this->_read_put_responses($i)) { return false; } $i = 0; $entries = $this->_list($path, true); if ($entries === false) { return $this->_setstat($path, $attr, false); } // normally $entries would have at least . and .. but it might not if the directories // permissions didn't allow reading if (empty($entries)) { return false; } unset($entries['.'], $entries['..']); foreach ($entries as $filename => $props) { if (!isset($props['type'])) { return false; } $temp = $path . '/' . $filename; if ($props['type'] == NET_SFTP_TYPE_DIRECTORY) { if (!$this->_setstat_recursive($temp, $attr, $i)) { return false; } } else { if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($temp), $temp, $attr))) { return false; } $i++; if ($i >= NET_SFTP_QUEUE_SIZE) { if (!$this->_read_put_responses($i)) { return false; } $i = 0; } } } if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($path), $path, $attr))) { return false; } $i++; if ($i >= NET_SFTP_QUEUE_SIZE) { if (!$this->_read_put_responses($i)) { return false; } $i = 0; } return true; } /** * Return the target of a symbolic link * * @param string $link * @return mixed * @access public */ function readlink($link) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $link = $this->_realpath($link); if (!$this->_send_sftp_packet(NET_SFTP_READLINK, pack('Na*', strlen($link), $link))) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_NAME: break; case NET_SFTP_STATUS: $this->_logError($response); return false; default: user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS'); return false; } if (strlen($response) < 4) { return false; } extract(unpack('Ncount', $this->_string_shift($response, 4))); // the file isn't a symlink if (!$count) { return false; } if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); return $this->_string_shift($response, $length); } /** * Create a symlink * * symlink() creates a symbolic link to the existing target with the specified name link. * * @param string $target * @param string $link * @return bool * @access public */ function symlink($target, $link) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } //$target = $this->_realpath($target); $link = $this->_realpath($link); $packet = pack('Na*Na*', strlen($target), $target, strlen($link), $link); if (!$this->_send_sftp_packet(NET_SFTP_SYMLINK, $packet)) { return false; } $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { user_error('Expected SSH_FXP_STATUS'); return false; } if (strlen($response) < 4) { return false; } extract(unpack('Nstatus', $this->_string_shift($response, 4))); if ($status != NET_SFTP_STATUS_OK) { $this->_logError($response, $status); return false; } return true; } /** * Creates a directory. * * @param string $dir * @return bool * @access public */ function mkdir($dir, $mode = -1, $recursive = false) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $dir = $this->_realpath($dir); // by not providing any permissions, hopefully the server will use the logged in users umask - their // default permissions. $attr = $mode == -1 ? "\0\0\0\0" : pack('N2', NET_SFTP_ATTR_PERMISSIONS, $mode & 07777); if ($recursive) { $dirs = explode('/', preg_replace('#/(?=/)|/$#', '', $dir)); if (empty($dirs[0])) { array_shift($dirs); $dirs[0] = '/' . $dirs[0]; } for ($i = 0; $i < count($dirs); $i++) { $temp = array_slice($dirs, 0, $i + 1); $temp = implode('/', $temp); $result = $this->_mkdir_helper($temp, $attr); } return $result; } return $this->_mkdir_helper($dir, $attr); } /** * Helper function for directory creation * * @param string $dir * @return bool * @access private */ function _mkdir_helper($dir, $attr) { if (!$this->_send_sftp_packet(NET_SFTP_MKDIR, pack('Na*a*', strlen($dir), $dir, $attr))) { return false; } $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { user_error('Expected SSH_FXP_STATUS'); return false; } if (strlen($response) < 4) { return false; } extract(unpack('Nstatus', $this->_string_shift($response, 4))); if ($status != NET_SFTP_STATUS_OK) { $this->_logError($response, $status); return false; } return true; } /** * Removes a directory. * * @param string $dir * @return bool * @access public */ function rmdir($dir) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $dir = $this->_realpath($dir); if ($dir === false) { return false; } if (!$this->_send_sftp_packet(NET_SFTP_RMDIR, pack('Na*', strlen($dir), $dir))) { return false; } $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { user_error('Expected SSH_FXP_STATUS'); return false; } if (strlen($response) < 4) { return false; } extract(unpack('Nstatus', $this->_string_shift($response, 4))); if ($status != NET_SFTP_STATUS_OK) { // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED? $this->_logError($response, $status); return false; } $this->_remove_from_stat_cache($dir); // the following will do a soft delete, which would be useful if you deleted a file // and then tried to do a stat on the deleted file. the above, in contrast, does // a hard delete //$this->_update_stat_cache($dir, false); return true; } /** * Uploads a file to the SFTP server. * * By default, \phpseclib\Net\SFTP::put() does not read from the local filesystem. $data is dumped directly into $remote_file. * So, for example, if you set $data to 'filename.ext' and then do \phpseclib\Net\SFTP::get(), you will get a file, twelve bytes * long, containing 'filename.ext' as its contents. * * Setting $mode to self::SOURCE_LOCAL_FILE will change the above behavior. With self::SOURCE_LOCAL_FILE, $remote_file will * contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how * large $remote_file will be, as well. * * Setting $mode to self::SOURCE_CALLBACK will use $data as callback function, which gets only one parameter -- number of bytes to return, and returns a string if there is some data or null if there is no more data * * If $data is a resource then it'll be used as a resource instead. * * Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take * care of that, yourself. * * $mode can take an additional two parameters - self::RESUME and self::RESUME_START. These are bitwise AND'd with * $mode. So if you want to resume upload of a 300mb file on the local file system you'd set $mode to the following: * * self::SOURCE_LOCAL_FILE | self::RESUME * * If you wanted to simply append the full contents of a local file to the full contents of a remote file you'd replace * self::RESUME with self::RESUME_START. * * If $mode & (self::RESUME | self::RESUME_START) then self::RESUME_START will be assumed. * * $start and $local_start give you more fine grained control over this process and take precident over self::RESUME * when they're non-negative. ie. $start could let you write at the end of a file (like self::RESUME) or in the middle * of one. $local_start could let you start your reading from the end of a file (like self::RESUME_START) or in the * middle of one. * * Setting $local_start to > 0 or $mode | self::RESUME_START doesn't do anything unless $mode | self::SOURCE_LOCAL_FILE. * * @param string $remote_file * @param string|resource $data * @param int $mode * @param int $start * @param int $local_start * @param callable|null $progressCallback * @return bool * @access public * @internal ASCII mode for SFTPv4/5/6 can be supported by adding a new function - \phpseclib\Net\SFTP::setMode(). */ function put($remote_file, $data, $mode = self::SOURCE_STRING, $start = -1, $local_start = -1, $progressCallback = null) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $remote_file = $this->_realpath($remote_file); if ($remote_file === false) { return false; } $this->_remove_from_stat_cache($remote_file); $flags = NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE; // according to the SFTP specs, NET_SFTP_OPEN_APPEND should "force all writes to append data at the end of the file." // in practice, it doesn't seem to do that. //$flags|= ($mode & self::RESUME) ? NET_SFTP_OPEN_APPEND : NET_SFTP_OPEN_TRUNCATE; if ($start >= 0) { $offset = $start; } elseif ($mode & self::RESUME) { // if NET_SFTP_OPEN_APPEND worked as it should _size() wouldn't need to be called $size = $this->size($remote_file); $offset = $size !== false ? $size : 0; } else { $offset = 0; $flags|= NET_SFTP_OPEN_TRUNCATE; } $packet = pack('Na*N2', strlen($remote_file), $remote_file, $flags, 0); if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_HANDLE: $handle = substr($response, 4); break; case NET_SFTP_STATUS: $this->_logError($response); return false; default: user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3 $dataCallback = false; switch (true) { case $mode & self::SOURCE_CALLBACK: if (!is_callable($data)) { user_error("\$data should be is_callable() if you specify SOURCE_CALLBACK flag"); } $dataCallback = $data; // do nothing break; case is_resource($data): $mode = $mode & ~self::SOURCE_LOCAL_FILE; $info = stream_get_meta_data($data); if ($info['wrapper_type'] == 'PHP' && $info['stream_type'] == 'Input') { $fp = fopen('php://memory', 'w+'); stream_copy_to_stream($data, $fp); rewind($fp); } else { $fp = $data; } break; case $mode & self::SOURCE_LOCAL_FILE: if (!is_file($data)) { user_error("$data is not a valid file"); return false; } $fp = @fopen($data, 'rb'); if (!$fp) { return false; } } if (isset($fp)) { $stat = fstat($fp); $size = !empty($stat) ? $stat['size'] : 0; if ($local_start >= 0) { fseek($fp, $local_start); $size-= $local_start; } } elseif ($dataCallback) { $size = 0; } else { $size = strlen($data); } $sent = 0; $size = $size < 0 ? ($size & 0x7FFFFFFF) + 0x80000000 : $size; $sftp_packet_size = 4096; // PuTTY uses 4096 // make the SFTP packet be exactly 4096 bytes by including the bytes in the NET_SFTP_WRITE packets "header" $sftp_packet_size-= strlen($handle) + 25; $i = 0; while ($dataCallback || ($size === 0 || $sent < $size)) { if ($dataCallback) { $temp = call_user_func($dataCallback, $sftp_packet_size); if (is_null($temp)) { break; } } else { $temp = isset($fp) ? fread($fp, $sftp_packet_size) : substr($data, $sent, $sftp_packet_size); if ($temp === false || $temp === '') { break; } } $subtemp = $offset + $sent; $packet = pack('Na*N3a*', strlen($handle), $handle, $subtemp / 4294967296, $subtemp, strlen($temp), $temp); if (!$this->_send_sftp_packet(NET_SFTP_WRITE, $packet)) { if ($mode & self::SOURCE_LOCAL_FILE) { fclose($fp); } return false; } $sent+= strlen($temp); if (is_callable($progressCallback)) { call_user_func($progressCallback, $sent); } $i++; if ($i == NET_SFTP_QUEUE_SIZE) { if (!$this->_read_put_responses($i)) { $i = 0; break; } $i = 0; } } if (!$this->_read_put_responses($i)) { if ($mode & self::SOURCE_LOCAL_FILE) { fclose($fp); } $this->_close_handle($handle); return false; } if ($mode & self::SOURCE_LOCAL_FILE) { fclose($fp); } return $this->_close_handle($handle); } /** * Reads multiple successive SSH_FXP_WRITE responses * * Sending an SSH_FXP_WRITE packet and immediately reading its response isn't as efficient as blindly sending out $i * SSH_FXP_WRITEs, in succession, and then reading $i responses. * * @param int $i * @return bool * @access private */ function _read_put_responses($i) { while ($i--) { $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { user_error('Expected SSH_FXP_STATUS'); return false; } if (strlen($response) < 4) { return false; } extract(unpack('Nstatus', $this->_string_shift($response, 4))); if ($status != NET_SFTP_STATUS_OK) { $this->_logError($response, $status); break; } } return $i < 0; } /** * Close handle * * @param string $handle * @return bool * @access private */ function _close_handle($handle) { if (!$this->_send_sftp_packet(NET_SFTP_CLOSE, pack('Na*', strlen($handle), $handle))) { return false; } // "The client MUST release all resources associated with the handle regardless of the status." // -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.3 $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { user_error('Expected SSH_FXP_STATUS'); return false; } if (strlen($response) < 4) { return false; } extract(unpack('Nstatus', $this->_string_shift($response, 4))); if ($status != NET_SFTP_STATUS_OK) { $this->_logError($response, $status); return false; } return true; } /** * Downloads a file from the SFTP server. * * Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if * the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the * operation. * * $offset and $length can be used to download files in chunks. * * @param string $remote_file * @param string $local_file * @param int $offset * @param int $length * @param callable|null $progressCallback * @return mixed * @access public */ function get($remote_file, $local_file = false, $offset = 0, $length = -1, $progressCallback = null) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $remote_file = $this->_realpath($remote_file); if ($remote_file === false) { return false; } $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0); if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_HANDLE: $handle = substr($response, 4); break; case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED $this->_logError($response); return false; default: user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } if (is_resource($local_file)) { $fp = $local_file; $stat = fstat($fp); $res_offset = $stat['size']; } else { $res_offset = 0; if ($local_file !== false) { $fp = fopen($local_file, 'wb'); if (!$fp) { return false; } } else { $content = ''; } } $fclose_check = $local_file !== false && !is_resource($local_file); $start = $offset; $read = 0; while (true) { $i = 0; while ($i < NET_SFTP_QUEUE_SIZE && ($length < 0 || $read < $length)) { $tempoffset = $start + $read; $packet_size = $length > 0 ? min($this->max_sftp_packet, $length - $read) : $this->max_sftp_packet; $packet = pack('Na*N3', strlen($handle), $handle, $tempoffset / 4294967296, $tempoffset, $packet_size); if (!$this->_send_sftp_packet(NET_SFTP_READ, $packet, $i)) { if ($fclose_check) { fclose($fp); } return false; } $packet = null; $read+= $packet_size; if (is_callable($progressCallback)) { call_user_func($progressCallback, $read); } $i++; } if (!$i) { break; } $packets_sent = $i - 1; $clear_responses = false; while ($i > 0) { $i--; if ($clear_responses) { $this->_get_sftp_packet($packets_sent - $i); continue; } else { $response = $this->_get_sftp_packet($packets_sent - $i); } switch ($this->packet_type) { case NET_SFTP_DATA: $temp = substr($response, 4); $offset+= strlen($temp); if ($local_file === false) { $content.= $temp; } else { fputs($fp, $temp); } $temp = null; break; case NET_SFTP_STATUS: // could, in theory, return false if !strlen($content) but we'll hold off for the time being $this->_logError($response); $clear_responses = true; // don't break out of the loop yet, so we can read the remaining responses break; default: if ($fclose_check) { fclose($fp); } user_error('Expected SSH_FX_DATA or SSH_FXP_STATUS'); } $response = null; } if ($clear_responses) { break; } } if ($length > 0 && $length <= $offset - $start) { if ($local_file === false) { $content = substr($content, 0, $length); } else { ftruncate($fp, $length + $res_offset); } } if ($fclose_check) { fclose($fp); } if (!$this->_close_handle($handle)) { return false; } // if $content isn't set that means a file was written to return isset($content) ? $content : true; } /** * Deletes a file on the SFTP server. * * @param string $path * @param bool $recursive * @return bool * @access public */ function delete($path, $recursive = true) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } if (is_object($path)) { // It's an object. Cast it as string before we check anything else. $path = (string) $path; } if (!is_string($path) || $path == '') { return false; } $path = $this->_realpath($path); if ($path === false) { return false; } // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3 if (!$this->_send_sftp_packet(NET_SFTP_REMOVE, pack('Na*', strlen($path), $path))) { return false; } $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { user_error('Expected SSH_FXP_STATUS'); return false; } // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED if (strlen($response) < 4) { return false; } extract(unpack('Nstatus', $this->_string_shift($response, 4))); if ($status != NET_SFTP_STATUS_OK) { $this->_logError($response, $status); if (!$recursive) { return false; } $i = 0; $result = $this->_delete_recursive($path, $i); $this->_read_put_responses($i); return $result; } $this->_remove_from_stat_cache($path); return true; } /** * Recursively deletes directories on the SFTP server * * Minimizes directory lookups and SSH_FXP_STATUS requests for speed. * * @param string $path * @param int $i * @return bool * @access private */ function _delete_recursive($path, &$i) { if (!$this->_read_put_responses($i)) { return false; } $i = 0; $entries = $this->_list($path, true); // normally $entries would have at least . and .. but it might not if the directories // permissions didn't allow reading if (empty($entries)) { return false; } unset($entries['.'], $entries['..']); foreach ($entries as $filename => $props) { if (!isset($props['type'])) { return false; } $temp = $path . '/' . $filename; if ($props['type'] == NET_SFTP_TYPE_DIRECTORY) { if (!$this->_delete_recursive($temp, $i)) { return false; } } else { if (!$this->_send_sftp_packet(NET_SFTP_REMOVE, pack('Na*', strlen($temp), $temp))) { return false; } $this->_remove_from_stat_cache($temp); $i++; if ($i >= NET_SFTP_QUEUE_SIZE) { if (!$this->_read_put_responses($i)) { return false; } $i = 0; } } } if (!$this->_send_sftp_packet(NET_SFTP_RMDIR, pack('Na*', strlen($path), $path))) { return false; } $this->_remove_from_stat_cache($path); $i++; if ($i >= NET_SFTP_QUEUE_SIZE) { if (!$this->_read_put_responses($i)) { return false; } $i = 0; } return true; } /** * Checks whether a file or directory exists * * @param string $path * @return bool * @access public */ function file_exists($path) { if ($this->use_stat_cache) { $path = $this->_realpath($path); $result = $this->_query_stat_cache($path); if (isset($result)) { // return true if $result is an array or if it's an stdClass object return $result !== false; } } return $this->stat($path) !== false; } /** * Tells whether the filename is a directory * * @param string $path * @return bool * @access public */ function is_dir($path) { $result = $this->_get_stat_cache_prop($path, 'type'); if ($result === false) { return false; } return $result === NET_SFTP_TYPE_DIRECTORY; } /** * Tells whether the filename is a regular file * * @param string $path * @return bool * @access public */ function is_file($path) { $result = $this->_get_stat_cache_prop($path, 'type'); if ($result === false) { return false; } return $result === NET_SFTP_TYPE_REGULAR; } /** * Tells whether the filename is a symbolic link * * @param string $path * @return bool * @access public */ function is_link($path) { $result = $this->_get_lstat_cache_prop($path, 'type'); if ($result === false) { return false; } return $result === NET_SFTP_TYPE_SYMLINK; } /** * Tells whether a file exists and is readable * * @param string $path * @return bool * @access public */ function is_readable($path) { $path = $this->_realpath($path); $packet = pack('Na*N2', strlen($path), $path, NET_SFTP_OPEN_READ, 0); if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_HANDLE: return true; case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED return false; default: user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } } /** * Tells whether the filename is writable * * @param string $path * @return bool * @access public */ function is_writable($path) { $path = $this->_realpath($path); $packet = pack('Na*N2', strlen($path), $path, NET_SFTP_OPEN_WRITE, 0); if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) { return false; } $response = $this->_get_sftp_packet(); switch ($this->packet_type) { case NET_SFTP_HANDLE: return true; case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED return false; default: user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } } /** * Tells whether the filename is writeable * * Alias of is_writable * * @param string $path * @return bool * @access public */ function is_writeable($path) { return $this->is_writable($path); } /** * Gets last access time of file * * @param string $path * @return mixed * @access public */ function fileatime($path) { return $this->_get_stat_cache_prop($path, 'atime'); } /** * Gets file modification time * * @param string $path * @return mixed * @access public */ function filemtime($path) { return $this->_get_stat_cache_prop($path, 'mtime'); } /** * Gets file permissions * * @param string $path * @return mixed * @access public */ function fileperms($path) { return $this->_get_stat_cache_prop($path, 'permissions'); } /** * Gets file owner * * @param string $path * @return mixed * @access public */ function fileowner($path) { return $this->_get_stat_cache_prop($path, 'uid'); } /** * Gets file group * * @param string $path * @return mixed * @access public */ function filegroup($path) { return $this->_get_stat_cache_prop($path, 'gid'); } /** * Gets file size * * @param string $path * @return mixed * @access public */ function filesize($path) { return $this->_get_stat_cache_prop($path, 'size'); } /** * Gets file type * * @param string $path * @return mixed * @access public */ function filetype($path) { $type = $this->_get_stat_cache_prop($path, 'type'); if ($type === false) { return false; } switch ($type) { case NET_SFTP_TYPE_BLOCK_DEVICE: return 'block'; case NET_SFTP_TYPE_CHAR_DEVICE: return 'char'; case NET_SFTP_TYPE_DIRECTORY: return 'dir'; case NET_SFTP_TYPE_FIFO: return 'fifo'; case NET_SFTP_TYPE_REGULAR: return 'file'; case NET_SFTP_TYPE_SYMLINK: return 'link'; default: return false; } } /** * Return a stat properity * * Uses cache if appropriate. * * @param string $path * @param string $prop * @return mixed * @access private */ function _get_stat_cache_prop($path, $prop) { return $this->_get_xstat_cache_prop($path, $prop, 'stat'); } /** * Return an lstat properity * * Uses cache if appropriate. * * @param string $path * @param string $prop * @return mixed * @access private */ function _get_lstat_cache_prop($path, $prop) { return $this->_get_xstat_cache_prop($path, $prop, 'lstat'); } /** * Return a stat or lstat properity * * Uses cache if appropriate. * * @param string $path * @param string $prop * @return mixed * @access private */ function _get_xstat_cache_prop($path, $prop, $type) { if ($this->use_stat_cache) { $path = $this->_realpath($path); $result = $this->_query_stat_cache($path); if (is_object($result) && isset($result->$type)) { return $result->{$type}[$prop]; } } $result = $this->$type($path); if ($result === false || !isset($result[$prop])) { return false; } return $result[$prop]; } /** * Renames a file or a directory on the SFTP server * * @param string $oldname * @param string $newname * @return bool * @access public */ function rename($oldname, $newname) { if (!($this->bitmap & SSH2::MASK_LOGIN)) { return false; } $oldname = $this->_realpath($oldname); $newname = $this->_realpath($newname); if ($oldname === false || $newname === false) { return false; } // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3 $packet = pack('Na*Na*', strlen($oldname), $oldname, strlen($newname), $newname); if (!$this->_send_sftp_packet(NET_SFTP_RENAME, $packet)) { return false; } $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { user_error('Expected SSH_FXP_STATUS'); return false; } // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED if (strlen($response) < 4) { return false; } extract(unpack('Nstatus', $this->_string_shift($response, 4))); if ($status != NET_SFTP_STATUS_OK) { $this->_logError($response, $status); return false; } // don't move the stat cache entry over since this operation could very well change the // atime and mtime attributes //$this->_update_stat_cache($newname, $this->_query_stat_cache($oldname)); $this->_remove_from_stat_cache($oldname); $this->_remove_from_stat_cache($newname); return true; } /** * Parse Attributes * * See '7. File Attributes' of draft-ietf-secsh-filexfer-13 for more info. * * @param string $response * @return array * @access private */ function _parseAttributes(&$response) { $attr = array(); if (strlen($response) < 4) { user_error('Malformed file attributes'); return array(); } extract(unpack('Nflags', $this->_string_shift($response, 4))); // SFTPv4+ have a type field (a byte) that follows the above flag field foreach ($this->attributes as $key => $value) { switch ($flags & $key) { case NET_SFTP_ATTR_SIZE: // 0x00000001 // The size attribute is defined as an unsigned 64-bit integer. // The following will use floats on 32-bit platforms, if necessary. // As can be seen in the BigInteger class, floats are generally // IEEE 754 binary64 "double precision" on such platforms and // as such can represent integers of at least 2^50 without loss // of precision. Interpreted in filesize, 2^50 bytes = 1024 TiB. $attr['size'] = hexdec(bin2hex($this->_string_shift($response, 8))); break; case NET_SFTP_ATTR_UIDGID: // 0x00000002 (SFTPv3 only) if (strlen($response) < 8) { user_error('Malformed file attributes'); return $attr; } $attr+= unpack('Nuid/Ngid', $this->_string_shift($response, 8)); break; case NET_SFTP_ATTR_PERMISSIONS: // 0x00000004 if (strlen($response) < 4) { user_error('Malformed file attributes'); return $attr; } $attr+= unpack('Npermissions', $this->_string_shift($response, 4)); // mode == permissions; permissions was the original array key and is retained for bc purposes. // mode was added because that's the more industry standard terminology $attr+= array('mode' => $attr['permissions']); $fileType = $this->_parseMode($attr['permissions']); if ($fileType !== false) { $attr+= array('type' => $fileType); } break; case NET_SFTP_ATTR_ACCESSTIME: // 0x00000008 if (strlen($response) < 8) { user_error('Malformed file attributes'); return $attr; } $attr+= unpack('Natime/Nmtime', $this->_string_shift($response, 8)); break; case NET_SFTP_ATTR_EXTENDED: // 0x80000000 if (strlen($response) < 4) { user_error('Malformed file attributes'); return $attr; } extract(unpack('Ncount', $this->_string_shift($response, 4))); for ($i = 0; $i < $count; $i++) { if (strlen($response) < 4) { user_error('Malformed file attributes'); return $attr; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $key = $this->_string_shift($response, $length); if (strlen($response) < 4) { user_error('Malformed file attributes'); return $attr; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $attr[$key] = $this->_string_shift($response, $length); } } } return $attr; } /** * Attempt to identify the file type * * Quoting the SFTP RFC, "Implementations MUST NOT send bits that are not defined" but they seem to anyway * * @param int $mode * @return int * @access private */ function _parseMode($mode) { // values come from http://lxr.free-electrons.com/source/include/uapi/linux/stat.h#L12 // see, also, http://linux.die.net/man/2/stat switch ($mode & 0170000) {// ie. 1111 0000 0000 0000 case 0000000: // no file type specified - figure out the file type using alternative means return false; case 0040000: return NET_SFTP_TYPE_DIRECTORY; case 0100000: return NET_SFTP_TYPE_REGULAR; case 0120000: return NET_SFTP_TYPE_SYMLINK; // new types introduced in SFTPv5+ // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-05#section-5.2 case 0010000: // named pipe (fifo) return NET_SFTP_TYPE_FIFO; case 0020000: // character special return NET_SFTP_TYPE_CHAR_DEVICE; case 0060000: // block special return NET_SFTP_TYPE_BLOCK_DEVICE; case 0140000: // socket return NET_SFTP_TYPE_SOCKET; case 0160000: // whiteout // "SPECIAL should be used for files that are of // a known type which cannot be expressed in the protocol" return NET_SFTP_TYPE_SPECIAL; default: return NET_SFTP_TYPE_UNKNOWN; } } /** * Parse Longname * * SFTPv3 doesn't provide any easy way of identifying a file type. You could try to open * a file as a directory and see if an error is returned or you could try to parse the * SFTPv3-specific longname field of the SSH_FXP_NAME packet. That's what this function does. * The result is returned using the * {@link http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-5.2 SFTPv4 type constants}. * * If the longname is in an unrecognized format bool(false) is returned. * * @param string $longname * @return mixed * @access private */ function _parseLongname($longname) { // http://en.wikipedia.org/wiki/Unix_file_types // http://en.wikipedia.org/wiki/Filesystem_permissions#Notation_of_traditional_Unix_permissions if (preg_match('#^[^/]([r-][w-][xstST-]){3}#', $longname)) { switch ($longname[0]) { case '-': return NET_SFTP_TYPE_REGULAR; case 'd': return NET_SFTP_TYPE_DIRECTORY; case 'l': return NET_SFTP_TYPE_SYMLINK; default: return NET_SFTP_TYPE_SPECIAL; } } return false; } /** * Sends SFTP Packets * * See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info. * * @param int $type * @param string $data * @see self::_get_sftp_packet() * @see self::_send_channel_packet() * @return bool * @access private */ function _send_sftp_packet($type, $data, $request_id = 1) { $packet = $this->use_request_id ? pack('NCNa*', strlen($data) + 5, $type, $request_id, $data) : pack('NCa*', strlen($data) + 1, $type, $data); $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 $result = $this->_send_channel_packet(self::CHANNEL, $packet); $stop = strtok(microtime(), ' ') + strtok(''); if (defined('NET_SFTP_LOGGING')) { $packet_type = '-> ' . $this->packet_types[$type] . ' (' . round($stop - $start, 4) . 's)'; if (NET_SFTP_LOGGING == self::LOG_REALTIME) { echo "<pre>\r\n" . $this->_format_log(array($data), array($packet_type)) . "\r\n</pre>\r\n"; flush(); ob_flush(); } else { $this->packet_type_log[] = $packet_type; if (NET_SFTP_LOGGING == self::LOG_COMPLEX) { $this->packet_log[] = $data; } } } return $result; } /** * Resets a connection for re-use * * @param int $reason * @access private */ function _reset_connection($reason) { parent::_reset_connection($reason); $this->use_request_id = false; $this->pwd = false; $this->requestBuffer = array(); } /** * Receives SFTP Packets * * See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info. * * Incidentally, the number of SSH_MSG_CHANNEL_DATA messages has no bearing on the number of SFTP packets present. * There can be one SSH_MSG_CHANNEL_DATA messages containing two SFTP packets or there can be two SSH_MSG_CHANNEL_DATA * messages containing one SFTP packet. * * @see self::_send_sftp_packet() * @return string * @access private */ function _get_sftp_packet($request_id = null) { if (isset($request_id) && isset($this->requestBuffer[$request_id])) { $this->packet_type = $this->requestBuffer[$request_id]['packet_type']; $temp = $this->requestBuffer[$request_id]['packet']; unset($this->requestBuffer[$request_id]); return $temp; } // in SSH2.php the timeout is cumulative per function call. eg. exec() will // timeout after 10s. but for SFTP.php it's cumulative per packet $this->curTimeout = $this->timeout; $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 // SFTP packet length while (strlen($this->packet_buffer) < 4) { $temp = $this->_get_channel_packet(self::CHANNEL, true); if (is_bool($temp)) { $this->packet_type = false; $this->packet_buffer = ''; return false; } $this->packet_buffer.= $temp; } if (strlen($this->packet_buffer) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($this->packet_buffer, 4))); $tempLength = $length; $tempLength-= strlen($this->packet_buffer); // 256 * 1024 is what SFTP_MAX_MSG_LENGTH is set to in OpenSSH's sftp-common.h if ($tempLength > 256 * 1024) { user_error('Invalid SFTP packet size'); return false; } // SFTP packet type and data payload while ($tempLength > 0) { $temp = $this->_get_channel_packet(self::CHANNEL, true); if (is_bool($temp)) { $this->packet_type = false; $this->packet_buffer = ''; return false; } $this->packet_buffer.= $temp; $tempLength-= strlen($temp); } $stop = strtok(microtime(), ' ') + strtok(''); $this->packet_type = ord($this->_string_shift($this->packet_buffer)); if ($this->use_request_id) { extract(unpack('Npacket_id', $this->_string_shift($this->packet_buffer, 4))); // remove the request id $length-= 5; // account for the request id and the packet type } else { $length-= 1; // account for the packet type } $packet = $this->_string_shift($this->packet_buffer, $length); if (defined('NET_SFTP_LOGGING')) { $packet_type = '<- ' . $this->packet_types[$this->packet_type] . ' (' . round($stop - $start, 4) . 's)'; if (NET_SFTP_LOGGING == self::LOG_REALTIME) { echo "<pre>\r\n" . $this->_format_log(array($packet), array($packet_type)) . "\r\n</pre>\r\n"; flush(); ob_flush(); } else { $this->packet_type_log[] = $packet_type; if (NET_SFTP_LOGGING == self::LOG_COMPLEX) { $this->packet_log[] = $packet; } } } if (isset($request_id) && $this->use_request_id && $packet_id != $request_id) { $this->requestBuffer[$packet_id] = array( 'packet_type' => $this->packet_type, 'packet' => $packet ); return $this->_get_sftp_packet($request_id); } return $packet; } /** * Returns a log of the packets that have been sent and received. * * Returns a string if NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX, an array if NET_SFTP_LOGGING == NET_SFTP_LOG_SIMPLE and false if !defined('NET_SFTP_LOGGING') * * @access public * @return string or Array */ function getSFTPLog() { if (!defined('NET_SFTP_LOGGING')) { return false; } switch (NET_SFTP_LOGGING) { case self::LOG_COMPLEX: return $this->_format_log($this->packet_log, $this->packet_type_log); break; //case self::LOG_SIMPLE: default: return $this->packet_type_log; } } /** * Returns all errors * * @return array * @access public */ function getSFTPErrors() { return $this->sftp_errors; } /** * Returns the last error * * @return string * @access public */ function getLastSFTPError() { return count($this->sftp_errors) ? $this->sftp_errors[count($this->sftp_errors) - 1] : ''; } /** * Get supported SFTP versions * * @return array * @access public */ function getSupportedVersions() { $temp = array('version' => $this->version); if (isset($this->extensions['versions'])) { $temp['extensions'] = $this->extensions['versions']; } return $temp; } /** * Disconnect * * @param int $reason * @return bool * @access private */ function _disconnect($reason) { $this->pwd = false; parent::_disconnect($reason); } } <?php /** * Pure-PHP implementation of SSHv1. * * PHP version 5 * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $ssh = new \phpseclib\Net\SSH1('www.domain.tld'); * if (!$ssh->login('username', 'password')) { * exit('Login Failed'); * } * * echo $ssh->exec('ls -la'); * ?> * </code> * * Here's another short example: * <code> * <?php * include 'vendor/autoload.php'; * * $ssh = new \phpseclib\Net\SSH1('www.domain.tld'); * if (!$ssh->login('username', 'password')) { * exit('Login Failed'); * } * * echo $ssh->read('username@username:~$'); * $ssh->write("ls -la\n"); * echo $ssh->read('username@username:~$'); * ?> * </code> * * More information on the SSHv1 specification can be found by reading * {@link http://www.snailbook.com/docs/protocol-1.5.txt protocol-1.5.txt}. * * @category Net * @package SSH1 * @author Jim Wigginton <terrafrost@php.net> * @copyright 2007 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Net; use phpseclib\Crypt\DES; use phpseclib\Crypt\Random; use phpseclib\Crypt\TripleDES; use phpseclib\Math\BigInteger; /** * Pure-PHP implementation of SSHv1. * * @package SSH1 * @author Jim Wigginton <terrafrost@php.net> * @access public */ class SSH1 { /**#@+ * Encryption Methods * * @see \phpseclib\Net\SSH1::getSupportedCiphers() * @access public */ /** * No encryption * * Not supported. */ const CIPHER_NONE = 0; /** * IDEA in CFB mode * * Not supported. */ const CIPHER_IDEA = 1; /** * DES in CBC mode */ const CIPHER_DES = 2; /** * Triple-DES in CBC mode * * All implementations are required to support this */ const CIPHER_3DES = 3; /** * TRI's Simple Stream encryption CBC * * Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, does define it (see cipher.h), * although it doesn't use it (see cipher.c) */ const CIPHER_BROKEN_TSS = 4; /** * RC4 * * Not supported. * * @internal According to the SSH1 specs: * * "The first 16 bytes of the session key are used as the key for * the server to client direction. The remaining 16 bytes are used * as the key for the client to server direction. This gives * independent 128-bit keys for each direction." * * This library currently only supports encryption when the same key is being used for both directions. This is * because there's only one $crypto object. Two could be added ($encrypt and $decrypt, perhaps). */ const CIPHER_RC4 = 5; /** * Blowfish * * Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, defines it (see cipher.h) and * uses it (see cipher.c) */ const CIPHER_BLOWFISH = 6; /**#@-*/ /**#@+ * Authentication Methods * * @see \phpseclib\Net\SSH1::getSupportedAuthentications() * @access public */ /** * .rhosts or /etc/hosts.equiv */ const AUTH_RHOSTS = 1; /** * pure RSA authentication */ const AUTH_RSA = 2; /** * password authentication * * This is the only method that is supported by this library. */ const AUTH_PASSWORD = 3; /** * .rhosts with RSA host authentication */ const AUTH_RHOSTS_RSA = 4; /**#@-*/ /**#@+ * Terminal Modes * * @link http://3sp.com/content/developer/maverick-net/docs/Maverick.SSH.PseudoTerminalModesMembers.html * @access private */ const TTY_OP_END = 0; /**#@-*/ /** * The Response Type * * @see \phpseclib\Net\SSH1::_get_binary_packet() * @access private */ const RESPONSE_TYPE = 1; /** * The Response Data * * @see \phpseclib\Net\SSH1::_get_binary_packet() * @access private */ const RESPONSE_DATA = 2; /**#@+ * Execution Bitmap Masks * * @see \phpseclib\Net\SSH1::bitmap * @access private */ const MASK_CONSTRUCTOR = 0x00000001; const MASK_CONNECTED = 0x00000002; const MASK_LOGIN = 0x00000004; const MASK_SHELL = 0x00000008; /**#@-*/ /**#@+ * @access public * @see \phpseclib\Net\SSH1::getLog() */ /** * Returns the message numbers */ const LOG_SIMPLE = 1; /** * Returns the message content */ const LOG_COMPLEX = 2; /** * Outputs the content real-time */ const LOG_REALTIME = 3; /** * Dumps the content real-time to a file */ const LOG_REALTIME_FILE = 4; /**#@-*/ /**#@+ * @access public * @see \phpseclib\Net\SSH1::read() */ /** * Returns when a string matching $expect exactly is found */ const READ_SIMPLE = 1; /** * Returns when a string matching the regular expression $expect is found */ const READ_REGEX = 2; /**#@-*/ /** * The SSH identifier * * @var string * @access private */ var $identifier = 'SSH-1.5-phpseclib'; /** * The Socket Object * * @var object * @access private */ var $fsock; /** * The cryptography object * * @var object * @access private */ var $crypto = false; /** * Execution Bitmap * * The bits that are set represent functions that have been called already. This is used to determine * if a requisite function has been successfully executed. If not, an error should be thrown. * * @var int * @access private */ var $bitmap = 0; /** * The Server Key Public Exponent * * Logged for debug purposes * * @see self::getServerKeyPublicExponent() * @var string * @access private */ var $server_key_public_exponent; /** * The Server Key Public Modulus * * Logged for debug purposes * * @see self::getServerKeyPublicModulus() * @var string * @access private */ var $server_key_public_modulus; /** * The Host Key Public Exponent * * Logged for debug purposes * * @see self::getHostKeyPublicExponent() * @var string * @access private */ var $host_key_public_exponent; /** * The Host Key Public Modulus * * Logged for debug purposes * * @see self::getHostKeyPublicModulus() * @var string * @access private */ var $host_key_public_modulus; /** * Supported Ciphers * * Logged for debug purposes * * @see self::getSupportedCiphers() * @var array * @access private */ var $supported_ciphers = array( self::CIPHER_NONE => 'No encryption', self::CIPHER_IDEA => 'IDEA in CFB mode', self::CIPHER_DES => 'DES in CBC mode', self::CIPHER_3DES => 'Triple-DES in CBC mode', self::CIPHER_BROKEN_TSS => 'TRI\'s Simple Stream encryption CBC', self::CIPHER_RC4 => 'RC4', self::CIPHER_BLOWFISH => 'Blowfish' ); /** * Supported Authentications * * Logged for debug purposes * * @see self::getSupportedAuthentications() * @var array * @access private */ var $supported_authentications = array( self::AUTH_RHOSTS => '.rhosts or /etc/hosts.equiv', self::AUTH_RSA => 'pure RSA authentication', self::AUTH_PASSWORD => 'password authentication', self::AUTH_RHOSTS_RSA => '.rhosts with RSA host authentication' ); /** * Server Identification * * @see self::getServerIdentification() * @var string * @access private */ var $server_identification = ''; /** * Protocol Flags * * @see self::__construct() * @var array * @access private */ var $protocol_flags = array(); /** * Protocol Flag Log * * @see self::getLog() * @var array * @access private */ var $protocol_flag_log = array(); /** * Message Log * * @see self::getLog() * @var array * @access private */ var $message_log = array(); /** * Real-time log file pointer * * @see self::_append_log() * @var resource * @access private */ var $realtime_log_file; /** * Real-time log file size * * @see self::_append_log() * @var int * @access private */ var $realtime_log_size; /** * Real-time log file wrap boolean * * @see self::_append_log() * @var bool * @access private */ var $realtime_log_wrap; /** * Interactive Buffer * * @see self::read() * @var array * @access private */ var $interactiveBuffer = ''; /** * Timeout * * @see self::setTimeout() * @access private */ var $timeout; /** * Current Timeout * * @see self::_get_channel_packet() * @access private */ var $curTimeout; /** * Log Boundary * * @see self::_format_log() * @access private */ var $log_boundary = ':'; /** * Log Long Width * * @see self::_format_log() * @access private */ var $log_long_width = 65; /** * Log Short Width * * @see self::_format_log() * @access private */ var $log_short_width = 16; /** * Hostname * * @see self::__construct() * @see self::_connect() * @var string * @access private */ var $host; /** * Port Number * * @see self::__construct() * @see self::_connect() * @var int * @access private */ var $port; /** * Timeout for initial connection * * Set by the constructor call. Calling setTimeout() is optional. If it's not called functions like * exec() won't timeout unless some PHP setting forces it too. The timeout specified in the constructor, * however, is non-optional. There will be a timeout, whether or not you set it. If you don't it'll be * 10 seconds. It is used by fsockopen() in that function. * * @see self::__construct() * @see self::_connect() * @var int * @access private */ var $connectionTimeout; /** * Default cipher * * @see self::__construct() * @see self::_connect() * @var int * @access private */ var $cipher; /** * Default Constructor. * * Connects to an SSHv1 server * * @param string $host * @param int $port * @param int $timeout * @param int $cipher * @return \phpseclib\Net\SSH1 * @access public */ function __construct($host, $port = 22, $timeout = 10, $cipher = self::CIPHER_3DES) { $this->protocol_flags = array( 1 => 'NET_SSH1_MSG_DISCONNECT', 2 => 'NET_SSH1_SMSG_PUBLIC_KEY', 3 => 'NET_SSH1_CMSG_SESSION_KEY', 4 => 'NET_SSH1_CMSG_USER', 9 => 'NET_SSH1_CMSG_AUTH_PASSWORD', 10 => 'NET_SSH1_CMSG_REQUEST_PTY', 12 => 'NET_SSH1_CMSG_EXEC_SHELL', 13 => 'NET_SSH1_CMSG_EXEC_CMD', 14 => 'NET_SSH1_SMSG_SUCCESS', 15 => 'NET_SSH1_SMSG_FAILURE', 16 => 'NET_SSH1_CMSG_STDIN_DATA', 17 => 'NET_SSH1_SMSG_STDOUT_DATA', 18 => 'NET_SSH1_SMSG_STDERR_DATA', 19 => 'NET_SSH1_CMSG_EOF', 20 => 'NET_SSH1_SMSG_EXITSTATUS', 33 => 'NET_SSH1_CMSG_EXIT_CONFIRMATION' ); $this->_define_array($this->protocol_flags); $this->host = $host; $this->port = $port; $this->connectionTimeout = $timeout; $this->cipher = $cipher; } /** * Connect to an SSHv1 server * * @return bool * @access private */ function _connect() { $this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->connectionTimeout); if (!$this->fsock) { user_error(rtrim("Cannot connect to {$this->host}:{$this->port}. Error $errno. $errstr")); return false; } $this->server_identification = $init_line = fgets($this->fsock, 255); if (defined('NET_SSH1_LOGGING')) { $this->_append_log('<-', $this->server_identification); $this->_append_log('->', $this->identifier . "\r\n"); } if (!preg_match('#SSH-([0-9\.]+)-(.+)#', $init_line, $parts)) { user_error('Can only connect to SSH servers'); return false; } if ($parts[1][0] != 1) { user_error("Cannot connect to SSH $parts[1] servers"); return false; } fputs($this->fsock, $this->identifier."\r\n"); $response = $this->_get_binary_packet(); if ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) { user_error('Expected SSH_SMSG_PUBLIC_KEY'); return false; } $anti_spoofing_cookie = $this->_string_shift($response[self::RESPONSE_DATA], 8); $this->_string_shift($response[self::RESPONSE_DATA], 4); if (strlen($response[self::RESPONSE_DATA]) < 2) { return false; } $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2)); $server_key_public_exponent = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $this->server_key_public_exponent = $server_key_public_exponent; if (strlen($response[self::RESPONSE_DATA]) < 2) { return false; } $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2)); $server_key_public_modulus = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $this->server_key_public_modulus = $server_key_public_modulus; $this->_string_shift($response[self::RESPONSE_DATA], 4); if (strlen($response[self::RESPONSE_DATA]) < 2) { return false; } $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2)); $host_key_public_exponent = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $this->host_key_public_exponent = $host_key_public_exponent; if (strlen($response[self::RESPONSE_DATA]) < 2) { return false; } $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2)); $host_key_public_modulus = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $this->host_key_public_modulus = $host_key_public_modulus; $this->_string_shift($response[self::RESPONSE_DATA], 4); // get a list of the supported ciphers if (strlen($response[self::RESPONSE_DATA]) < 4) { return false; } extract(unpack('Nsupported_ciphers_mask', $this->_string_shift($response[self::RESPONSE_DATA], 4))); foreach ($this->supported_ciphers as $mask => $name) { if (($supported_ciphers_mask & (1 << $mask)) == 0) { unset($this->supported_ciphers[$mask]); } } // get a list of the supported authentications if (strlen($response[self::RESPONSE_DATA]) < 4) { return false; } extract(unpack('Nsupported_authentications_mask', $this->_string_shift($response[self::RESPONSE_DATA], 4))); foreach ($this->supported_authentications as $mask => $name) { if (($supported_authentications_mask & (1 << $mask)) == 0) { unset($this->supported_authentications[$mask]); } } $session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie)); $session_key = Random::string(32); $double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0)); if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) { $double_encrypted_session_key = $this->_rsa_crypt( $double_encrypted_session_key, array( $server_key_public_exponent, $server_key_public_modulus ) ); $double_encrypted_session_key = $this->_rsa_crypt( $double_encrypted_session_key, array( $host_key_public_exponent, $host_key_public_modulus ) ); } else { $double_encrypted_session_key = $this->_rsa_crypt( $double_encrypted_session_key, array( $host_key_public_exponent, $host_key_public_modulus ) ); $double_encrypted_session_key = $this->_rsa_crypt( $double_encrypted_session_key, array( $server_key_public_exponent, $server_key_public_modulus ) ); } $cipher = isset($this->supported_ciphers[$this->cipher]) ? $this->cipher : self::CIPHER_3DES; $data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0); if (!$this->_send_binary_packet($data)) { user_error('Error sending SSH_CMSG_SESSION_KEY'); return false; } switch ($cipher) { //case self::CIPHER_NONE: // $this->crypto = new \phpseclib\Crypt\Null(); // break; case self::CIPHER_DES: $this->crypto = new DES(); $this->crypto->disablePadding(); $this->crypto->enableContinuousBuffer(); $this->crypto->setKey(substr($session_key, 0, 8)); break; case self::CIPHER_3DES: $this->crypto = new TripleDES(TripleDES::MODE_3CBC); $this->crypto->disablePadding(); $this->crypto->enableContinuousBuffer(); $this->crypto->setKey(substr($session_key, 0, 24)); break; //case self::CIPHER_RC4: // $this->crypto = new RC4(); // $this->crypto->enableContinuousBuffer(); // $this->crypto->setKey(substr($session_key, 0, 16)); // break; } $response = $this->_get_binary_packet(); if ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) { user_error('Expected SSH_SMSG_SUCCESS'); return false; } $this->bitmap = self::MASK_CONNECTED; return true; } /** * Login * * @param string $username * @param string $password * @return bool * @access public */ function login($username, $password = '') { if (!($this->bitmap & self::MASK_CONSTRUCTOR)) { $this->bitmap |= self::MASK_CONSTRUCTOR; if (!$this->_connect()) { return false; } } if (!($this->bitmap & self::MASK_CONNECTED)) { return false; } $data = pack('CNa*', NET_SSH1_CMSG_USER, strlen($username), $username); if (!$this->_send_binary_packet($data)) { user_error('Error sending SSH_CMSG_USER'); return false; } $response = $this->_get_binary_packet(); if ($response === true) { return false; } if ($response[self::RESPONSE_TYPE] == NET_SSH1_SMSG_SUCCESS) { $this->bitmap |= self::MASK_LOGIN; return true; } elseif ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_FAILURE) { user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE'); return false; } $data = pack('CNa*', NET_SSH1_CMSG_AUTH_PASSWORD, strlen($password), $password); if (!$this->_send_binary_packet($data)) { user_error('Error sending SSH_CMSG_AUTH_PASSWORD'); return false; } // remove the username and password from the last logged packet if (defined('NET_SSH1_LOGGING') && NET_SSH1_LOGGING == self::LOG_COMPLEX) { $data = pack('CNa*', NET_SSH1_CMSG_AUTH_PASSWORD, strlen('password'), 'password'); $this->message_log[count($this->message_log) - 1] = $data; } $response = $this->_get_binary_packet(); if ($response === true) { return false; } if ($response[self::RESPONSE_TYPE] == NET_SSH1_SMSG_SUCCESS) { $this->bitmap |= self::MASK_LOGIN; return true; } elseif ($response[self::RESPONSE_TYPE] == NET_SSH1_SMSG_FAILURE) { return false; } else { user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE'); return false; } } /** * Set Timeout * * $ssh->exec('ping 127.0.0.1'); on a Linux host will never return and will run indefinitely. setTimeout() makes it so it'll timeout. * Setting $timeout to false or 0 will mean there is no timeout. * * @param mixed $timeout */ function setTimeout($timeout) { $this->timeout = $this->curTimeout = $timeout; } /** * Executes a command on a non-interactive shell, returns the output, and quits. * * An SSH1 server will close the connection after a command has been executed on a non-interactive shell. SSH2 * servers don't, however, this isn't an SSH2 client. The way this works, on the server, is by initiating a * shell with the -s option, as discussed in the following links: * * {@link http://www.faqs.org/docs/bashman/bashref_65.html http://www.faqs.org/docs/bashman/bashref_65.html} * {@link http://www.faqs.org/docs/bashman/bashref_62.html http://www.faqs.org/docs/bashman/bashref_62.html} * * To execute further commands, a new \phpseclib\Net\SSH1 object will need to be created. * * Returns false on failure and the output, otherwise. * * @see self::interactiveRead() * @see self::interactiveWrite() * @param string $cmd * @return mixed * @access public */ function exec($cmd, $block = true) { if (!($this->bitmap & self::MASK_LOGIN)) { user_error('Operation disallowed prior to login()'); return false; } $data = pack('CNa*', NET_SSH1_CMSG_EXEC_CMD, strlen($cmd), $cmd); if (!$this->_send_binary_packet($data)) { user_error('Error sending SSH_CMSG_EXEC_CMD'); return false; } if (!$block) { return true; } $output = ''; $response = $this->_get_binary_packet(); if ($response !== false) { do { $output.= substr($response[self::RESPONSE_DATA], 4); $response = $this->_get_binary_packet(); } while (is_array($response) && $response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_EXITSTATUS); } $data = pack('C', NET_SSH1_CMSG_EXIT_CONFIRMATION); // i don't think it's really all that important if this packet gets sent or not. $this->_send_binary_packet($data); fclose($this->fsock); // reset the execution bitmap - a new \phpseclib\Net\SSH1 object needs to be created. $this->bitmap = 0; return $output; } /** * Creates an interactive shell * * @see self::interactiveRead() * @see self::interactiveWrite() * @return bool * @access private */ function _initShell() { // connect using the sample parameters in protocol-1.5.txt. // according to wikipedia.org's entry on text terminals, "the fundamental type of application running on a text // terminal is a command line interpreter or shell". thus, opening a terminal session to run the shell. $data = pack('CNa*N4C', NET_SSH1_CMSG_REQUEST_PTY, strlen('vt100'), 'vt100', 24, 80, 0, 0, self::TTY_OP_END); if (!$this->_send_binary_packet($data)) { user_error('Error sending SSH_CMSG_REQUEST_PTY'); return false; } $response = $this->_get_binary_packet(); if ($response === true) { return false; } if ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) { user_error('Expected SSH_SMSG_SUCCESS'); return false; } $data = pack('C', NET_SSH1_CMSG_EXEC_SHELL); if (!$this->_send_binary_packet($data)) { user_error('Error sending SSH_CMSG_EXEC_SHELL'); return false; } $this->bitmap |= self::MASK_SHELL; //stream_set_blocking($this->fsock, 0); return true; } /** * Inputs a command into an interactive shell. * * @see self::interactiveWrite() * @param string $cmd * @return bool * @access public */ function write($cmd) { return $this->interactiveWrite($cmd); } /** * Returns the output of an interactive shell when there's a match for $expect * * $expect can take the form of a string literal or, if $mode == self::READ_REGEX, * a regular expression. * * @see self::write() * @param string $expect * @param int $mode * @return bool * @access public */ function read($expect, $mode = self::READ_SIMPLE) { if (!($this->bitmap & self::MASK_LOGIN)) { user_error('Operation disallowed prior to login()'); return false; } if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) { user_error('Unable to initiate an interactive shell session'); return false; } $match = $expect; while (true) { if ($mode == self::READ_REGEX) { preg_match($expect, $this->interactiveBuffer, $matches); $match = isset($matches[0]) ? $matches[0] : ''; } $pos = strlen($match) ? strpos($this->interactiveBuffer, $match) : false; if ($pos !== false) { return $this->_string_shift($this->interactiveBuffer, $pos + strlen($match)); } $response = $this->_get_binary_packet(); if ($response === true) { return $this->_string_shift($this->interactiveBuffer, strlen($this->interactiveBuffer)); } $this->interactiveBuffer.= substr($response[self::RESPONSE_DATA], 4); } } /** * Inputs a command into an interactive shell. * * @see self::interactiveRead() * @param string $cmd * @return bool * @access public */ function interactiveWrite($cmd) { if (!($this->bitmap & self::MASK_LOGIN)) { user_error('Operation disallowed prior to login()'); return false; } if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) { user_error('Unable to initiate an interactive shell session'); return false; } $data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($cmd), $cmd); if (!$this->_send_binary_packet($data)) { user_error('Error sending SSH_CMSG_STDIN'); return false; } return true; } /** * Returns the output of an interactive shell when no more output is available. * * Requires PHP 4.3.0 or later due to the use of the stream_select() function. If you see stuff like * "^[[00m", you're seeing ANSI escape codes. According to * {@link http://support.microsoft.com/kb/101875 How to Enable ANSI.SYS in a Command Window}, "Windows NT * does not support ANSI escape sequences in Win32 Console applications", so if you're a Windows user, * there's not going to be much recourse. * * @see self::interactiveRead() * @return string * @access public */ function interactiveRead() { if (!($this->bitmap & self::MASK_LOGIN)) { user_error('Operation disallowed prior to login()'); return false; } if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) { user_error('Unable to initiate an interactive shell session'); return false; } $read = array($this->fsock); $write = $except = null; if (stream_select($read, $write, $except, 0)) { $response = $this->_get_binary_packet(); return substr($response[self::RESPONSE_DATA], 4); } else { return ''; } } /** * Disconnect * * @access public */ function disconnect() { $this->_disconnect(); } /** * Destructor. * * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call * disconnect(). * * @access public */ function __destruct() { $this->_disconnect(); } /** * Disconnect * * @param string $msg * @access private */ function _disconnect($msg = 'Client Quit') { if ($this->bitmap) { $data = pack('C', NET_SSH1_CMSG_EOF); $this->_send_binary_packet($data); /* $response = $this->_get_binary_packet(); if ($response === true) { $response = array(self::RESPONSE_TYPE => -1); } switch ($response[self::RESPONSE_TYPE]) { case NET_SSH1_SMSG_EXITSTATUS: $data = pack('C', NET_SSH1_CMSG_EXIT_CONFIRMATION); break; default: $data = pack('CNa*', NET_SSH1_MSG_DISCONNECT, strlen($msg), $msg); } */ $data = pack('CNa*', NET_SSH1_MSG_DISCONNECT, strlen($msg), $msg); $this->_send_binary_packet($data); fclose($this->fsock); $this->bitmap = 0; } } /** * Gets Binary Packets * * See 'The Binary Packet Protocol' of protocol-1.5.txt for more info. * * Also, this function could be improved upon by adding detection for the following exploit: * http://www.securiteam.com/securitynews/5LP042K3FY.html * * @see self::_send_binary_packet() * @return array * @access private */ function _get_binary_packet() { if (feof($this->fsock)) { //user_error('connection closed prematurely'); return false; } if ($this->curTimeout) { $read = array($this->fsock); $write = $except = null; $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 $sec = floor($this->curTimeout); $usec = 1000000 * ($this->curTimeout - $sec); // on windows this returns a "Warning: Invalid CRT parameters detected" error if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) { //$this->_disconnect('Timeout'); return true; } $elapsed = strtok(microtime(), ' ') + strtok('') - $start; $this->curTimeout-= $elapsed; } $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 $data = fread($this->fsock, 4); if (strlen($data) < 4) { return false; } $temp = unpack('Nlength', $data); $padding_length = 8 - ($temp['length'] & 7); $length = $temp['length'] + $padding_length; $raw = ''; while ($length > 0) { $temp = fread($this->fsock, $length); $raw.= $temp; $length-= strlen($temp); } $stop = strtok(microtime(), ' ') + strtok(''); if (strlen($raw) && $this->crypto !== false) { $raw = $this->crypto->decrypt($raw); } $padding = substr($raw, 0, $padding_length); $type = $raw[$padding_length]; $data = substr($raw, $padding_length + 1, -4); if (strlen($raw) < 4) { return false; } $temp = unpack('Ncrc', substr($raw, -4)); //if ( $temp['crc'] != $this->_crc($padding . $type . $data) ) { // user_error('Bad CRC in packet from server'); // return false; //} $type = ord($type); if (defined('NET_SSH1_LOGGING')) { $temp = isset($this->protocol_flags[$type]) ? $this->protocol_flags[$type] : 'UNKNOWN'; $temp = '<- ' . $temp . ' (' . round($stop - $start, 4) . 's)'; $this->_append_log($temp, $data); } return array( self::RESPONSE_TYPE => $type, self::RESPONSE_DATA => $data ); } /** * Sends Binary Packets * * Returns true on success, false on failure. * * @see self::_get_binary_packet() * @param string $data * @return bool * @access private */ function _send_binary_packet($data) { if (feof($this->fsock)) { //user_error('connection closed prematurely'); return false; } $length = strlen($data) + 4; $padding = Random::string(8 - ($length & 7)); $orig = $data; $data = $padding . $data; $data.= pack('N', $this->_crc($data)); if ($this->crypto !== false) { $data = $this->crypto->encrypt($data); } $packet = pack('Na*', $length, $data); $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 $result = strlen($packet) == fputs($this->fsock, $packet); $stop = strtok(microtime(), ' ') + strtok(''); if (defined('NET_SSH1_LOGGING')) { $temp = isset($this->protocol_flags[ord($orig[0])]) ? $this->protocol_flags[ord($orig[0])] : 'UNKNOWN'; $temp = '-> ' . $temp . ' (' . round($stop - $start, 4) . 's)'; $this->_append_log($temp, $orig); } return $result; } /** * Cyclic Redundancy Check (CRC) * * PHP's crc32 function is implemented slightly differently than the one that SSH v1 uses, so * we've reimplemented it. A more detailed discussion of the differences can be found after * $crc_lookup_table's initialization. * * @see self::_get_binary_packet() * @see self::_send_binary_packet() * @param string $data * @return int * @access private */ function _crc($data) { static $crc_lookup_table = array( 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D ); // For this function to yield the same output as PHP's crc32 function, $crc would have to be // set to 0xFFFFFFFF, initially - not 0x00000000 as it currently is. $crc = 0x00000000; $length = strlen($data); for ($i=0; $i<$length; $i++) { // We AND $crc >> 8 with 0x00FFFFFF because we want the eight newly added bits to all // be zero. PHP, unfortunately, doesn't always do this. 0x80000000 >> 8, as an example, // yields 0xFF800000 - not 0x00800000. The following link elaborates: // http://www.php.net/manual/en/language.operators.bitwise.php#57281 $crc = (($crc >> 8) & 0x00FFFFFF) ^ $crc_lookup_table[($crc & 0xFF) ^ ord($data[$i])]; } // In addition to having to set $crc to 0xFFFFFFFF, initially, the return value must be XOR'd with // 0xFFFFFFFF for this function to return the same thing that PHP's crc32 function would. return $crc; } /** * String Shift * * Inspired by array_shift * * @param string $string * @param int $index * @return string * @access private */ function _string_shift(&$string, $index = 1) { $substr = substr($string, 0, $index); $string = substr($string, $index); return $substr; } /** * RSA Encrypt * * Returns mod(pow($m, $e), $n), where $n should be the product of two (large) primes $p and $q and where $e * should be a number with the property that gcd($e, ($p - 1) * ($q - 1)) == 1. Could just make anything that * calls this call modexp, instead, but I think this makes things clearer, maybe... * * @see self::__construct() * @param BigInteger $m * @param array $key * @return BigInteger * @access private */ function _rsa_crypt($m, $key) { /* $rsa = new RSA(); $rsa->loadKey($key, RSA::PUBLIC_FORMAT_RAW); $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); return $rsa->encrypt($m); */ // To quote from protocol-1.5.txt: // The most significant byte (which is only partial as the value must be // less than the public modulus, which is never a power of two) is zero. // // The next byte contains the value 2 (which stands for public-key // encrypted data in the PKCS standard [PKCS#1]). Then, there are non- // zero random bytes to fill any unused space, a zero byte, and the data // to be encrypted in the least significant bytes, the last byte of the // data in the least significant byte. // Presumably the part of PKCS#1 they're refering to is "Section 7.2.1 Encryption Operation", // under "7.2 RSAES-PKCS1-v1.5" and "7 Encryption schemes" of the following URL: // ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf $modulus = $key[1]->toBytes(); $length = strlen($modulus) - strlen($m) - 3; $random = ''; while (strlen($random) != $length) { $block = Random::string($length - strlen($random)); $block = str_replace("\x00", '', $block); $random.= $block; } $temp = chr(0) . chr(2) . $random . chr(0) . $m; $m = new BigInteger($temp, 256); $m = $m->modPow($key[0], $key[1]); return $m->toBytes(); } /** * Define Array * * Takes any number of arrays whose indices are integers and whose values are strings and defines a bunch of * named constants from it, using the value as the name of the constant and the index as the value of the constant. * If any of the constants that would be defined already exists, none of the constants will be defined. * * @param array $array * @access private */ function _define_array() { $args = func_get_args(); foreach ($args as $arg) { foreach ($arg as $key => $value) { if (!defined($value)) { define($value, $key); } else { break 2; } } } } /** * Returns a log of the packets that have been sent and received. * * Returns a string if NET_SSH1_LOGGING == self::LOG_COMPLEX, an array if NET_SSH1_LOGGING == self::LOG_SIMPLE and false if !defined('NET_SSH1_LOGGING') * * @access public * @return array|false|string */ function getLog() { if (!defined('NET_SSH1_LOGGING')) { return false; } switch (NET_SSH1_LOGGING) { case self::LOG_SIMPLE: return $this->message_number_log; break; case self::LOG_COMPLEX: return $this->_format_log($this->message_log, $this->protocol_flags_log); break; default: return false; } } /** * Formats a log for printing * * @param array $message_log * @param array $message_number_log * @access private * @return string */ function _format_log($message_log, $message_number_log) { $output = ''; for ($i = 0; $i < count($message_log); $i++) { $output.= $message_number_log[$i] . "\r\n"; $current_log = $message_log[$i]; $j = 0; do { if (strlen($current_log)) { $output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 '; } $fragment = $this->_string_shift($current_log, $this->log_short_width); $hex = substr(preg_replace_callback('#.#s', array($this, '_format_log_helper'), $fragment), strlen($this->log_boundary)); // replace non ASCII printable characters with dots // http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters // also replace < with a . since < messes up the output on web browsers $raw = preg_replace('#[^\x20-\x7E]|<#', '.', $fragment); $output.= str_pad($hex, $this->log_long_width - $this->log_short_width, ' ') . $raw . "\r\n"; $j++; } while (strlen($current_log)); $output.= "\r\n"; } return $output; } /** * Helper function for _format_log * * For use with preg_replace_callback() * * @param array $matches * @access private * @return string */ function _format_log_helper($matches) { return $this->log_boundary . str_pad(dechex(ord($matches[0])), 2, '0', STR_PAD_LEFT); } /** * Return the server key public exponent * * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead, * the raw bytes. This behavior is similar to PHP's md5() function. * * @param bool $raw_output * @return string * @access public */ function getServerKeyPublicExponent($raw_output = false) { return $raw_output ? $this->server_key_public_exponent->toBytes() : $this->server_key_public_exponent->toString(); } /** * Return the server key public modulus * * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead, * the raw bytes. This behavior is similar to PHP's md5() function. * * @param bool $raw_output * @return string * @access public */ function getServerKeyPublicModulus($raw_output = false) { return $raw_output ? $this->server_key_public_modulus->toBytes() : $this->server_key_public_modulus->toString(); } /** * Return the host key public exponent * * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead, * the raw bytes. This behavior is similar to PHP's md5() function. * * @param bool $raw_output * @return string * @access public */ function getHostKeyPublicExponent($raw_output = false) { return $raw_output ? $this->host_key_public_exponent->toBytes() : $this->host_key_public_exponent->toString(); } /** * Return the host key public modulus * * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead, * the raw bytes. This behavior is similar to PHP's md5() function. * * @param bool $raw_output * @return string * @access public */ function getHostKeyPublicModulus($raw_output = false) { return $raw_output ? $this->host_key_public_modulus->toBytes() : $this->host_key_public_modulus->toString(); } /** * Return a list of ciphers supported by SSH1 server. * * Just because a cipher is supported by an SSH1 server doesn't mean it's supported by this library. If $raw_output * is set to true, returns, instead, an array of constants. ie. instead of array('Triple-DES in CBC mode'), you'll * get array(self::CIPHER_3DES). * * @param bool $raw_output * @return array * @access public */ function getSupportedCiphers($raw_output = false) { return $raw_output ? array_keys($this->supported_ciphers) : array_values($this->supported_ciphers); } /** * Return a list of authentications supported by SSH1 server. * * Just because a cipher is supported by an SSH1 server doesn't mean it's supported by this library. If $raw_output * is set to true, returns, instead, an array of constants. ie. instead of array('password authentication'), you'll * get array(self::AUTH_PASSWORD). * * @param bool $raw_output * @return array * @access public */ function getSupportedAuthentications($raw_output = false) { return $raw_output ? array_keys($this->supported_authentications) : array_values($this->supported_authentications); } /** * Return the server identification. * * @return string * @access public */ function getServerIdentification() { return rtrim($this->server_identification); } /** * Logs data packets * * Makes sure that only the last 1MB worth of packets will be logged * * @param string $data * @access private */ function _append_log($protocol_flags, $message) { switch (NET_SSH1_LOGGING) { // useful for benchmarks case self::LOG_SIMPLE: $this->protocol_flags_log[] = $protocol_flags; break; // the most useful log for SSH1 case self::LOG_COMPLEX: $this->protocol_flags_log[] = $protocol_flags; $this->_string_shift($message); $this->log_size+= strlen($message); $this->message_log[] = $message; while ($this->log_size > self::LOG_MAX_SIZE) { $this->log_size-= strlen(array_shift($this->message_log)); array_shift($this->protocol_flags_log); } break; // dump the output out realtime; packets may be interspersed with non packets, // passwords won't be filtered out and select other packets may not be correctly // identified case self::LOG_REALTIME: echo "<pre>\r\n" . $this->_format_log(array($message), array($protocol_flags)) . "\r\n</pre>\r\n"; @flush(); @ob_flush(); break; // basically the same thing as self::LOG_REALTIME with the caveat that self::LOG_REALTIME_FILE // needs to be defined and that the resultant log file will be capped out at self::LOG_MAX_SIZE. // the earliest part of the log file is denoted by the first <<< START >>> and is not going to necessarily // at the beginning of the file case self::LOG_REALTIME_FILE: if (!isset($this->realtime_log_file)) { // PHP doesn't seem to like using constants in fopen() $filename = self::LOG_REALTIME_FILE; $fp = fopen($filename, 'w'); $this->realtime_log_file = $fp; } if (!is_resource($this->realtime_log_file)) { break; } $entry = $this->_format_log(array($message), array($protocol_flags)); if ($this->realtime_log_wrap) { $temp = "<<< START >>>\r\n"; $entry.= $temp; fseek($this->realtime_log_file, ftell($this->realtime_log_file) - strlen($temp)); } $this->realtime_log_size+= strlen($entry); if ($this->realtime_log_size > self::LOG_MAX_SIZE) { fseek($this->realtime_log_file, 0); $this->realtime_log_size = strlen($entry); $this->realtime_log_wrap = true; } fputs($this->realtime_log_file, $entry); } } } <?php /** * Pure-PHP implementation of SCP. * * PHP version 5 * * The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}. * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $ssh = new \phpseclib\Net\SSH2('www.domain.tld'); * if (!$ssh->login('username', 'password')) { * exit('bad login'); * } * $scp = new \phpseclib\Net\SCP($ssh); * * $scp->put('abcd', str_repeat('x', 1024*1024)); * ?> * </code> * * @category Net * @package SCP * @author Jim Wigginton <terrafrost@php.net> * @copyright 2010 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Net; /** * Pure-PHP implementations of SCP. * * @package SCP * @author Jim Wigginton <terrafrost@php.net> * @access public */ class SCP { /**#@+ * @access public * @see \phpseclib\Net\SCP::put() */ /** * Reads data from a local file. */ const SOURCE_LOCAL_FILE = 1; /** * Reads data from a string. */ const SOURCE_STRING = 2; /**#@-*/ /**#@+ * @access private * @see \phpseclib\Net\SCP::_send() * @see \phpseclib\Net\SCP::_receive() */ /** * SSH1 is being used. */ const MODE_SSH1 = 1; /** * SSH2 is being used. */ const MODE_SSH2 = 2; /**#@-*/ /** * SSH Object * * @var object * @access private */ var $ssh; /** * Packet Size * * @var int * @access private */ var $packet_size; /** * Mode * * @var int * @access private */ var $mode; /** * Default Constructor. * * Connects to an SSH server * * @param \phpseclib\Net\SSH1|\phpseclib\Net\SSH2 $ssh * @return \phpseclib\Net\SCP * @access public */ function __construct($ssh) { if ($ssh instanceof SSH2) { $this->mode = self::MODE_SSH2; } elseif ($ssh instanceof SSH1) { $this->packet_size = 50000; $this->mode = self::MODE_SSH1; } else { return; } $this->ssh = $ssh; } /** * Uploads a file to the SCP server. * * By default, \phpseclib\Net\SCP::put() does not read from the local filesystem. $data is dumped directly into $remote_file. * So, for example, if you set $data to 'filename.ext' and then do \phpseclib\Net\SCP::get(), you will get a file, twelve bytes * long, containing 'filename.ext' as its contents. * * Setting $mode to self::SOURCE_LOCAL_FILE will change the above behavior. With self::SOURCE_LOCAL_FILE, $remote_file will * contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how * large $remote_file will be, as well. * * Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take * care of that, yourself. * * @param string $remote_file * @param string $data * @param int $mode * @param callable $callback * @return bool * @access public */ function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null) { if (!isset($this->ssh)) { return false; } if (empty($remote_file)) { user_error('remote_file cannot be blank', E_USER_NOTICE); return false; } if (!$this->ssh->exec('scp -t ' . escapeshellarg($remote_file), false)) { // -t = to return false; } $temp = $this->_receive(); if ($temp !== chr(0)) { return false; } if ($this->mode == self::MODE_SSH2) { $this->packet_size = $this->ssh->packet_size_client_to_server[SSH2::CHANNEL_EXEC] - 4; } $remote_file = basename($remote_file); if ($mode == self::SOURCE_STRING) { $size = strlen($data); } else { if (!is_file($data)) { user_error("$data is not a valid file", E_USER_NOTICE); return false; } $fp = @fopen($data, 'rb'); if (!$fp) { return false; } $size = filesize($data); } $this->_send('C0644 ' . $size . ' ' . $remote_file . "\n"); $temp = $this->_receive(); if ($temp !== chr(0)) { return false; } $sent = 0; while ($sent < $size) { $temp = $mode & self::SOURCE_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size); $this->_send($temp); $sent+= strlen($temp); if (is_callable($callback)) { call_user_func($callback, $sent); } } $this->_close(); if ($mode != self::SOURCE_STRING) { fclose($fp); } return true; } /** * Downloads a file from the SCP server. * * Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if * the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the * operation * * @param string $remote_file * @param string $local_file * @return mixed * @access public */ function get($remote_file, $local_file = false) { if (!isset($this->ssh)) { return false; } if (!$this->ssh->exec('scp -f ' . escapeshellarg($remote_file), false)) { // -f = from return false; } $this->_send("\0"); if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->_receive()), $info)) { return false; } $this->_send("\0"); $size = 0; if ($local_file !== false) { $fp = @fopen($local_file, 'wb'); if (!$fp) { return false; } } $content = ''; while ($size < $info['size']) { $data = $this->_receive(); // SCP usually seems to split stuff out into 16k chunks $size+= strlen($data); if ($local_file === false) { $content.= $data; } else { fputs($fp, $data); } } $this->_close(); if ($local_file !== false) { fclose($fp); return true; } return $content; } /** * Sends a packet to an SSH server * * @param string $data * @access private */ function _send($data) { switch ($this->mode) { case self::MODE_SSH2: $this->ssh->_send_channel_packet(SSH2::CHANNEL_EXEC, $data); break; case self::MODE_SSH1: $data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($data), $data); $this->ssh->_send_binary_packet($data); } } /** * Receives a packet from an SSH server * * @return string * @access private */ function _receive() { switch ($this->mode) { case self::MODE_SSH2: return $this->ssh->_get_channel_packet(SSH2::CHANNEL_EXEC, true); case self::MODE_SSH1: if (!$this->ssh->bitmap) { return false; } while (true) { $response = $this->ssh->_get_binary_packet(); switch ($response[SSH1::RESPONSE_TYPE]) { case NET_SSH1_SMSG_STDOUT_DATA: if (strlen($response[SSH1::RESPONSE_DATA]) < 4) { return false; } extract(unpack('Nlength', $response[SSH1::RESPONSE_DATA])); return $this->ssh->_string_shift($response[SSH1::RESPONSE_DATA], $length); case NET_SSH1_SMSG_STDERR_DATA: break; case NET_SSH1_SMSG_EXITSTATUS: $this->ssh->_send_binary_packet(chr(NET_SSH1_CMSG_EXIT_CONFIRMATION)); fclose($this->ssh->fsock); $this->ssh->bitmap = 0; return false; default: user_error('Unknown packet received', E_USER_NOTICE); return false; } } } } /** * Closes the connection to an SSH server * * @access private */ function _close() { switch ($this->mode) { case self::MODE_SSH2: $this->ssh->_close_channel(SSH2::CHANNEL_EXEC, true); break; case self::MODE_SSH1: $this->ssh->disconnect(); } } } <?php /** * SFTP Stream Wrapper * * Creates an sftp:// protocol handler that can be used with, for example, fopen(), dir(), etc. * * PHP version 5 * * @category Net * @package SFTP * @author Jim Wigginton <terrafrost@php.net> * @copyright 2013 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Net\SFTP; use phpseclib\Crypt\RSA; use phpseclib\Net\SFTP; /** * SFTP Stream Wrapper * * @package SFTP * @author Jim Wigginton <terrafrost@php.net> * @access public */ class Stream { /** * SFTP instances * * Rather than re-create the connection we re-use instances if possible * * @var array */ static $instances; /** * SFTP instance * * @var object * @access private */ var $sftp; /** * Path * * @var string * @access private */ var $path; /** * Mode * * @var string * @access private */ var $mode; /** * Position * * @var int * @access private */ var $pos; /** * Size * * @var int * @access private */ var $size; /** * Directory entries * * @var array * @access private */ var $entries; /** * EOF flag * * @var bool * @access private */ var $eof; /** * Context resource * * Technically this needs to be publically accessible so PHP can set it directly * * @var resource * @access public */ var $context; /** * Notification callback function * * @var callable * @access public */ var $notification; /** * Registers this class as a URL wrapper. * * @param string $protocol The wrapper name to be registered. * @return bool True on success, false otherwise. * @access public */ static function register($protocol = 'sftp') { if (in_array($protocol, stream_get_wrappers(), true)) { return false; } return stream_wrapper_register($protocol, get_called_class()); } /** * The Constructor * * @access public */ function __construct() { if (defined('NET_SFTP_STREAM_LOGGING')) { echo "__construct()\r\n"; } } /** * Path Parser * * Extract a path from a URI and actually connect to an SSH server if appropriate * * If "notification" is set as a context parameter the message code for successful login is * NET_SSH2_MSG_USERAUTH_SUCCESS. For a failed login it's NET_SSH2_MSG_USERAUTH_FAILURE. * * @param string $path * @return string * @access private */ function _parse_path($path) { $orig = $path; extract(parse_url($path) + array('port' => 22)); if (isset($query)) { $path.= '?' . $query; } elseif (preg_match('/(\?|\?#)$/', $orig)) { $path.= '?'; } if (isset($fragment)) { $path.= '#' . $fragment; } elseif ($orig[strlen($orig) - 1] == '#') { $path.= '#'; } if (!isset($host)) { return false; } if (isset($this->context)) { $context = stream_context_get_params($this->context); if (isset($context['notification'])) { $this->notification = $context['notification']; } } if ($host[0] == '$') { $host = substr($host, 1); global ${$host}; if (($$host instanceof SFTP) === false) { return false; } $this->sftp = $$host; } else { if (isset($this->context)) { $context = stream_context_get_options($this->context); } if (isset($context[$scheme]['session'])) { $sftp = $context[$scheme]['session']; } if (isset($context[$scheme]['sftp'])) { $sftp = $context[$scheme]['sftp']; } if (isset($sftp) && $sftp instanceof SFTP) { $this->sftp = $sftp; return $path; } if (isset($context[$scheme]['username'])) { $user = $context[$scheme]['username']; } if (isset($context[$scheme]['password'])) { $pass = $context[$scheme]['password']; } if (isset($context[$scheme]['privkey']) && $context[$scheme]['privkey'] instanceof RSA) { $pass = $context[$scheme]['privkey']; } if (!isset($user) || !isset($pass)) { return false; } // casting $pass to a string is necessary in the event that it's a \phpseclib\Crypt\RSA object if (isset(self::$instances[$host][$port][$user][(string) $pass])) { $this->sftp = self::$instances[$host][$port][$user][(string) $pass]; } else { $this->sftp = new SFTP($host, $port); $this->sftp->disableStatCache(); if (isset($this->notification) && is_callable($this->notification)) { /* if !is_callable($this->notification) we could do this: user_error('fopen(): failed to call user notifier', E_USER_WARNING); the ftp wrapper gives errors like that when the notifier isn't callable. i've opted not to do that, however, since the ftp wrapper gives the line on which the fopen occurred as the line number - not the line that the user_error is on. */ call_user_func($this->notification, STREAM_NOTIFY_CONNECT, STREAM_NOTIFY_SEVERITY_INFO, '', 0, 0, 0); call_user_func($this->notification, STREAM_NOTIFY_AUTH_REQUIRED, STREAM_NOTIFY_SEVERITY_INFO, '', 0, 0, 0); if (!$this->sftp->login($user, $pass)) { call_user_func($this->notification, STREAM_NOTIFY_AUTH_RESULT, STREAM_NOTIFY_SEVERITY_ERR, 'Login Failure', NET_SSH2_MSG_USERAUTH_FAILURE, 0, 0); return false; } call_user_func($this->notification, STREAM_NOTIFY_AUTH_RESULT, STREAM_NOTIFY_SEVERITY_INFO, 'Login Success', NET_SSH2_MSG_USERAUTH_SUCCESS, 0, 0); } else { if (!$this->sftp->login($user, $pass)) { return false; } } self::$instances[$host][$port][$user][(string) $pass] = $this->sftp; } } return $path; } /** * Opens file or URL * * @param string $path * @param string $mode * @param int $options * @param string $opened_path * @return bool * @access public */ function _stream_open($path, $mode, $options, &$opened_path) { $path = $this->_parse_path($path); if ($path === false) { return false; } $this->path = $path; $this->size = $this->sftp->size($path); $this->mode = preg_replace('#[bt]$#', '', $mode); $this->eof = false; if ($this->size === false) { if ($this->mode[0] == 'r') { return false; } else { $this->sftp->touch($path); $this->size = 0; } } else { switch ($this->mode[0]) { case 'x': return false; case 'w': $this->sftp->truncate($path, 0); $this->size = 0; } } $this->pos = $this->mode[0] != 'a' ? 0 : $this->size; return true; } /** * Read from stream * * @param int $count * @return mixed * @access public */ function _stream_read($count) { switch ($this->mode) { case 'w': case 'a': case 'x': case 'c': return false; } // commented out because some files - eg. /dev/urandom - will say their size is 0 when in fact it's kinda infinite //if ($this->pos >= $this->size) { // $this->eof = true; // return false; //} $result = $this->sftp->get($this->path, false, $this->pos, $count); if (isset($this->notification) && is_callable($this->notification)) { if ($result === false) { call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0); return 0; } // seems that PHP calls stream_read in 8k chunks call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($result), $this->size); } if (empty($result)) { // ie. false or empty string $this->eof = true; return false; } $this->pos+= strlen($result); return $result; } /** * Write to stream * * @param string $data * @return mixed * @access public */ function _stream_write($data) { switch ($this->mode) { case 'r': return false; } $result = $this->sftp->put($this->path, $data, SFTP::SOURCE_STRING, $this->pos); if (isset($this->notification) && is_callable($this->notification)) { if (!$result) { call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0); return 0; } // seems that PHP splits up strings into 8k blocks before calling stream_write call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($data), strlen($data)); } if ($result === false) { return false; } $this->pos+= strlen($data); if ($this->pos > $this->size) { $this->size = $this->pos; } $this->eof = false; return strlen($data); } /** * Retrieve the current position of a stream * * @return int * @access public */ function _stream_tell() { return $this->pos; } /** * Tests for end-of-file on a file pointer * * In my testing there are four classes functions that normally effect the pointer: * fseek, fputs / fwrite, fgets / fread and ftruncate. * * Only fgets / fread, however, results in feof() returning true. do fputs($fp, 'aaa') on a blank file and feof() * will return false. do fread($fp, 1) and feof() will then return true. do fseek($fp, 10) on ablank file and feof() * will return false. do fread($fp, 1) and feof() will then return true. * * @return bool * @access public */ function _stream_eof() { return $this->eof; } /** * Seeks to specific location in a stream * * @param int $offset * @param int $whence * @return bool * @access public */ function _stream_seek($offset, $whence) { switch ($whence) { case SEEK_SET: if ($offset >= $this->size || $offset < 0) { return false; } break; case SEEK_CUR: $offset+= $this->pos; break; case SEEK_END: $offset+= $this->size; } $this->pos = $offset; $this->eof = false; return true; } /** * Change stream options * * @param string $path * @param int $option * @param mixed $var * @return bool * @access public */ function _stream_metadata($path, $option, $var) { $path = $this->_parse_path($path); if ($path === false) { return false; } // stream_metadata was introduced in PHP 5.4.0 but as of 5.4.11 the constants haven't been defined // see http://www.php.net/streamwrapper.stream-metadata and https://bugs.php.net/64246 // and https://github.com/php/php-src/blob/master/main/php_streams.h#L592 switch ($option) { case 1: // PHP_STREAM_META_TOUCH return $this->sftp->touch($path, $var[0], $var[1]); case 2: // PHP_STREAM_OWNER_NAME case 3: // PHP_STREAM_GROUP_NAME return false; case 4: // PHP_STREAM_META_OWNER return $this->sftp->chown($path, $var); case 5: // PHP_STREAM_META_GROUP return $this->sftp->chgrp($path, $var); case 6: // PHP_STREAM_META_ACCESS return $this->sftp->chmod($path, $var) !== false; } } /** * Retrieve the underlaying resource * * @param int $cast_as * @return resource * @access public */ function _stream_cast($cast_as) { return $this->sftp->fsock; } /** * Advisory file locking * * @param int $operation * @return bool * @access public */ function _stream_lock($operation) { return false; } /** * Renames a file or directory * * Attempts to rename oldname to newname, moving it between directories if necessary. * If newname exists, it will be overwritten. This is a departure from what \phpseclib\Net\SFTP * does. * * @param string $path_from * @param string $path_to * @return bool * @access public */ function _rename($path_from, $path_to) { $path1 = parse_url($path_from); $path2 = parse_url($path_to); unset($path1['path'], $path2['path']); if ($path1 != $path2) { return false; } $path_from = $this->_parse_path($path_from); $path_to = parse_url($path_to); if ($path_from === false) { return false; } $path_to = $path_to['path']; // the $component part of parse_url() was added in PHP 5.1.2 // "It is an error if there already exists a file with the name specified by newpath." // -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-6.5 if (!$this->sftp->rename($path_from, $path_to)) { if ($this->sftp->stat($path_to)) { return $this->sftp->delete($path_to, true) && $this->sftp->rename($path_from, $path_to); } return false; } return true; } /** * Open directory handle * * The only $options is "whether or not to enforce safe_mode (0x04)". Since safe mode was deprecated in 5.3 and * removed in 5.4 I'm just going to ignore it. * * Also, nlist() is the best that this function is realistically going to be able to do. When an SFTP client * sends a SSH_FXP_READDIR packet you don't generally get info on just one file but on multiple files. Quoting * the SFTP specs: * * The SSH_FXP_NAME response has the following format: * * uint32 id * uint32 count * repeats count times: * string filename * string longname * ATTRS attrs * * @param string $path * @param int $options * @return bool * @access public */ function _dir_opendir($path, $options) { $path = $this->_parse_path($path); if ($path === false) { return false; } $this->pos = 0; $this->entries = $this->sftp->nlist($path); return $this->entries !== false; } /** * Read entry from directory handle * * @return mixed * @access public */ function _dir_readdir() { if (isset($this->entries[$this->pos])) { return $this->entries[$this->pos++]; } return false; } /** * Rewind directory handle * * @return bool * @access public */ function _dir_rewinddir() { $this->pos = 0; return true; } /** * Close directory handle * * @return bool * @access public */ function _dir_closedir() { return true; } /** * Create a directory * * Only valid $options is STREAM_MKDIR_RECURSIVE * * @param string $path * @param int $mode * @param int $options * @return bool * @access public */ function _mkdir($path, $mode, $options) { $path = $this->_parse_path($path); if ($path === false) { return false; } return $this->sftp->mkdir($path, $mode, $options & STREAM_MKDIR_RECURSIVE); } /** * Removes a directory * * Only valid $options is STREAM_MKDIR_RECURSIVE per <http://php.net/streamwrapper.rmdir>, however, * <http://php.net/rmdir> does not have a $recursive parameter as mkdir() does so I don't know how * STREAM_MKDIR_RECURSIVE is supposed to be set. Also, when I try it out with rmdir() I get 8 as * $options. What does 8 correspond to? * * @param string $path * @param int $mode * @param int $options * @return bool * @access public */ function _rmdir($path, $options) { $path = $this->_parse_path($path); if ($path === false) { return false; } return $this->sftp->rmdir($path); } /** * Flushes the output * * See <http://php.net/fflush>. Always returns true because \phpseclib\Net\SFTP doesn't cache stuff before writing * * @return bool * @access public */ function _stream_flush() { return true; } /** * Retrieve information about a file resource * * @return mixed * @access public */ function _stream_stat() { $results = $this->sftp->stat($this->path); if ($results === false) { return false; } return $results; } /** * Delete a file * * @param string $path * @return bool * @access public */ function _unlink($path) { $path = $this->_parse_path($path); if ($path === false) { return false; } return $this->sftp->delete($path, false); } /** * Retrieve information about a file * * Ignores the STREAM_URL_STAT_QUIET flag because the entirety of \phpseclib\Net\SFTP\Stream is quiet by default * might be worthwhile to reconstruct bits 12-16 (ie. the file type) if mode doesn't have them but we'll * cross that bridge when and if it's reached * * @param string $path * @param int $flags * @return mixed * @access public */ function _url_stat($path, $flags) { $path = $this->_parse_path($path); if ($path === false) { return false; } $results = $flags & STREAM_URL_STAT_LINK ? $this->sftp->lstat($path) : $this->sftp->stat($path); if ($results === false) { return false; } return $results; } /** * Truncate stream * * @param int $new_size * @return bool * @access public */ function _stream_truncate($new_size) { if (!$this->sftp->truncate($this->path, $new_size)) { return false; } $this->eof = false; $this->size = $new_size; return true; } /** * Change stream options * * STREAM_OPTION_WRITE_BUFFER isn't supported for the same reason stream_flush isn't. * The other two aren't supported because of limitations in \phpseclib\Net\SFTP. * * @param int $option * @param int $arg1 * @param int $arg2 * @return bool * @access public */ function _stream_set_option($option, $arg1, $arg2) { return false; } /** * Close an resource * * @access public */ function _stream_close() { } /** * __call Magic Method * * When you're utilizing an SFTP stream you're not calling the methods in this class directly - PHP is calling them for you. * Which kinda begs the question... what methods is PHP calling and what parameters is it passing to them? This function * lets you figure that out. * * If NET_SFTP_STREAM_LOGGING is defined all calls will be output on the screen and then (regardless of whether or not * NET_SFTP_STREAM_LOGGING is enabled) the parameters will be passed through to the appropriate method. * * @param string * @param array * @return mixed * @access public */ function __call($name, $arguments) { if (defined('NET_SFTP_STREAM_LOGGING')) { echo $name . '('; $last = count($arguments) - 1; foreach ($arguments as $i => $argument) { var_export($argument); if ($i != $last) { echo ','; } } echo ")\r\n"; } $name = '_' . $name; if (!method_exists($this, $name)) { return false; } return call_user_func_array(array($this, $name), $arguments); } } <?php /** * Pure-PHP implementation of SSHv2. * * PHP version 5 * * Here are some examples of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $ssh = new \phpseclib\Net\SSH2('www.domain.tld'); * if (!$ssh->login('username', 'password')) { * exit('Login Failed'); * } * * echo $ssh->exec('pwd'); * echo $ssh->exec('ls -la'); * ?> * </code> * * <code> * <?php * include 'vendor/autoload.php'; * * $key = new \phpseclib\Crypt\RSA(); * //$key->setPassword('whatever'); * $key->loadKey(file_get_contents('privatekey')); * * $ssh = new \phpseclib\Net\SSH2('www.domain.tld'); * if (!$ssh->login('username', $key)) { * exit('Login Failed'); * } * * echo $ssh->read('username@username:~$'); * $ssh->write("ls -la\n"); * echo $ssh->read('username@username:~$'); * ?> * </code> * * @category Net * @package SSH2 * @author Jim Wigginton <terrafrost@php.net> * @copyright 2007 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Net; use phpseclib\Crypt\Base; use phpseclib\Crypt\Blowfish; use phpseclib\Crypt\Hash; use phpseclib\Crypt\Random; use phpseclib\Crypt\RC4; use phpseclib\Crypt\Rijndael; use phpseclib\Crypt\RSA; use phpseclib\Crypt\TripleDES; use phpseclib\Crypt\Twofish; use phpseclib\Math\BigInteger; // Used to do Diffie-Hellman key exchange and DSA/RSA signature verification. use phpseclib\System\SSH\Agent; /** * Pure-PHP implementation of SSHv2. * * @package SSH2 * @author Jim Wigginton <terrafrost@php.net> * @access public */ class SSH2 { /**#@+ * Execution Bitmap Masks * * @see \phpseclib\Net\SSH2::bitmap * @access private */ const MASK_CONSTRUCTOR = 0x00000001; const MASK_CONNECTED = 0x00000002; const MASK_LOGIN_REQ = 0x00000004; const MASK_LOGIN = 0x00000008; const MASK_SHELL = 0x00000010; const MASK_WINDOW_ADJUST = 0x00000020; /**#@-*/ /**#@+ * Channel constants * * RFC4254 refers not to client and server channels but rather to sender and recipient channels. we don't refer * to them in that way because RFC4254 toggles the meaning. the client sends a SSH_MSG_CHANNEL_OPEN message with * a sender channel and the server sends a SSH_MSG_CHANNEL_OPEN_CONFIRMATION in response, with a sender and a * recepient channel. at first glance, you might conclude that SSH_MSG_CHANNEL_OPEN_CONFIRMATION's sender channel * would be the same thing as SSH_MSG_CHANNEL_OPEN's sender channel, but it's not, per this snipet: * The 'recipient channel' is the channel number given in the original * open request, and 'sender channel' is the channel number allocated by * the other side. * * @see \phpseclib\Net\SSH2::_send_channel_packet() * @see \phpseclib\Net\SSH2::_get_channel_packet() * @access private */ const CHANNEL_EXEC = 1; // PuTTy uses 0x100 const CHANNEL_SHELL = 2; const CHANNEL_SUBSYSTEM = 3; const CHANNEL_AGENT_FORWARD = 4; const CHANNEL_KEEP_ALIVE = 5; /**#@-*/ /**#@+ * @access public * @see \phpseclib\Net\SSH2::getLog() */ /** * Returns the message numbers */ const LOG_SIMPLE = 1; /** * Returns the message content */ const LOG_COMPLEX = 2; /** * Outputs the content real-time */ const LOG_REALTIME = 3; /** * Dumps the content real-time to a file */ const LOG_REALTIME_FILE = 4; /** * Make sure that the log never gets larger than this */ const LOG_MAX_SIZE = 1048576; // 1024 * 1024 /**#@-*/ /**#@+ * @access public * @see \phpseclib\Net\SSH2::read() */ /** * Returns when a string matching $expect exactly is found */ const READ_SIMPLE = 1; /** * Returns when a string matching the regular expression $expect is found */ const READ_REGEX = 2; /** * Returns whenever a data packet is received. * * Some data packets may only contain a single character so it may be necessary * to call read() multiple times when using this option */ const READ_NEXT = 3; /**#@-*/ /** * The SSH identifier * * @var string * @access private */ var $identifier; /** * The Socket Object * * @var object * @access private */ var $fsock; /** * Execution Bitmap * * The bits that are set represent functions that have been called already. This is used to determine * if a requisite function has been successfully executed. If not, an error should be thrown. * * @var int * @access private */ var $bitmap = 0; /** * Error information * * @see self::getErrors() * @see self::getLastError() * @var string * @access private */ var $errors = array(); /** * Server Identifier * * @see self::getServerIdentification() * @var array|false * @access private */ var $server_identifier = false; /** * Key Exchange Algorithms * * @see self::getKexAlgorithims() * @var array|false * @access private */ var $kex_algorithms = false; /** * Key Exchange Algorithm * * @see self::getMethodsNegotiated() * @var string|false * @access private */ var $kex_algorithm = false; /** * Minimum Diffie-Hellman Group Bit Size in RFC 4419 Key Exchange Methods * * @see self::_key_exchange() * @var int * @access private */ var $kex_dh_group_size_min = 1536; /** * Preferred Diffie-Hellman Group Bit Size in RFC 4419 Key Exchange Methods * * @see self::_key_exchange() * @var int * @access private */ var $kex_dh_group_size_preferred = 2048; /** * Maximum Diffie-Hellman Group Bit Size in RFC 4419 Key Exchange Methods * * @see self::_key_exchange() * @var int * @access private */ var $kex_dh_group_size_max = 4096; /** * Server Host Key Algorithms * * @see self::getServerHostKeyAlgorithms() * @var array|false * @access private */ var $server_host_key_algorithms = false; /** * Encryption Algorithms: Client to Server * * @see self::getEncryptionAlgorithmsClient2Server() * @var array|false * @access private */ var $encryption_algorithms_client_to_server = false; /** * Encryption Algorithms: Server to Client * * @see self::getEncryptionAlgorithmsServer2Client() * @var array|false * @access private */ var $encryption_algorithms_server_to_client = false; /** * MAC Algorithms: Client to Server * * @see self::getMACAlgorithmsClient2Server() * @var array|false * @access private */ var $mac_algorithms_client_to_server = false; /** * MAC Algorithms: Server to Client * * @see self::getMACAlgorithmsServer2Client() * @var array|false * @access private */ var $mac_algorithms_server_to_client = false; /** * Compression Algorithms: Client to Server * * @see self::getCompressionAlgorithmsClient2Server() * @var array|false * @access private */ var $compression_algorithms_client_to_server = false; /** * Compression Algorithms: Server to Client * * @see self::getCompressionAlgorithmsServer2Client() * @var array|false * @access private */ var $compression_algorithms_server_to_client = false; /** * Languages: Server to Client * * @see self::getLanguagesServer2Client() * @var array|false * @access private */ var $languages_server_to_client = false; /** * Languages: Client to Server * * @see self::getLanguagesClient2Server() * @var array|false * @access private */ var $languages_client_to_server = false; /** * Preferred Algorithms * * @see self::setPreferredAlgorithms() * @var array * @access private */ var $preferred = array(); /** * Block Size for Server to Client Encryption * * "Note that the length of the concatenation of 'packet_length', * 'padding_length', 'payload', and 'random padding' MUST be a multiple * of the cipher block size or 8, whichever is larger. This constraint * MUST be enforced, even when using stream ciphers." * * -- http://tools.ietf.org/html/rfc4253#section-6 * * @see self::__construct() * @see self::_send_binary_packet() * @var int * @access private */ var $encrypt_block_size = 8; /** * Block Size for Client to Server Encryption * * @see self::__construct() * @see self::_get_binary_packet() * @var int * @access private */ var $decrypt_block_size = 8; /** * Server to Client Encryption Object * * @see self::_get_binary_packet() * @var object * @access private */ var $decrypt = false; /** * Client to Server Encryption Object * * @see self::_send_binary_packet() * @var object * @access private */ var $encrypt = false; /** * Client to Server HMAC Object * * @see self::_send_binary_packet() * @var object * @access private */ var $hmac_create = false; /** * Server to Client HMAC Object * * @see self::_get_binary_packet() * @var object * @access private */ var $hmac_check = false; /** * Size of server to client HMAC * * We need to know how big the HMAC will be for the server to client direction so that we know how many bytes to read. * For the client to server side, the HMAC object will make the HMAC as long as it needs to be. All we need to do is * append it. * * @see self::_get_binary_packet() * @var int * @access private */ var $hmac_size = false; /** * Server Public Host Key * * @see self::getServerPublicHostKey() * @var string * @access private */ var $server_public_host_key; /** * Session identifier * * "The exchange hash H from the first key exchange is additionally * used as the session identifier, which is a unique identifier for * this connection." * * -- http://tools.ietf.org/html/rfc4253#section-7.2 * * @see self::_key_exchange() * @var string * @access private */ var $session_id = false; /** * Exchange hash * * The current exchange hash * * @see self::_key_exchange() * @var string * @access private */ var $exchange_hash = false; /** * Message Numbers * * @see self::__construct() * @var array * @access private */ var $message_numbers = array(); /** * Disconnection Message 'reason codes' defined in RFC4253 * * @see self::__construct() * @var array * @access private */ var $disconnect_reasons = array(); /** * SSH_MSG_CHANNEL_OPEN_FAILURE 'reason codes', defined in RFC4254 * * @see self::__construct() * @var array * @access private */ var $channel_open_failure_reasons = array(); /** * Terminal Modes * * @link http://tools.ietf.org/html/rfc4254#section-8 * @see self::__construct() * @var array * @access private */ var $terminal_modes = array(); /** * SSH_MSG_CHANNEL_EXTENDED_DATA's data_type_codes * * @link http://tools.ietf.org/html/rfc4254#section-5.2 * @see self::__construct() * @var array * @access private */ var $channel_extended_data_type_codes = array(); /** * Send Sequence Number * * See 'Section 6.4. Data Integrity' of rfc4253 for more info. * * @see self::_send_binary_packet() * @var int * @access private */ var $send_seq_no = 0; /** * Get Sequence Number * * See 'Section 6.4. Data Integrity' of rfc4253 for more info. * * @see self::_get_binary_packet() * @var int * @access private */ var $get_seq_no = 0; /** * Server Channels * * Maps client channels to server channels * * @see self::_get_channel_packet() * @see self::exec() * @var array * @access private */ var $server_channels = array(); /** * Channel Buffers * * If a client requests a packet from one channel but receives two packets from another those packets should * be placed in a buffer * * @see self::_get_channel_packet() * @see self::exec() * @var array * @access private */ var $channel_buffers = array(); /** * Channel Status * * Contains the type of the last sent message * * @see self::_get_channel_packet() * @var array * @access private */ var $channel_status = array(); /** * Packet Size * * Maximum packet size indexed by channel * * @see self::_send_channel_packet() * @var array * @access private */ var $packet_size_client_to_server = array(); /** * Message Number Log * * @see self::getLog() * @var array * @access private */ var $message_number_log = array(); /** * Message Log * * @see self::getLog() * @var array * @access private */ var $message_log = array(); /** * The Window Size * * Bytes the other party can send before it must wait for the window to be adjusted (0x7FFFFFFF = 2GB) * * @var int * @see self::_send_channel_packet() * @see self::exec() * @access private */ var $window_size = 0x7FFFFFFF; /** * Window size, server to client * * Window size indexed by channel * * @see self::_send_channel_packet() * @var array * @access private */ var $window_size_server_to_client = array(); /** * Window size, client to server * * Window size indexed by channel * * @see self::_get_channel_packet() * @var array * @access private */ var $window_size_client_to_server = array(); /** * Server signature * * Verified against $this->session_id * * @see self::getServerPublicHostKey() * @var string * @access private */ var $signature = ''; /** * Server signature format * * ssh-rsa or ssh-dss. * * @see self::getServerPublicHostKey() * @var string * @access private */ var $signature_format = ''; /** * Interactive Buffer * * @see self::read() * @var array * @access private */ var $interactiveBuffer = ''; /** * Current log size * * Should never exceed self::LOG_MAX_SIZE * * @see self::_send_binary_packet() * @see self::_get_binary_packet() * @var int * @access private */ var $log_size; /** * Timeout * * @see self::setTimeout() * @access private */ var $timeout; /** * Current Timeout * * @see self::_get_channel_packet() * @access private */ var $curTimeout; /** * Real-time log file pointer * * @see self::_append_log() * @var resource * @access private */ var $realtime_log_file; /** * Real-time log file size * * @see self::_append_log() * @var int * @access private */ var $realtime_log_size; /** * Has the signature been validated? * * @see self::getServerPublicHostKey() * @var bool * @access private */ var $signature_validated = false; /** * Real-time log file wrap boolean * * @see self::_append_log() * @access private */ var $realtime_log_wrap; /** * Flag to suppress stderr from output * * @see self::enableQuietMode() * @access private */ var $quiet_mode = false; /** * Time of first network activity * * @var int * @access private */ var $last_packet; /** * Exit status returned from ssh if any * * @var int * @access private */ var $exit_status; /** * Flag to request a PTY when using exec() * * @var bool * @see self::enablePTY() * @access private */ var $request_pty = false; /** * Flag set while exec() is running when using enablePTY() * * @var bool * @access private */ var $in_request_pty_exec = false; /** * Flag set after startSubsystem() is called * * @var bool * @access private */ var $in_subsystem; /** * Contents of stdError * * @var string * @access private */ var $stdErrorLog; /** * The Last Interactive Response * * @see self::_keyboard_interactive_process() * @var string * @access private */ var $last_interactive_response = ''; /** * Keyboard Interactive Request / Responses * * @see self::_keyboard_interactive_process() * @var array * @access private */ var $keyboard_requests_responses = array(); /** * Banner Message * * Quoting from the RFC, "in some jurisdictions, sending a warning message before * authentication may be relevant for getting legal protection." * * @see self::_filter() * @see self::getBannerMessage() * @var string * @access private */ var $banner_message = ''; /** * Did read() timeout or return normally? * * @see self::isTimeout() * @var bool * @access private */ var $is_timeout = false; /** * Log Boundary * * @see self::_format_log() * @var string * @access private */ var $log_boundary = ':'; /** * Log Long Width * * @see self::_format_log() * @var int * @access private */ var $log_long_width = 65; /** * Log Short Width * * @see self::_format_log() * @var int * @access private */ var $log_short_width = 16; /** * Hostname * * @see self::__construct() * @see self::_connect() * @var string * @access private */ var $host; /** * Port Number * * @see self::__construct() * @see self::_connect() * @var int * @access private */ var $port; /** * Number of columns for terminal window size * * @see self::getWindowColumns() * @see self::setWindowColumns() * @see self::setWindowSize() * @var int * @access private */ var $windowColumns = 80; /** * Number of columns for terminal window size * * @see self::getWindowRows() * @see self::setWindowRows() * @see self::setWindowSize() * @var int * @access private */ var $windowRows = 24; /** * Crypto Engine * * @see self::setCryptoEngine() * @see self::_key_exchange() * @var int * @access private */ var $crypto_engine = false; /** * A System_SSH_Agent for use in the SSH2 Agent Forwarding scenario * * @var System_SSH_Agent * @access private */ var $agent; /** * Send the identification string first? * * @var bool * @access private */ var $send_id_string_first = true; /** * Send the key exchange initiation packet first? * * @var bool * @access private */ var $send_kex_first = true; /** * Some versions of OpenSSH incorrectly calculate the key size * * @var bool * @access private */ var $bad_key_size_fix = false; /** * Should we try to re-connect to re-establish keys? * * @var bool * @access private */ var $retry_connect = false; /** * Binary Packet Buffer * * @var string|false * @access private */ var $binary_packet_buffer = false; /** * Preferred Signature Format * * @var string|false * @access private */ var $preferred_signature_format = false; /** * Authentication Credentials * * @var array * @access private */ var $auth = array(); /** * Default Constructor. * * $host can either be a string, representing the host, or a stream resource. * * @param mixed $host * @param int $port * @param int $timeout * @see self::login() * @return \phpseclib\Net\SSH2 * @access public */ function __construct($host, $port = 22, $timeout = 10) { $this->message_numbers = array( 1 => 'NET_SSH2_MSG_DISCONNECT', 2 => 'NET_SSH2_MSG_IGNORE', 3 => 'NET_SSH2_MSG_UNIMPLEMENTED', 4 => 'NET_SSH2_MSG_DEBUG', 5 => 'NET_SSH2_MSG_SERVICE_REQUEST', 6 => 'NET_SSH2_MSG_SERVICE_ACCEPT', 20 => 'NET_SSH2_MSG_KEXINIT', 21 => 'NET_SSH2_MSG_NEWKEYS', 30 => 'NET_SSH2_MSG_KEXDH_INIT', 31 => 'NET_SSH2_MSG_KEXDH_REPLY', 50 => 'NET_SSH2_MSG_USERAUTH_REQUEST', 51 => 'NET_SSH2_MSG_USERAUTH_FAILURE', 52 => 'NET_SSH2_MSG_USERAUTH_SUCCESS', 53 => 'NET_SSH2_MSG_USERAUTH_BANNER', 80 => 'NET_SSH2_MSG_GLOBAL_REQUEST', 81 => 'NET_SSH2_MSG_REQUEST_SUCCESS', 82 => 'NET_SSH2_MSG_REQUEST_FAILURE', 90 => 'NET_SSH2_MSG_CHANNEL_OPEN', 91 => 'NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION', 92 => 'NET_SSH2_MSG_CHANNEL_OPEN_FAILURE', 93 => 'NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST', 94 => 'NET_SSH2_MSG_CHANNEL_DATA', 95 => 'NET_SSH2_MSG_CHANNEL_EXTENDED_DATA', 96 => 'NET_SSH2_MSG_CHANNEL_EOF', 97 => 'NET_SSH2_MSG_CHANNEL_CLOSE', 98 => 'NET_SSH2_MSG_CHANNEL_REQUEST', 99 => 'NET_SSH2_MSG_CHANNEL_SUCCESS', 100 => 'NET_SSH2_MSG_CHANNEL_FAILURE' ); $this->disconnect_reasons = array( 1 => 'NET_SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT', 2 => 'NET_SSH2_DISCONNECT_PROTOCOL_ERROR', 3 => 'NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED', 4 => 'NET_SSH2_DISCONNECT_RESERVED', 5 => 'NET_SSH2_DISCONNECT_MAC_ERROR', 6 => 'NET_SSH2_DISCONNECT_COMPRESSION_ERROR', 7 => 'NET_SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE', 8 => 'NET_SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED', 9 => 'NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE', 10 => 'NET_SSH2_DISCONNECT_CONNECTION_LOST', 11 => 'NET_SSH2_DISCONNECT_BY_APPLICATION', 12 => 'NET_SSH2_DISCONNECT_TOO_MANY_CONNECTIONS', 13 => 'NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER', 14 => 'NET_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE', 15 => 'NET_SSH2_DISCONNECT_ILLEGAL_USER_NAME' ); $this->channel_open_failure_reasons = array( 1 => 'NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED' ); $this->terminal_modes = array( 0 => 'NET_SSH2_TTY_OP_END' ); $this->channel_extended_data_type_codes = array( 1 => 'NET_SSH2_EXTENDED_DATA_STDERR' ); $this->_define_array( $this->message_numbers, $this->disconnect_reasons, $this->channel_open_failure_reasons, $this->terminal_modes, $this->channel_extended_data_type_codes, array(60 => 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ'), array(60 => 'NET_SSH2_MSG_USERAUTH_PK_OK'), array(60 => 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST', 61 => 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE'), // RFC 4419 - diffie-hellman-group-exchange-sha{1,256} array(30 => 'NET_SSH2_MSG_KEXDH_GEX_REQUEST_OLD', 31 => 'NET_SSH2_MSG_KEXDH_GEX_GROUP', 32 => 'NET_SSH2_MSG_KEXDH_GEX_INIT', 33 => 'NET_SSH2_MSG_KEXDH_GEX_REPLY', 34 => 'NET_SSH2_MSG_KEXDH_GEX_REQUEST'), // RFC 5656 - Elliptic Curves (for curve25519-sha256@libssh.org) array(30 => 'NET_SSH2_MSG_KEX_ECDH_INIT', 31 => 'NET_SSH2_MSG_KEX_ECDH_REPLY') ); if (is_resource($host)) { $this->fsock = $host; return; } if (is_string($host)) { $this->host = $host; $this->port = $port; $this->timeout = $timeout; } } /** * Set Crypto Engine Mode * * Possible $engine values: * CRYPT_MODE_INTERNAL, CRYPT_MODE_MCRYPT * * @param int $engine * @access public */ function setCryptoEngine($engine) { $this->crypto_engine = $engine; } /** * Send Identification String First * * https://tools.ietf.org/html/rfc4253#section-4.2 says "when the connection has been established, * both sides MUST send an identification string". It does not say which side sends it first. In * theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy * * @access public */ function sendIdentificationStringFirst() { $this->send_id_string_first = true; } /** * Send Identification String Last * * https://tools.ietf.org/html/rfc4253#section-4.2 says "when the connection has been established, * both sides MUST send an identification string". It does not say which side sends it first. In * theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy * * @access public */ function sendIdentificationStringLast() { $this->send_id_string_first = false; } /** * Send SSH_MSG_KEXINIT First * * https://tools.ietf.org/html/rfc4253#section-7.1 says "key exchange begins by each sending * sending the [SSH_MSG_KEXINIT] packet". It does not say which side sends it first. In theory * it shouldn't matter but it is a fact of life that some SSH servers are simply buggy * * @access public */ function sendKEXINITFirst() { $this->send_kex_first = true; } /** * Send SSH_MSG_KEXINIT Last * * https://tools.ietf.org/html/rfc4253#section-7.1 says "key exchange begins by each sending * sending the [SSH_MSG_KEXINIT] packet". It does not say which side sends it first. In theory * it shouldn't matter but it is a fact of life that some SSH servers are simply buggy * * @access public */ function sendKEXINITLast() { $this->send_kex_first = false; } /** * Connect to an SSHv2 server * * @return bool * @access private */ function _connect() { if ($this->bitmap & self::MASK_CONSTRUCTOR) { return false; } $this->bitmap |= self::MASK_CONSTRUCTOR; $this->curTimeout = $this->timeout; $this->last_packet = microtime(true); if (!is_resource($this->fsock)) { $start = microtime(true); // with stream_select a timeout of 0 means that no timeout takes place; // with fsockopen a timeout of 0 means that you instantly timeout // to resolve this incompatibility a timeout of 100,000 will be used for fsockopen if timeout is 0 $this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->curTimeout == 0 ? 100000 : $this->curTimeout); if (!$this->fsock) { $host = $this->host . ':' . $this->port; user_error(rtrim("Cannot connect to $host. Error $errno. $errstr")); return false; } $elapsed = microtime(true) - $start; if ($this->curTimeout) { $this->curTimeout-= $elapsed; if ($this->curTimeout < 0) { $this->is_timeout = true; return false; } } } $this->identifier = $this->_generate_identifier(); if ($this->send_id_string_first) { fputs($this->fsock, $this->identifier . "\r\n"); } /* According to the SSH2 specs, "The server MAY send other lines of data before sending the version string. Each line SHOULD be terminated by a Carriage Return and Line Feed. Such lines MUST NOT begin with "SSH-", and SHOULD be encoded in ISO-10646 UTF-8 [RFC3629] (language is not specified). Clients MUST be able to process such lines." */ $data = ''; while (!feof($this->fsock) && !preg_match('#(.*)^(SSH-(\d\.\d+).*)#ms', $data, $matches)) { $line = ''; while (true) { if ($this->curTimeout) { if ($this->curTimeout < 0) { $this->is_timeout = true; return false; } $read = array($this->fsock); $write = $except = null; $start = microtime(true); $sec = floor($this->curTimeout); $usec = 1000000 * ($this->curTimeout - $sec); // on windows this returns a "Warning: Invalid CRT parameters detected" error // the !count() is done as a workaround for <https://bugs.php.net/42682> if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) { $this->is_timeout = true; return false; } $elapsed = microtime(true) - $start; $this->curTimeout-= $elapsed; } $temp = stream_get_line($this->fsock, 255, "\n"); if (strlen($temp) == 255) { continue; } $line.= "$temp\n"; // quoting RFC4253, "Implementers who wish to maintain // compatibility with older, undocumented versions of this protocol may // want to process the identification string without expecting the // presence of the carriage return character for reasons described in // Section 5 of this document." //if (substr($line, -2) == "\r\n") { // break; //} break; } $data.= $line; } if (feof($this->fsock)) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } $extra = $matches[1]; if (defined('NET_SSH2_LOGGING')) { $this->_append_log('<-', $matches[0]); $this->_append_log('->', $this->identifier . "\r\n"); } $this->server_identifier = trim($temp, "\r\n"); if (strlen($extra)) { $this->errors[] = $data; } if (version_compare($matches[3], '1.99', '<')) { user_error("Cannot connect to SSH $matches[3] servers"); return false; } if (!$this->send_id_string_first) { fputs($this->fsock, $this->identifier . "\r\n"); } if (!$this->send_kex_first) { $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (!strlen($response) || ord($response[0]) != NET_SSH2_MSG_KEXINIT) { user_error('Expected SSH_MSG_KEXINIT'); return false; } if (!$this->_key_exchange($response)) { return false; } } if ($this->send_kex_first && !$this->_key_exchange()) { return false; } $this->bitmap|= self::MASK_CONNECTED; return true; } /** * Generates the SSH identifier * * You should overwrite this method in your own class if you want to use another identifier * * @access protected * @return string */ function _generate_identifier() { $identifier = 'SSH-2.0-phpseclib_2.0'; $ext = array(); if (function_exists('sodium_crypto_box_publickey_from_secretkey')) { $ext[] = 'libsodium'; } if (extension_loaded('openssl')) { $ext[] = 'openssl'; } elseif (extension_loaded('mcrypt')) { $ext[] = 'mcrypt'; } if (extension_loaded('gmp')) { $ext[] = 'gmp'; } elseif (extension_loaded('bcmath')) { $ext[] = 'bcmath'; } if (!empty($ext)) { $identifier .= ' (' . implode(', ', $ext) . ')'; } return $identifier; } /** * Key Exchange * * @param string $kexinit_payload_server optional * @access private */ function _key_exchange($kexinit_payload_server = false) { $preferred = $this->preferred; $kex_algorithms = isset($preferred['kex']) ? $preferred['kex'] : $this->getSupportedKEXAlgorithms(); $server_host_key_algorithms = isset($preferred['hostkey']) ? $preferred['hostkey'] : $this->getSupportedHostKeyAlgorithms(); $s2c_encryption_algorithms = isset($preferred['server_to_client']['crypt']) ? $preferred['server_to_client']['crypt'] : $this->getSupportedEncryptionAlgorithms(); $c2s_encryption_algorithms = isset($preferred['client_to_server']['crypt']) ? $preferred['client_to_server']['crypt'] : $this->getSupportedEncryptionAlgorithms(); $s2c_mac_algorithms = isset($preferred['server_to_client']['mac']) ? $preferred['server_to_client']['mac'] : $this->getSupportedMACAlgorithms(); $c2s_mac_algorithms = isset($preferred['client_to_server']['mac']) ? $preferred['client_to_server']['mac'] : $this->getSupportedMACAlgorithms(); $s2c_compression_algorithms = isset($preferred['server_to_client']['comp']) ? $preferred['server_to_client']['comp'] : $this->getSupportedCompressionAlgorithms(); $c2s_compression_algorithms = isset($preferred['client_to_server']['comp']) ? $preferred['client_to_server']['comp'] : $this->getSupportedCompressionAlgorithms(); // some SSH servers have buggy implementations of some of the above algorithms switch (true) { case $this->server_identifier == 'SSH-2.0-SSHD': case substr($this->server_identifier, 0, 13) == 'SSH-2.0-DLINK': if (!isset($preferred['server_to_client']['mac'])) { $s2c_mac_algorithms = array_values(array_diff( $s2c_mac_algorithms, array('hmac-sha1-96', 'hmac-md5-96') )); } if (!isset($preferred['client_to_server']['mac'])) { $c2s_mac_algorithms = array_values(array_diff( $c2s_mac_algorithms, array('hmac-sha1-96', 'hmac-md5-96') )); } } $str_kex_algorithms = implode(',', $kex_algorithms); $str_server_host_key_algorithms = implode(',', $server_host_key_algorithms); $encryption_algorithms_server_to_client = implode(',', $s2c_encryption_algorithms); $encryption_algorithms_client_to_server = implode(',', $c2s_encryption_algorithms); $mac_algorithms_server_to_client = implode(',', $s2c_mac_algorithms); $mac_algorithms_client_to_server = implode(',', $c2s_mac_algorithms); $compression_algorithms_server_to_client = implode(',', $s2c_compression_algorithms); $compression_algorithms_client_to_server = implode(',', $c2s_compression_algorithms); $client_cookie = Random::string(16); $kexinit_payload_client = pack( 'Ca*Na*Na*Na*Na*Na*Na*Na*Na*Na*Na*CN', NET_SSH2_MSG_KEXINIT, $client_cookie, strlen($str_kex_algorithms), $str_kex_algorithms, strlen($str_server_host_key_algorithms), $str_server_host_key_algorithms, strlen($encryption_algorithms_client_to_server), $encryption_algorithms_client_to_server, strlen($encryption_algorithms_server_to_client), $encryption_algorithms_server_to_client, strlen($mac_algorithms_client_to_server), $mac_algorithms_client_to_server, strlen($mac_algorithms_server_to_client), $mac_algorithms_server_to_client, strlen($compression_algorithms_client_to_server), $compression_algorithms_client_to_server, strlen($compression_algorithms_server_to_client), $compression_algorithms_server_to_client, 0, '', 0, '', 0, 0 ); if ($this->send_kex_first) { if (!$this->_send_binary_packet($kexinit_payload_client)) { return false; } $kexinit_payload_server = $this->_get_binary_packet(); if ($kexinit_payload_server === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (!strlen($kexinit_payload_server) || ord($kexinit_payload_server[0]) != NET_SSH2_MSG_KEXINIT) { user_error('Expected SSH_MSG_KEXINIT'); return false; } } $response = $kexinit_payload_server; $this->_string_shift($response, 1); // skip past the message number (it should be SSH_MSG_KEXINIT) $server_cookie = $this->_string_shift($response, 16); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->kex_algorithms = explode(',', $this->_string_shift($response, $temp['length'])); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->server_host_key_algorithms = explode(',', $this->_string_shift($response, $temp['length'])); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->encryption_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length'])); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->encryption_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length'])); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->mac_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length'])); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->mac_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length'])); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->compression_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length'])); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->compression_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length'])); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->languages_client_to_server = explode(',', $this->_string_shift($response, $temp['length'])); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->languages_server_to_client = explode(',', $this->_string_shift($response, $temp['length'])); if (!strlen($response)) { return false; } extract(unpack('Cfirst_kex_packet_follows', $this->_string_shift($response, 1))); $first_kex_packet_follows = $first_kex_packet_follows != 0; if (!$this->send_kex_first && !$this->_send_binary_packet($kexinit_payload_client)) { return false; } // we need to decide upon the symmetric encryption algorithms before we do the diffie-hellman key exchange // we don't initialize any crypto-objects, yet - we do that, later. for now, we need the lengths to make the // diffie-hellman key exchange as fast as possible $decrypt = $this->_array_intersect_first($s2c_encryption_algorithms, $this->encryption_algorithms_server_to_client); $decryptKeyLength = $this->_encryption_algorithm_to_key_size($decrypt); if ($decryptKeyLength === null) { user_error('No compatible server to client encryption algorithms found'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } $encrypt = $this->_array_intersect_first($c2s_encryption_algorithms, $this->encryption_algorithms_client_to_server); $encryptKeyLength = $this->_encryption_algorithm_to_key_size($encrypt); if ($encryptKeyLength === null) { user_error('No compatible client to server encryption algorithms found'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } // through diffie-hellman key exchange a symmetric key is obtained $this->kex_algorithm = $kex_algorithm = $this->_array_intersect_first($kex_algorithms, $this->kex_algorithms); if ($kex_algorithm === false) { user_error('No compatible key exchange algorithms found'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } // Only relevant in diffie-hellman-group-exchange-sha{1,256}, otherwise empty. $exchange_hash_rfc4419 = ''; if ($kex_algorithm === 'curve25519-sha256@libssh.org') { $x = Random::string(32); $eBytes = sodium_crypto_box_publickey_from_secretkey($x); $clientKexInitMessage = NET_SSH2_MSG_KEX_ECDH_INIT; $serverKexReplyMessage = NET_SSH2_MSG_KEX_ECDH_REPLY; $kexHash = new Hash('sha256'); } else { if (strpos($kex_algorithm, 'diffie-hellman-group-exchange') === 0) { $dh_group_sizes_packed = pack( 'NNN', $this->kex_dh_group_size_min, $this->kex_dh_group_size_preferred, $this->kex_dh_group_size_max ); $packet = pack( 'Ca*', NET_SSH2_MSG_KEXDH_GEX_REQUEST, $dh_group_sizes_packed ); if (!$this->_send_binary_packet($packet)) { return false; } $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } extract(unpack('Ctype', $this->_string_shift($response, 1))); if ($type != NET_SSH2_MSG_KEXDH_GEX_GROUP) { user_error('Expected SSH_MSG_KEX_DH_GEX_GROUP'); return false; } if (strlen($response) < 4) { return false; } extract(unpack('NprimeLength', $this->_string_shift($response, 4))); $primeBytes = $this->_string_shift($response, $primeLength); $prime = new BigInteger($primeBytes, -256); if (strlen($response) < 4) { return false; } extract(unpack('NgLength', $this->_string_shift($response, 4))); $gBytes = $this->_string_shift($response, $gLength); $g = new BigInteger($gBytes, -256); $exchange_hash_rfc4419 = pack( 'a*Na*Na*', $dh_group_sizes_packed, $primeLength, $primeBytes, $gLength, $gBytes ); $clientKexInitMessage = NET_SSH2_MSG_KEXDH_GEX_INIT; $serverKexReplyMessage = NET_SSH2_MSG_KEXDH_GEX_REPLY; } else { switch ($kex_algorithm) { // see http://tools.ietf.org/html/rfc2409#section-6.2 and // http://tools.ietf.org/html/rfc2412, appendex E case 'diffie-hellman-group1-sha1': $prime = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' . '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' . '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' . 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF'; break; // see http://tools.ietf.org/html/rfc3526#section-3 case 'diffie-hellman-group14-sha1': $prime = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' . '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' . '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' . 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05' . '98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB' . '9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' . 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' . '3995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF'; break; } // For both diffie-hellman-group1-sha1 and diffie-hellman-group14-sha1 // the generator field element is 2 (decimal) and the hash function is sha1. $g = new BigInteger(2); $prime = new BigInteger($prime, 16); $clientKexInitMessage = NET_SSH2_MSG_KEXDH_INIT; $serverKexReplyMessage = NET_SSH2_MSG_KEXDH_REPLY; } switch ($kex_algorithm) { case 'diffie-hellman-group-exchange-sha256': $kexHash = new Hash('sha256'); break; default: $kexHash = new Hash('sha1'); } /* To increase the speed of the key exchange, both client and server may reduce the size of their private exponents. It should be at least twice as long as the key material that is generated from the shared secret. For more details, see the paper by van Oorschot and Wiener [VAN-OORSCHOT]. -- http://tools.ietf.org/html/rfc4419#section-6.2 */ $one = new BigInteger(1); $keyLength = min($kexHash->getLength(), max($encryptKeyLength, $decryptKeyLength)); $max = $one->bitwise_leftShift(16 * $keyLength); // 2 * 8 * $keyLength $max = $max->subtract($one); $x = $one->random($one, $max); $e = $g->modPow($x, $prime); $eBytes = $e->toBytes(true); } $data = pack('CNa*', $clientKexInitMessage, strlen($eBytes), $eBytes); if (!$this->_send_binary_packet($data)) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (!strlen($response)) { return false; } extract(unpack('Ctype', $this->_string_shift($response, 1))); if ($type != $serverKexReplyMessage) { user_error('Expected SSH_MSG_KEXDH_REPLY'); return false; } if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->server_public_host_key = $server_public_host_key = $this->_string_shift($response, $temp['length']); if (strlen($server_public_host_key) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $public_key_format = $this->_string_shift($server_public_host_key, $temp['length']); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $fBytes = $this->_string_shift($response, $temp['length']); if (strlen($response) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($response, 4)); $this->signature = $this->_string_shift($response, $temp['length']); if (strlen($this->signature) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($this->signature, 4)); $this->signature_format = $this->_string_shift($this->signature, $temp['length']); if ($kex_algorithm === 'curve25519-sha256@libssh.org') { if (strlen($fBytes) !== 32) { user_error('Received curve25519 public key of invalid length.'); return false; } $key = new BigInteger(sodium_crypto_scalarmult($x, $fBytes), 256); sodium_memzero($x); } else { $f = new BigInteger($fBytes, -256); $key = $f->modPow($x, $prime); } $keyBytes = $key->toBytes(true); $this->exchange_hash = pack( 'Na*Na*Na*Na*Na*a*Na*Na*Na*', strlen($this->identifier), $this->identifier, strlen($this->server_identifier), $this->server_identifier, strlen($kexinit_payload_client), $kexinit_payload_client, strlen($kexinit_payload_server), $kexinit_payload_server, strlen($this->server_public_host_key), $this->server_public_host_key, $exchange_hash_rfc4419, strlen($eBytes), $eBytes, strlen($fBytes), $fBytes, strlen($keyBytes), $keyBytes ); $this->exchange_hash = $kexHash->hash($this->exchange_hash); if ($this->session_id === false) { $this->session_id = $this->exchange_hash; } $server_host_key_algorithm = $this->_array_intersect_first($server_host_key_algorithms, $this->server_host_key_algorithms); if ($server_host_key_algorithm === false) { user_error('No compatible server host key algorithms found'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } switch ($server_host_key_algorithm) { case 'ssh-dss': $expected_key_format = 'ssh-dss'; break; //case 'rsa-sha2-256': //case 'rsa-sha2-512': //case 'ssh-rsa': default: $expected_key_format = 'ssh-rsa'; } if ($public_key_format != $expected_key_format || $this->signature_format != $server_host_key_algorithm) { switch (true) { case $this->signature_format == $server_host_key_algorithm: case $server_host_key_algorithm != 'rsa-sha2-256' && $server_host_key_algorithm != 'rsa-sha2-512': case $this->signature_format != 'ssh-rsa': user_error('Server Host Key Algorithm Mismatch'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } } $packet = pack( 'C', NET_SSH2_MSG_NEWKEYS ); if (!$this->_send_binary_packet($packet)) { return false; } $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (!strlen($response)) { return false; } extract(unpack('Ctype', $this->_string_shift($response, 1))); if ($type != NET_SSH2_MSG_NEWKEYS) { user_error('Expected SSH_MSG_NEWKEYS'); return false; } $keyBytes = pack('Na*', strlen($keyBytes), $keyBytes); $this->encrypt = $this->_encryption_algorithm_to_crypt_instance($encrypt); if ($this->encrypt) { if ($this->crypto_engine) { $this->encrypt->setPreferredEngine($this->crypto_engine); } if ($this->encrypt->block_size) { $this->encrypt_block_size = $this->encrypt->block_size; } $this->encrypt->enableContinuousBuffer(); $this->encrypt->disablePadding(); if ($this->encrypt->getBlockLength()) { $this->encrypt_block_size = $this->encrypt->getBlockLength() >> 3; } $iv = $kexHash->hash($keyBytes . $this->exchange_hash . 'A' . $this->session_id); while ($this->encrypt_block_size > strlen($iv)) { $iv.= $kexHash->hash($keyBytes . $this->exchange_hash . $iv); } $this->encrypt->setIV(substr($iv, 0, $this->encrypt_block_size)); $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'C' . $this->session_id); while ($encryptKeyLength > strlen($key)) { $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key); } $this->encrypt->setKey(substr($key, 0, $encryptKeyLength)); $this->encrypt->name = $decrypt; } $this->decrypt = $this->_encryption_algorithm_to_crypt_instance($decrypt); if ($this->decrypt) { if ($this->crypto_engine) { $this->decrypt->setPreferredEngine($this->crypto_engine); } if ($this->decrypt->block_size) { $this->decrypt_block_size = $this->decrypt->block_size; } $this->decrypt->enableContinuousBuffer(); $this->decrypt->disablePadding(); if ($this->decrypt->getBlockLength()) { $this->decrypt_block_size = $this->decrypt->getBlockLength() >> 3; } $iv = $kexHash->hash($keyBytes . $this->exchange_hash . 'B' . $this->session_id); while ($this->decrypt_block_size > strlen($iv)) { $iv.= $kexHash->hash($keyBytes . $this->exchange_hash . $iv); } $this->decrypt->setIV(substr($iv, 0, $this->decrypt_block_size)); $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'D' . $this->session_id); while ($decryptKeyLength > strlen($key)) { $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key); } $this->decrypt->setKey(substr($key, 0, $decryptKeyLength)); $this->decrypt->name = $decrypt; } /* The "arcfour128" algorithm is the RC4 cipher, as described in [SCHNEIER], using a 128-bit key. The first 1536 bytes of keystream generated by the cipher MUST be discarded, and the first byte of the first encrypted packet MUST be encrypted using the 1537th byte of keystream. -- http://tools.ietf.org/html/rfc4345#section-4 */ if ($encrypt == 'arcfour128' || $encrypt == 'arcfour256') { $this->encrypt->encrypt(str_repeat("\0", 1536)); } if ($decrypt == 'arcfour128' || $decrypt == 'arcfour256') { $this->decrypt->decrypt(str_repeat("\0", 1536)); } $mac_algorithm = $this->_array_intersect_first($c2s_mac_algorithms, $this->mac_algorithms_client_to_server); if ($mac_algorithm === false) { user_error('No compatible client to server message authentication algorithms found'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } $createKeyLength = 0; // ie. $mac_algorithm == 'none' switch ($mac_algorithm) { case 'hmac-sha2-256': $this->hmac_create = new Hash('sha256'); $createKeyLength = 32; break; case 'hmac-sha1': $this->hmac_create = new Hash('sha1'); $createKeyLength = 20; break; case 'hmac-sha1-96': $this->hmac_create = new Hash('sha1-96'); $createKeyLength = 20; break; case 'hmac-md5': $this->hmac_create = new Hash('md5'); $createKeyLength = 16; break; case 'hmac-md5-96': $this->hmac_create = new Hash('md5-96'); $createKeyLength = 16; } $this->hmac_create->name = $mac_algorithm; $mac_algorithm = $this->_array_intersect_first($s2c_mac_algorithms, $this->mac_algorithms_server_to_client); if ($mac_algorithm === false) { user_error('No compatible server to client message authentication algorithms found'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } $checkKeyLength = 0; $this->hmac_size = 0; switch ($mac_algorithm) { case 'hmac-sha2-256': $this->hmac_check = new Hash('sha256'); $checkKeyLength = 32; $this->hmac_size = 32; break; case 'hmac-sha1': $this->hmac_check = new Hash('sha1'); $checkKeyLength = 20; $this->hmac_size = 20; break; case 'hmac-sha1-96': $this->hmac_check = new Hash('sha1-96'); $checkKeyLength = 20; $this->hmac_size = 12; break; case 'hmac-md5': $this->hmac_check = new Hash('md5'); $checkKeyLength = 16; $this->hmac_size = 16; break; case 'hmac-md5-96': $this->hmac_check = new Hash('md5-96'); $checkKeyLength = 16; $this->hmac_size = 12; } $this->hmac_check->name = $mac_algorithm; $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'E' . $this->session_id); while ($createKeyLength > strlen($key)) { $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key); } $this->hmac_create->setKey(substr($key, 0, $createKeyLength)); $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'F' . $this->session_id); while ($checkKeyLength > strlen($key)) { $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key); } $this->hmac_check->setKey(substr($key, 0, $checkKeyLength)); $compression_algorithm = $this->_array_intersect_first($c2s_compression_algorithms, $this->compression_algorithms_client_to_server); if ($compression_algorithm === false) { user_error('No compatible client to server compression algorithms found'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } //$this->decompress = $compression_algorithm == 'zlib'; $compression_algorithm = $this->_array_intersect_first($s2c_compression_algorithms, $this->compression_algorithms_client_to_server); if ($compression_algorithm === false) { user_error('No compatible server to client compression algorithms found'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } //$this->compress = $compression_algorithm == 'zlib'; return true; } /** * Maps an encryption algorithm name to the number of key bytes. * * @param string $algorithm Name of the encryption algorithm * @return int|null Number of bytes as an integer or null for unknown * @access private */ function _encryption_algorithm_to_key_size($algorithm) { if ($this->bad_key_size_fix && $this->_bad_algorithm_candidate($algorithm)) { return 16; } switch ($algorithm) { case 'none': return 0; case 'aes128-cbc': case 'aes128-ctr': case 'arcfour': case 'arcfour128': case 'blowfish-cbc': case 'blowfish-ctr': case 'twofish128-cbc': case 'twofish128-ctr': return 16; case '3des-cbc': case '3des-ctr': case 'aes192-cbc': case 'aes192-ctr': case 'twofish192-cbc': case 'twofish192-ctr': return 24; case 'aes256-cbc': case 'aes256-ctr': case 'arcfour256': case 'twofish-cbc': case 'twofish256-cbc': case 'twofish256-ctr': return 32; } return null; } /** * Maps an encryption algorithm name to an instance of a subclass of * \phpseclib\Crypt\Base. * * @param string $algorithm Name of the encryption algorithm * @return mixed Instance of \phpseclib\Crypt\Base or null for unknown * @access private */ function _encryption_algorithm_to_crypt_instance($algorithm) { switch ($algorithm) { case '3des-cbc': return new TripleDES(); case '3des-ctr': return new TripleDES(Base::MODE_CTR); case 'aes256-cbc': case 'aes192-cbc': case 'aes128-cbc': return new Rijndael(); case 'aes256-ctr': case 'aes192-ctr': case 'aes128-ctr': return new Rijndael(Base::MODE_CTR); case 'blowfish-cbc': return new Blowfish(); case 'blowfish-ctr': return new Blowfish(Base::MODE_CTR); case 'twofish128-cbc': case 'twofish192-cbc': case 'twofish256-cbc': case 'twofish-cbc': return new Twofish(); case 'twofish128-ctr': case 'twofish192-ctr': case 'twofish256-ctr': return new Twofish(Base::MODE_CTR); case 'arcfour': case 'arcfour128': case 'arcfour256': return new RC4(); } return null; } /** * Tests whether or not proposed algorithm has a potential for issues * * @link https://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/ssh2-aesctr-openssh.html * @link https://bugzilla.mindrot.org/show_bug.cgi?id=1291 * @param string $algorithm Name of the encryption algorithm * @return bool * @access private */ function _bad_algorithm_candidate($algorithm) { switch ($algorithm) { case 'arcfour256': case 'aes192-ctr': case 'aes256-ctr': return true; } return false; } /** * Login * * The $password parameter can be a plaintext password, a \phpseclib\Crypt\RSA object or an array * * @param string $username * @param mixed $password * @param mixed $... * @return bool * @see self::_login() * @access public */ function login($username) { $args = func_get_args(); $this->auth[] = $args; return call_user_func_array(array(&$this, '_login'), $args); } /** * Login Helper * * @param string $username * @param mixed $password * @param mixed $... * @return bool * @see self::_login_helper() * @access private */ function _login($username) { if (!($this->bitmap & self::MASK_CONSTRUCTOR)) { if (!$this->_connect()) { return false; } } $args = array_slice(func_get_args(), 1); if (empty($args)) { return $this->_login_helper($username); } foreach ($args as $arg) { if ($this->_login_helper($username, $arg)) { return true; } } return false; } /** * Login Helper * * @param string $username * @param string $password * @return bool * @access private * @internal It might be worthwhile, at some point, to protect against {@link http://tools.ietf.org/html/rfc4251#section-9.3.9 traffic analysis} * by sending dummy SSH_MSG_IGNORE messages. */ function _login_helper($username, $password = null) { if (!($this->bitmap & self::MASK_CONNECTED)) { return false; } if (!($this->bitmap & self::MASK_LOGIN_REQ)) { $packet = pack( 'CNa*', NET_SSH2_MSG_SERVICE_REQUEST, strlen('ssh-userauth'), 'ssh-userauth' ); if (!$this->_send_binary_packet($packet)) { return false; } $response = $this->_get_binary_packet(); if ($response === false) { if ($this->retry_connect) { $this->retry_connect = false; if (!$this->_connect()) { return false; } return $this->_login_helper($username, $password); } $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (strlen($response) < 4) { return false; } extract(unpack('Ctype', $this->_string_shift($response, 1))); if ($type != NET_SSH2_MSG_SERVICE_ACCEPT) { user_error('Expected SSH_MSG_SERVICE_ACCEPT'); return false; } $this->bitmap |= self::MASK_LOGIN_REQ; } if (strlen($this->last_interactive_response)) { return !is_string($password) && !is_array($password) ? false : $this->_keyboard_interactive_process($password); } if ($password instanceof RSA) { return $this->_privatekey_login($username, $password); } elseif ($password instanceof Agent) { return $this->_ssh_agent_login($username, $password); } if (is_array($password)) { if ($this->_keyboard_interactive_login($username, $password)) { $this->bitmap |= self::MASK_LOGIN; return true; } return false; } if (!isset($password)) { $packet = pack( 'CNa*Na*Na*', NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection', strlen('none'), 'none' ); if (!$this->_send_binary_packet($packet)) { return false; } $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (!strlen($response)) { return false; } extract(unpack('Ctype', $this->_string_shift($response, 1))); switch ($type) { case NET_SSH2_MSG_USERAUTH_SUCCESS: $this->bitmap |= self::MASK_LOGIN; return true; //case NET_SSH2_MSG_USERAUTH_FAILURE: default: return false; } } $packet = pack( 'CNa*Na*Na*CNa*', NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection', strlen('password'), 'password', 0, strlen($password), $password ); // remove the username and password from the logged packet if (!defined('NET_SSH2_LOGGING')) { $logged = null; } else { $logged = pack( 'CNa*Na*Na*CNa*', NET_SSH2_MSG_USERAUTH_REQUEST, strlen('username'), 'username', strlen('ssh-connection'), 'ssh-connection', strlen('password'), 'password', 0, strlen('password'), 'password' ); } if (!$this->_send_binary_packet($packet, $logged)) { return false; } $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (!strlen($response)) { return false; } extract(unpack('Ctype', $this->_string_shift($response, 1))); switch ($type) { case NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ: // in theory, the password can be changed if (defined('NET_SSH2_LOGGING')) { $this->message_number_log[count($this->message_number_log) - 1] = 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ'; } if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $this->errors[] = 'SSH_MSG_USERAUTH_PASSWD_CHANGEREQ: ' . $this->_string_shift($response, $length); return $this->_disconnect(NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER); case NET_SSH2_MSG_USERAUTH_FAILURE: // can we use keyboard-interactive authentication? if not then either the login is bad or the server employees // multi-factor authentication if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $auth_methods = explode(',', $this->_string_shift($response, $length)); if (!strlen($response)) { return false; } extract(unpack('Cpartial_success', $this->_string_shift($response, 1))); $partial_success = $partial_success != 0; if (!$partial_success && in_array('keyboard-interactive', $auth_methods)) { if ($this->_keyboard_interactive_login($username, $password)) { $this->bitmap |= self::MASK_LOGIN; return true; } return false; } return false; case NET_SSH2_MSG_USERAUTH_SUCCESS: $this->bitmap |= self::MASK_LOGIN; return true; } return false; } /** * Login via keyboard-interactive authentication * * See {@link http://tools.ietf.org/html/rfc4256 RFC4256} for details. This is not a full-featured keyboard-interactive authenticator. * * @param string $username * @param string $password * @return bool * @access private */ function _keyboard_interactive_login($username, $password) { $packet = pack( 'CNa*Na*Na*Na*Na*', NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection', strlen('keyboard-interactive'), 'keyboard-interactive', 0, '', 0, '' ); if (!$this->_send_binary_packet($packet)) { return false; } return $this->_keyboard_interactive_process($password); } /** * Handle the keyboard-interactive requests / responses. * * @param string $responses... * @return bool * @access private */ function _keyboard_interactive_process() { $responses = func_get_args(); if (strlen($this->last_interactive_response)) { $response = $this->last_interactive_response; } else { $orig = $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } } if (!strlen($response)) { return false; } extract(unpack('Ctype', $this->_string_shift($response, 1))); switch ($type) { case NET_SSH2_MSG_USERAUTH_INFO_REQUEST: if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $this->_string_shift($response, $length); // name; may be empty if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $this->_string_shift($response, $length); // instruction; may be empty if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $this->_string_shift($response, $length); // language tag; may be empty if (strlen($response) < 4) { return false; } extract(unpack('Nnum_prompts', $this->_string_shift($response, 4))); for ($i = 0; $i < count($responses); $i++) { if (is_array($responses[$i])) { foreach ($responses[$i] as $key => $value) { $this->keyboard_requests_responses[$key] = $value; } unset($responses[$i]); } } $responses = array_values($responses); if (isset($this->keyboard_requests_responses)) { for ($i = 0; $i < $num_prompts; $i++) { if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); // prompt - ie. "Password: "; must not be empty $prompt = $this->_string_shift($response, $length); //$echo = $this->_string_shift($response) != chr(0); foreach ($this->keyboard_requests_responses as $key => $value) { if (substr($prompt, 0, strlen($key)) == $key) { $responses[] = $value; break; } } } } // see http://tools.ietf.org/html/rfc4256#section-3.2 if (strlen($this->last_interactive_response)) { $this->last_interactive_response = ''; } elseif (defined('NET_SSH2_LOGGING')) { $this->message_number_log[count($this->message_number_log) - 1] = str_replace( 'UNKNOWN', 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST', $this->message_number_log[count($this->message_number_log) - 1] ); } if (!count($responses) && $num_prompts) { $this->last_interactive_response = $orig; return false; } /* After obtaining the requested information from the user, the client MUST respond with an SSH_MSG_USERAUTH_INFO_RESPONSE message. */ // see http://tools.ietf.org/html/rfc4256#section-3.4 $packet = $logged = pack('CN', NET_SSH2_MSG_USERAUTH_INFO_RESPONSE, count($responses)); for ($i = 0; $i < count($responses); $i++) { $packet.= pack('Na*', strlen($responses[$i]), $responses[$i]); $logged.= pack('Na*', strlen('dummy-answer'), 'dummy-answer'); } if (!$this->_send_binary_packet($packet, $logged)) { return false; } if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == self::LOG_COMPLEX) { $this->message_number_log[count($this->message_number_log) - 1] = str_replace( 'UNKNOWN', 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE', $this->message_number_log[count($this->message_number_log) - 1] ); } /* After receiving the response, the server MUST send either an SSH_MSG_USERAUTH_SUCCESS, SSH_MSG_USERAUTH_FAILURE, or another SSH_MSG_USERAUTH_INFO_REQUEST message. */ // maybe phpseclib should force close the connection after x request / responses? unless something like that is done // there could be an infinite loop of request / responses. return $this->_keyboard_interactive_process(); case NET_SSH2_MSG_USERAUTH_SUCCESS: return true; case NET_SSH2_MSG_USERAUTH_FAILURE: return false; } return false; } /** * Login with an ssh-agent provided key * * @param string $username * @param \phpseclib\System\SSH\Agent $agent * @return bool * @access private */ function _ssh_agent_login($username, $agent) { $this->agent = $agent; $keys = $agent->requestIdentities(); foreach ($keys as $key) { if ($this->_privatekey_login($username, $key)) { return true; } } return false; } /** * Login with an RSA private key * * @param string $username * @param \phpseclib\Crypt\RSA $password * @return bool * @access private * @internal It might be worthwhile, at some point, to protect against {@link http://tools.ietf.org/html/rfc4251#section-9.3.9 traffic analysis} * by sending dummy SSH_MSG_IGNORE messages. */ function _privatekey_login($username, $privatekey) { // see http://tools.ietf.org/html/rfc4253#page-15 $publickey = $privatekey->getPublicKey(RSA::PUBLIC_FORMAT_RAW); if ($publickey === false) { return false; } $publickey = array( 'e' => $publickey['e']->toBytes(true), 'n' => $publickey['n']->toBytes(true) ); $publickey = pack( 'Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publickey['e']), $publickey['e'], strlen($publickey['n']), $publickey['n'] ); switch ($this->signature_format) { case 'rsa-sha2-512': $hash = 'sha512'; $signatureType = 'rsa-sha2-512'; break; case 'rsa-sha2-256': $hash = 'sha256'; $signatureType = 'rsa-sha2-256'; break; //case 'ssh-rsa': default: $hash = 'sha1'; $signatureType = 'ssh-rsa'; } $part1 = pack( 'CNa*Na*Na*', NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection', strlen('publickey'), 'publickey' ); $part2 = pack('Na*Na*', strlen($signatureType), $signatureType, strlen($publickey), $publickey); $packet = $part1 . chr(0) . $part2; if (!$this->_send_binary_packet($packet)) { return false; } $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (!strlen($response)) { return false; } extract(unpack('Ctype', $this->_string_shift($response, 1))); switch ($type) { case NET_SSH2_MSG_USERAUTH_FAILURE: if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $this->errors[] = 'SSH_MSG_USERAUTH_FAILURE: ' . $this->_string_shift($response, $length); return false; case NET_SSH2_MSG_USERAUTH_PK_OK: // we'll just take it on faith that the public key blob and the public key algorithm name are as // they should be if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == self::LOG_COMPLEX) { $this->message_number_log[count($this->message_number_log) - 1] = str_replace( 'UNKNOWN', 'NET_SSH2_MSG_USERAUTH_PK_OK', $this->message_number_log[count($this->message_number_log) - 1] ); } } $packet = $part1 . chr(1) . $part2; $privatekey->setSignatureMode(RSA::SIGNATURE_PKCS1); $privatekey->setHash($hash); $signature = $privatekey->sign(pack('Na*a*', strlen($this->session_id), $this->session_id, $packet)); $signature = pack('Na*Na*', strlen($signatureType), $signatureType, strlen($signature), $signature); $packet.= pack('Na*', strlen($signature), $signature); if (!$this->_send_binary_packet($packet)) { return false; } $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (!strlen($response)) { return false; } extract(unpack('Ctype', $this->_string_shift($response, 1))); switch ($type) { case NET_SSH2_MSG_USERAUTH_FAILURE: // either the login is bad or the server employs multi-factor authentication return false; case NET_SSH2_MSG_USERAUTH_SUCCESS: $this->bitmap |= self::MASK_LOGIN; return true; } return false; } /** * Set Timeout * * $ssh->exec('ping 127.0.0.1'); on a Linux host will never return and will run indefinitely. setTimeout() makes it so it'll timeout. * Setting $timeout to false or 0 will mean there is no timeout. * * @param mixed $timeout * @access public */ function setTimeout($timeout) { $this->timeout = $this->curTimeout = $timeout; } /** * Get the output from stdError * * @access public */ function getStdError() { return $this->stdErrorLog; } /** * Execute Command * * If $callback is set to false then \phpseclib\Net\SSH2::_get_channel_packet(self::CHANNEL_EXEC) will need to be called manually. * In all likelihood, this is not a feature you want to be taking advantage of. * * @param string $command * @param Callback $callback * @return string * @access public */ function exec($command, $callback = null) { $this->curTimeout = $this->timeout; $this->is_timeout = false; $this->stdErrorLog = ''; if (!$this->isAuthenticated()) { return false; } if ($this->in_request_pty_exec) { user_error('If you want to run multiple exec()\'s you will need to disable (and re-enable if appropriate) a PTY for each one.'); return false; } // RFC4254 defines the (client) window size as "bytes the other party can send before it must wait for the window to // be adjusted". 0x7FFFFFFF is, at 2GB, the max size. technically, it should probably be decremented, but, // honestly, if you're transferring more than 2GB, you probably shouldn't be using phpseclib, anyway. // see http://tools.ietf.org/html/rfc4254#section-5.2 for more info $this->window_size_server_to_client[self::CHANNEL_EXEC] = $this->window_size; // 0x8000 is the maximum max packet size, per http://tools.ietf.org/html/rfc4253#section-6.1, although since PuTTy // uses 0x4000, that's what will be used here, as well. $packet_size = 0x4000; $packet = pack( 'CNa*N3', NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', self::CHANNEL_EXEC, $this->window_size_server_to_client[self::CHANNEL_EXEC], $packet_size ); if (!$this->_send_binary_packet($packet)) { return false; } $this->channel_status[self::CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_OPEN; $response = $this->_get_channel_packet(self::CHANNEL_EXEC); if ($response === false) { return false; } if ($this->request_pty === true) { $terminal_modes = pack('C', NET_SSH2_TTY_OP_END); $packet = pack( 'CNNa*CNa*N5a*', NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL_EXEC], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100', $this->windowColumns, $this->windowRows, 0, 0, strlen($terminal_modes), $terminal_modes ); if (!$this->_send_binary_packet($packet)) { return false; } $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (!strlen($response)) { return false; } list(, $type) = unpack('C', $this->_string_shift($response, 1)); switch ($type) { case NET_SSH2_MSG_CHANNEL_SUCCESS: break; case NET_SSH2_MSG_CHANNEL_FAILURE: default: user_error('Unable to request pseudo-terminal'); return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); } $this->in_request_pty_exec = true; } // sending a pty-req SSH_MSG_CHANNEL_REQUEST message is unnecessary and, in fact, in most cases, slows things // down. the one place where it might be desirable is if you're doing something like \phpseclib\Net\SSH2::exec('ping localhost &'). // with a pty-req SSH_MSG_CHANNEL_REQUEST, exec() will return immediately and the ping process will then // then immediately terminate. without such a request exec() will loop indefinitely. the ping process won't end but // neither will your script. // although, in theory, the size of SSH_MSG_CHANNEL_REQUEST could exceed the maximum packet size established by // SSH_MSG_CHANNEL_OPEN_CONFIRMATION, RFC4254#section-5.1 states that the "maximum packet size" refers to the // "maximum size of an individual data packet". ie. SSH_MSG_CHANNEL_DATA. RFC4254#section-5.2 corroborates. $packet = pack( 'CNNa*CNa*', NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL_EXEC], strlen('exec'), 'exec', 1, strlen($command), $command ); if (!$this->_send_binary_packet($packet)) { return false; } $this->channel_status[self::CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_REQUEST; $response = $this->_get_channel_packet(self::CHANNEL_EXEC); if ($response === false) { return false; } $this->channel_status[self::CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_DATA; if ($callback === false || $this->in_request_pty_exec) { return true; } $output = ''; while (true) { $temp = $this->_get_channel_packet(self::CHANNEL_EXEC); switch (true) { case $temp === true: return is_callable($callback) ? true : $output; case $temp === false: return false; default: if (is_callable($callback)) { if (call_user_func($callback, $temp) === true) { $this->_close_channel(self::CHANNEL_EXEC); return true; } } else { $output.= $temp; } } } } /** * Creates an interactive shell * * @see self::read() * @see self::write() * @return bool * @access private */ function _initShell() { if ($this->in_request_pty_exec === true) { return true; } $this->window_size_server_to_client[self::CHANNEL_SHELL] = $this->window_size; $packet_size = 0x4000; $packet = pack( 'CNa*N3', NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', self::CHANNEL_SHELL, $this->window_size_server_to_client[self::CHANNEL_SHELL], $packet_size ); if (!$this->_send_binary_packet($packet)) { return false; } $this->channel_status[self::CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_OPEN; $response = $this->_get_channel_packet(self::CHANNEL_SHELL); if ($response === false) { return false; } $terminal_modes = pack('C', NET_SSH2_TTY_OP_END); $packet = pack( 'CNNa*CNa*N5a*', NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL_SHELL], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100', $this->windowColumns, $this->windowRows, 0, 0, strlen($terminal_modes), $terminal_modes ); if (!$this->_send_binary_packet($packet)) { return false; } $response = $this->_get_binary_packet(); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } if (!strlen($response)) { return false; } list(, $type) = unpack('C', $this->_string_shift($response, 1)); switch ($type) { case NET_SSH2_MSG_CHANNEL_SUCCESS: // if a pty can't be opened maybe commands can still be executed case NET_SSH2_MSG_CHANNEL_FAILURE: break; default: user_error('Unable to request pseudo-terminal'); return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); } $packet = pack( 'CNNa*C', NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL_SHELL], strlen('shell'), 'shell', 1 ); if (!$this->_send_binary_packet($packet)) { return false; } $this->channel_status[self::CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_REQUEST; $response = $this->_get_channel_packet(self::CHANNEL_SHELL); if ($response === false) { return false; } $this->channel_status[self::CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_DATA; $this->bitmap |= self::MASK_SHELL; return true; } /** * Return the channel to be used with read() / write() * * @see self::read() * @see self::write() * @return int * @access public */ function _get_interactive_channel() { switch (true) { case $this->in_subsystem: return self::CHANNEL_SUBSYSTEM; case $this->in_request_pty_exec: return self::CHANNEL_EXEC; default: return self::CHANNEL_SHELL; } } /** * Return an available open channel * * @return int * @access public */ function _get_open_channel() { $channel = self::CHANNEL_EXEC; do { if (isset($this->channel_status[$channel]) && $this->channel_status[$channel] == NET_SSH2_MSG_CHANNEL_OPEN) { return $channel; } } while ($channel++ < self::CHANNEL_SUBSYSTEM); return false; } /** * Returns the output of an interactive shell * * Returns when there's a match for $expect, which can take the form of a string literal or, * if $mode == self::READ_REGEX, a regular expression. * * @see self::write() * @param string $expect * @param int $mode * @return string * @access public */ function read($expect = '', $mode = self::READ_SIMPLE) { $this->curTimeout = $this->timeout; $this->is_timeout = false; if (!$this->isAuthenticated()) { user_error('Operation disallowed prior to login()'); return false; } if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) { user_error('Unable to initiate an interactive shell session'); return false; } $channel = $this->_get_interactive_channel(); if ($mode == self::READ_NEXT) { return $this->_get_channel_packet($channel); } $match = $expect; while (true) { if ($mode == self::READ_REGEX) { preg_match($expect, substr($this->interactiveBuffer, -1024), $matches); $match = isset($matches[0]) ? $matches[0] : ''; } $pos = strlen($match) ? strpos($this->interactiveBuffer, $match) : false; if ($pos !== false) { return $this->_string_shift($this->interactiveBuffer, $pos + strlen($match)); } $response = $this->_get_channel_packet($channel); if (is_bool($response)) { $this->in_request_pty_exec = false; return $response ? $this->_string_shift($this->interactiveBuffer, strlen($this->interactiveBuffer)) : false; } $this->interactiveBuffer.= $response; } } /** * Inputs a command into an interactive shell. * * @see self::read() * @param string $cmd * @return bool * @access public */ function write($cmd) { if (!$this->isAuthenticated()) { user_error('Operation disallowed prior to login()'); return false; } if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) { user_error('Unable to initiate an interactive shell session'); return false; } return $this->_send_channel_packet($this->_get_interactive_channel(), $cmd); } /** * Start a subsystem. * * Right now only one subsystem at a time is supported. To support multiple subsystem's stopSubsystem() could accept * a string that contained the name of the subsystem, but at that point, only one subsystem of each type could be opened. * To support multiple subsystem's of the same name maybe it'd be best if startSubsystem() generated a new channel id and * returns that and then that that was passed into stopSubsystem() but that'll be saved for a future date and implemented * if there's sufficient demand for such a feature. * * @see self::stopSubsystem() * @param string $subsystem * @return bool * @access public */ function startSubsystem($subsystem) { $this->window_size_server_to_client[self::CHANNEL_SUBSYSTEM] = $this->window_size; $packet = pack( 'CNa*N3', NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', self::CHANNEL_SUBSYSTEM, $this->window_size, 0x4000 ); if (!$this->_send_binary_packet($packet)) { return false; } $this->channel_status[self::CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_OPEN; $response = $this->_get_channel_packet(self::CHANNEL_SUBSYSTEM); if ($response === false) { return false; } $packet = pack( 'CNNa*CNa*', NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL_SUBSYSTEM], strlen('subsystem'), 'subsystem', 1, strlen($subsystem), $subsystem ); if (!$this->_send_binary_packet($packet)) { return false; } $this->channel_status[self::CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_REQUEST; $response = $this->_get_channel_packet(self::CHANNEL_SUBSYSTEM); if ($response === false) { return false; } $this->channel_status[self::CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_DATA; $this->bitmap |= self::MASK_SHELL; $this->in_subsystem = true; return true; } /** * Stops a subsystem. * * @see self::startSubsystem() * @return bool * @access public */ function stopSubsystem() { $this->in_subsystem = false; $this->_close_channel(self::CHANNEL_SUBSYSTEM); return true; } /** * Closes a channel * * If read() timed out you might want to just close the channel and have it auto-restart on the next read() call * * @access public */ function reset() { $this->_close_channel($this->_get_interactive_channel()); } /** * Is timeout? * * Did exec() or read() return because they timed out or because they encountered the end? * * @access public */ function isTimeout() { return $this->is_timeout; } /** * Disconnect * * @access public */ function disconnect() { $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); if (isset($this->realtime_log_file) && is_resource($this->realtime_log_file)) { fclose($this->realtime_log_file); } } /** * Destructor. * * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call * disconnect(). * * @access public */ function __destruct() { $this->disconnect(); } /** * Is the connection still active? * * @return bool * @access public */ function isConnected() { return (bool) ($this->bitmap & self::MASK_CONNECTED); } /** * Have you successfully been logged in? * * @return bool * @access public */ function isAuthenticated() { return (bool) ($this->bitmap & self::MASK_LOGIN); } /** * Pings a server connection, or tries to reconnect if the connection has gone down * * Inspired by http://php.net/manual/en/mysqli.ping.php * * @return bool * @access public */ function ping() { if (!$this->isAuthenticated()) { if (!empty($this->auth)) { return $this->_reconnect(); } return false; } $this->window_size_server_to_client[self::CHANNEL_KEEP_ALIVE] = $this->window_size; $packet_size = 0x4000; $packet = pack( 'CNa*N3', NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', self::CHANNEL_KEEP_ALIVE, $this->window_size_server_to_client[self::CHANNEL_KEEP_ALIVE], $packet_size ); if (!@$this->_send_binary_packet($packet)) { return $this->_reconnect(); } $this->channel_status[self::CHANNEL_KEEP_ALIVE] = NET_SSH2_MSG_CHANNEL_OPEN; $response = @$this->_get_channel_packet(self::CHANNEL_KEEP_ALIVE); if ($response !== false) { $this->_close_channel(self::CHANNEL_KEEP_ALIVE); return true; } return $this->_reconnect(); } /** * In situ reconnect method * * @return boolean * @access private */ function _reconnect() { $this->_reset_connection(NET_SSH2_DISCONNECT_CONNECTION_LOST); $this->retry_connect = true; if (!$this->_connect()) { return false; } foreach ($this->auth as $auth) { $result = call_user_func_array(array(&$this, 'login'), $auth); } return $result; } /** * Resets a connection for re-use * * @param int $reason * @access private */ function _reset_connection($reason) { $this->_disconnect($reason); $this->decrypt = $this->encrypt = false; $this->decrypt_block_size = $this->encrypt_block_size = 8; $this->hmac_check = $this->hmac_create = false; $this->hmac_size = false; $this->session_id = false; $this->retry_connect = true; $this->get_seq_no = $this->send_seq_no = 0; } /** * Gets Binary Packets * * See '6. Binary Packet Protocol' of rfc4253 for more info. * * @see self::_send_binary_packet() * @return string * @access private */ function _get_binary_packet($skip_channel_filter = false) { if (!is_resource($this->fsock) || feof($this->fsock)) { $this->bitmap = 0; user_error('Connection closed prematurely'); return false; } $start = microtime(true); $raw = stream_get_contents($this->fsock, $this->decrypt_block_size); if (!strlen($raw)) { return ''; } if ($this->decrypt !== false) { $raw = $this->decrypt->decrypt($raw); } if ($raw === false) { user_error('Unable to decrypt content'); return false; } if (strlen($raw) < 5) { return false; } extract(unpack('Npacket_length/Cpadding_length', $this->_string_shift($raw, 5))); $remaining_length = $packet_length + 4 - $this->decrypt_block_size; // quoting <http://tools.ietf.org/html/rfc4253#section-6.1>, // "implementations SHOULD check that the packet length is reasonable" // PuTTY uses 0x9000 as the actual max packet size and so to shall we if ($remaining_length < -$this->decrypt_block_size || $remaining_length > 0x9000 || $remaining_length % $this->decrypt_block_size != 0) { if (!$this->bad_key_size_fix && $this->_bad_algorithm_candidate($this->decrypt->name) && !($this->bitmap & SSH2::MASK_LOGIN)) { $this->bad_key_size_fix = true; $this->_reset_connection(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); return false; } user_error('Invalid size'); return false; } $buffer = ''; while ($remaining_length > 0) { $temp = stream_get_contents($this->fsock, $remaining_length); if ($temp === false || feof($this->fsock)) { $this->bitmap = 0; user_error('Error reading from socket'); return false; } $buffer.= $temp; $remaining_length-= strlen($temp); } $stop = microtime(true); if (strlen($buffer)) { $raw.= $this->decrypt !== false ? $this->decrypt->decrypt($buffer) : $buffer; } $payload = $this->_string_shift($raw, $packet_length - $padding_length - 1); $padding = $this->_string_shift($raw, $padding_length); // should leave $raw empty if ($this->hmac_check !== false) { $hmac = stream_get_contents($this->fsock, $this->hmac_size); if ($hmac === false || strlen($hmac) != $this->hmac_size) { $this->bitmap = 0; user_error('Error reading socket'); return false; } elseif ($hmac != $this->hmac_check->hash(pack('NNCa*', $this->get_seq_no, $packet_length, $padding_length, $payload . $padding))) { user_error('Invalid HMAC'); return false; } } //if ($this->decompress) { // $payload = gzinflate(substr($payload, 2)); //} $this->get_seq_no++; if (defined('NET_SSH2_LOGGING')) { $current = microtime(true); $message_number = isset($this->message_numbers[ord($payload[0])]) ? $this->message_numbers[ord($payload[0])] : 'UNKNOWN (' . ord($payload[0]) . ')'; $message_number = '<- ' . $message_number . ' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)'; $this->_append_log($message_number, $payload); $this->last_packet = $current; } return $this->_filter($payload, $skip_channel_filter); } /** * Filter Binary Packets * * Because some binary packets need to be ignored... * * @see self::_get_binary_packet() * @return string * @access private */ function _filter($payload, $skip_channel_filter) { switch (ord($payload[0])) { case NET_SSH2_MSG_DISCONNECT: $this->_string_shift($payload, 1); if (strlen($payload) < 8) { return false; } extract(unpack('Nreason_code/Nlength', $this->_string_shift($payload, 8))); $this->errors[] = 'SSH_MSG_DISCONNECT: ' . $this->disconnect_reasons[$reason_code] . "\r\n" . $this->_string_shift($payload, $length); $this->bitmap = 0; return false; case NET_SSH2_MSG_IGNORE: $payload = $this->_get_binary_packet($skip_channel_filter); break; case NET_SSH2_MSG_DEBUG: $this->_string_shift($payload, 2); if (strlen($payload) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($payload, 4))); $this->errors[] = 'SSH_MSG_DEBUG: ' . $this->_string_shift($payload, $length); $payload = $this->_get_binary_packet($skip_channel_filter); break; case NET_SSH2_MSG_UNIMPLEMENTED: return false; case NET_SSH2_MSG_KEXINIT: if ($this->session_id !== false) { $this->send_kex_first = false; if (!$this->_key_exchange($payload)) { $this->bitmap = 0; return false; } $payload = $this->_get_binary_packet($skip_channel_filter); } } // see http://tools.ietf.org/html/rfc4252#section-5.4; only called when the encryption has been activated and when we haven't already logged in if (($this->bitmap & self::MASK_CONNECTED) && !$this->isAuthenticated() && ord($payload[0]) == NET_SSH2_MSG_USERAUTH_BANNER) { $this->_string_shift($payload, 1); if (strlen($payload) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($payload, 4))); $this->banner_message = $this->_string_shift($payload, $length); $payload = $this->_get_binary_packet(); } // only called when we've already logged in if (($this->bitmap & self::MASK_CONNECTED) && $this->isAuthenticated()) { switch (ord($payload[0])) { case NET_SSH2_MSG_CHANNEL_DATA: case NET_SSH2_MSG_CHANNEL_EXTENDED_DATA: case NET_SSH2_MSG_CHANNEL_REQUEST: case NET_SSH2_MSG_CHANNEL_CLOSE: case NET_SSH2_MSG_CHANNEL_EOF: if (!$skip_channel_filter && !empty($this->server_channels)) { $this->binary_packet_buffer = $payload; $this->_get_channel_packet(true); $payload = $this->_get_binary_packet(); } break; case NET_SSH2_MSG_GLOBAL_REQUEST: // see http://tools.ietf.org/html/rfc4254#section-4 if (strlen($payload) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($payload, 4))); $this->errors[] = 'SSH_MSG_GLOBAL_REQUEST: ' . $this->_string_shift($payload, $length); if (!$this->_send_binary_packet(pack('C', NET_SSH2_MSG_REQUEST_FAILURE))) { return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); } $payload = $this->_get_binary_packet($skip_channel_filter); break; case NET_SSH2_MSG_CHANNEL_OPEN: // see http://tools.ietf.org/html/rfc4254#section-5.1 $this->_string_shift($payload, 1); if (strlen($payload) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($payload, 4))); $data = $this->_string_shift($payload, $length); if (strlen($payload) < 4) { return false; } extract(unpack('Nserver_channel', $this->_string_shift($payload, 4))); switch ($data) { case 'auth-agent': case 'auth-agent@openssh.com': if (isset($this->agent)) { $new_channel = self::CHANNEL_AGENT_FORWARD; if (strlen($payload) < 8) { return false; } extract(unpack('Nremote_window_size', $this->_string_shift($payload, 4))); extract(unpack('Nremote_maximum_packet_size', $this->_string_shift($payload, 4))); $this->packet_size_client_to_server[$new_channel] = $remote_window_size; $this->window_size_server_to_client[$new_channel] = $remote_maximum_packet_size; $this->window_size_client_to_server[$new_channel] = $this->window_size; $packet_size = 0x4000; $packet = pack( 'CN4', NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, $server_channel, $new_channel, $packet_size, $packet_size ); $this->server_channels[$new_channel] = $server_channel; $this->channel_status[$new_channel] = NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION; if (!$this->_send_binary_packet($packet)) { return false; } } break; default: $packet = pack( 'CN3a*Na*', NET_SSH2_MSG_REQUEST_FAILURE, $server_channel, NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED, 0, '', 0, '' ); if (!$this->_send_binary_packet($packet)) { return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); } } $payload = $this->_get_binary_packet($skip_channel_filter); break; case NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST: $this->_string_shift($payload, 1); if (strlen($payload) < 8) { return false; } extract(unpack('Nchannel', $this->_string_shift($payload, 4))); extract(unpack('Nwindow_size', $this->_string_shift($payload, 4))); $this->window_size_client_to_server[$channel]+= $window_size; $payload = ($this->bitmap & self::MASK_WINDOW_ADJUST) ? true : $this->_get_binary_packet($skip_channel_filter); } } return $payload; } /** * Enable Quiet Mode * * Suppress stderr from output * * @access public */ function enableQuietMode() { $this->quiet_mode = true; } /** * Disable Quiet Mode * * Show stderr in output * * @access public */ function disableQuietMode() { $this->quiet_mode = false; } /** * Returns whether Quiet Mode is enabled or not * * @see self::enableQuietMode() * @see self::disableQuietMode() * @access public * @return bool */ function isQuietModeEnabled() { return $this->quiet_mode; } /** * Enable request-pty when using exec() * * @access public */ function enablePTY() { $this->request_pty = true; } /** * Disable request-pty when using exec() * * @access public */ function disablePTY() { if ($this->in_request_pty_exec) { $this->_close_channel(self::CHANNEL_EXEC); $this->in_request_pty_exec = false; } $this->request_pty = false; } /** * Returns whether request-pty is enabled or not * * @see self::enablePTY() * @see self::disablePTY() * @access public * @return bool */ function isPTYEnabled() { return $this->request_pty; } /** * Gets channel data * * Returns the data as a string if it's available and false if not. * * @param $client_channel * @return mixed * @access private */ function _get_channel_packet($client_channel, $skip_extended = false) { if (!empty($this->channel_buffers[$client_channel])) { return array_shift($this->channel_buffers[$client_channel]); } while (true) { if ($this->binary_packet_buffer !== false) { $response = $this->binary_packet_buffer; $this->binary_packet_buffer = false; } else { $read = array($this->fsock); $write = $except = null; if (!$this->curTimeout) { @stream_select($read, $write, $except, null); } else { if ($this->curTimeout < 0) { $this->is_timeout = true; return true; } $read = array($this->fsock); $write = $except = null; $start = microtime(true); $sec = floor($this->curTimeout); $usec = 1000000 * ($this->curTimeout - $sec); // on windows this returns a "Warning: Invalid CRT parameters detected" error if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) { $this->is_timeout = true; if ($client_channel == self::CHANNEL_EXEC && !$this->request_pty) { $this->_close_channel($client_channel); } return true; } $elapsed = microtime(true) - $start; $this->curTimeout-= $elapsed; } $response = $this->_get_binary_packet(true); if ($response === false) { $this->bitmap = 0; user_error('Connection closed by server'); return false; } } if ($client_channel == -1 && $response === true) { return true; } if (!strlen($response)) { return false; } extract(unpack('Ctype', $this->_string_shift($response, 1))); if (strlen($response) < 4) { return false; } if ($type == NET_SSH2_MSG_CHANNEL_OPEN) { extract(unpack('Nlength', $this->_string_shift($response, 4))); } else { extract(unpack('Nchannel', $this->_string_shift($response, 4))); } // will not be setup yet on incoming channel open request if (isset($channel) && isset($this->channel_status[$channel]) && isset($this->window_size_server_to_client[$channel])) { $this->window_size_server_to_client[$channel]-= strlen($response); // resize the window, if appropriate if ($this->window_size_server_to_client[$channel] < 0) { $packet = pack('CNN', NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST, $this->server_channels[$channel], $this->window_size); if (!$this->_send_binary_packet($packet)) { return false; } $this->window_size_server_to_client[$channel]+= $this->window_size; } switch ($type) { case NET_SSH2_MSG_CHANNEL_EXTENDED_DATA: /* if ($client_channel == self::CHANNEL_EXEC) { $this->_send_channel_packet($client_channel, chr(0)); } */ // currently, there's only one possible value for $data_type_code: NET_SSH2_EXTENDED_DATA_STDERR if (strlen($response) < 8) { return false; } extract(unpack('Ndata_type_code/Nlength', $this->_string_shift($response, 8))); $data = $this->_string_shift($response, $length); $this->stdErrorLog.= $data; if ($skip_extended || $this->quiet_mode) { continue 2; } if ($client_channel == $channel && $this->channel_status[$channel] == NET_SSH2_MSG_CHANNEL_DATA) { return $data; } if (!isset($this->channel_buffers[$channel])) { $this->channel_buffers[$channel] = array(); } $this->channel_buffers[$channel][] = $data; continue 2; case NET_SSH2_MSG_CHANNEL_REQUEST: if ($this->channel_status[$channel] == NET_SSH2_MSG_CHANNEL_CLOSE) { continue 2; } if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $value = $this->_string_shift($response, $length); switch ($value) { case 'exit-signal': $this->_string_shift($response, 1); if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $this->errors[] = 'SSH_MSG_CHANNEL_REQUEST (exit-signal): ' . $this->_string_shift($response, $length); $this->_string_shift($response, 1); if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); if ($length) { $this->errors[count($this->errors)].= "\r\n" . $this->_string_shift($response, $length); } $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$client_channel])); $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel])); $this->channel_status[$channel] = NET_SSH2_MSG_CHANNEL_EOF; continue 3; case 'exit-status': if (strlen($response) < 5) { return false; } extract(unpack('Cfalse/Nexit_status', $this->_string_shift($response, 5))); $this->exit_status = $exit_status; // "The client MAY ignore these messages." // -- http://tools.ietf.org/html/rfc4254#section-6.10 continue 3; default: // "Some systems may not implement signals, in which case they SHOULD ignore this message." // -- http://tools.ietf.org/html/rfc4254#section-6.9 continue 3; } } switch ($this->channel_status[$channel]) { case NET_SSH2_MSG_CHANNEL_OPEN: switch ($type) { case NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: if (strlen($response) < 4) { return false; } extract(unpack('Nserver_channel', $this->_string_shift($response, 4))); $this->server_channels[$channel] = $server_channel; if (strlen($response) < 4) { return false; } extract(unpack('Nwindow_size', $this->_string_shift($response, 4))); if ($window_size < 0) { $window_size&= 0x7FFFFFFF; $window_size+= 0x80000000; } $this->window_size_client_to_server[$channel] = $window_size; if (strlen($response) < 4) { return false; } $temp = unpack('Npacket_size_client_to_server', $this->_string_shift($response, 4)); $this->packet_size_client_to_server[$channel] = $temp['packet_size_client_to_server']; $result = $client_channel == $channel ? true : $this->_get_channel_packet($client_channel, $skip_extended); $this->_on_channel_open(); return $result; //case NET_SSH2_MSG_CHANNEL_OPEN_FAILURE: default: user_error('Unable to open channel'); return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); } break; case NET_SSH2_MSG_CHANNEL_REQUEST: switch ($type) { case NET_SSH2_MSG_CHANNEL_SUCCESS: return true; case NET_SSH2_MSG_CHANNEL_FAILURE: return false; default: user_error('Unable to fulfill channel request'); return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); } case NET_SSH2_MSG_CHANNEL_CLOSE: return $type == NET_SSH2_MSG_CHANNEL_CLOSE ? true : $this->_get_channel_packet($client_channel, $skip_extended); } } // ie. $this->channel_status[$channel] == NET_SSH2_MSG_CHANNEL_DATA switch ($type) { case NET_SSH2_MSG_CHANNEL_DATA: /* if ($channel == self::CHANNEL_EXEC) { // SCP requires null packets, such as this, be sent. further, in the case of the ssh.com SSH server // this actually seems to make things twice as fast. more to the point, the message right after // SSH_MSG_CHANNEL_DATA (usually SSH_MSG_IGNORE) won't block for as long as it would have otherwise. // in OpenSSH it slows things down but only by a couple thousandths of a second. $this->_send_channel_packet($channel, chr(0)); } */ if (strlen($response) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($response, 4))); $data = $this->_string_shift($response, $length); if ($channel == self::CHANNEL_AGENT_FORWARD) { $agent_response = $this->agent->_forward_data($data); if (!is_bool($agent_response)) { $this->_send_channel_packet($channel, $agent_response); } break; } if ($client_channel == $channel) { return $data; } if (!isset($this->channel_buffers[$channel])) { $this->channel_buffers[$channel] = array(); } $this->channel_buffers[$channel][] = $data; break; case NET_SSH2_MSG_CHANNEL_CLOSE: $this->curTimeout = 0; if ($this->bitmap & self::MASK_SHELL) { $this->bitmap&= ~self::MASK_SHELL; } if ($this->channel_status[$channel] != NET_SSH2_MSG_CHANNEL_EOF) { $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel])); } $this->channel_status[$channel] = NET_SSH2_MSG_CHANNEL_CLOSE; if ($client_channel == $channel) { return true; } case NET_SSH2_MSG_CHANNEL_EOF: break; default: user_error('Error reading channel data'); return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); } } } /** * Sends Binary Packets * * See '6. Binary Packet Protocol' of rfc4253 for more info. * * @param string $data * @param string $logged * @see self::_get_binary_packet() * @return bool * @access private */ function _send_binary_packet($data, $logged = null) { if (!is_resource($this->fsock) || feof($this->fsock)) { $this->bitmap = 0; user_error('Connection closed prematurely'); return false; } //if ($this->compress) { // // the -4 removes the checksum: // // http://php.net/function.gzcompress#57710 // $data = substr(gzcompress($data), 0, -4); //} // 4 (packet length) + 1 (padding length) + 4 (minimal padding amount) == 9 $packet_length = strlen($data) + 9; // round up to the nearest $this->encrypt_block_size $packet_length+= (($this->encrypt_block_size - 1) * $packet_length) % $this->encrypt_block_size; // subtracting strlen($data) is obvious - subtracting 5 is necessary because of packet_length and padding_length $padding_length = $packet_length - strlen($data) - 5; $padding = Random::string($padding_length); // we subtract 4 from packet_length because the packet_length field isn't supposed to include itself $packet = pack('NCa*', $packet_length - 4, $padding_length, $data . $padding); $hmac = $this->hmac_create !== false ? $this->hmac_create->hash(pack('Na*', $this->send_seq_no, $packet)) : ''; $this->send_seq_no++; if ($this->encrypt !== false) { $packet = $this->encrypt->encrypt($packet); } $packet.= $hmac; $start = microtime(true); $result = strlen($packet) == fputs($this->fsock, $packet); $stop = microtime(true); if (defined('NET_SSH2_LOGGING')) { $current = microtime(true); $message_number = isset($this->message_numbers[ord($data[0])]) ? $this->message_numbers[ord($data[0])] : 'UNKNOWN (' . ord($data[0]) . ')'; $message_number = '-> ' . $message_number . ' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)'; $this->_append_log($message_number, isset($logged) ? $logged : $data); $this->last_packet = $current; } return $result; } /** * Logs data packets * * Makes sure that only the last 1MB worth of packets will be logged * * @param string $data * @access private */ function _append_log($message_number, $message) { // remove the byte identifying the message type from all but the first two messages (ie. the identification strings) if (strlen($message_number) > 2) { $this->_string_shift($message); } switch (NET_SSH2_LOGGING) { // useful for benchmarks case self::LOG_SIMPLE: $this->message_number_log[] = $message_number; break; // the most useful log for SSH2 case self::LOG_COMPLEX: $this->message_number_log[] = $message_number; $this->log_size+= strlen($message); $this->message_log[] = $message; while ($this->log_size > self::LOG_MAX_SIZE) { $this->log_size-= strlen(array_shift($this->message_log)); array_shift($this->message_number_log); } break; // dump the output out realtime; packets may be interspersed with non packets, // passwords won't be filtered out and select other packets may not be correctly // identified case self::LOG_REALTIME: switch (PHP_SAPI) { case 'cli': $start = $stop = "\r\n"; break; default: $start = '<pre>'; $stop = '</pre>'; } echo $start . $this->_format_log(array($message), array($message_number)) . $stop; @flush(); @ob_flush(); break; // basically the same thing as self::LOG_REALTIME with the caveat that self::LOG_REALTIME_FILE // needs to be defined and that the resultant log file will be capped out at self::LOG_MAX_SIZE. // the earliest part of the log file is denoted by the first <<< START >>> and is not going to necessarily // at the beginning of the file case self::LOG_REALTIME_FILE: if (!isset($this->realtime_log_file)) { // PHP doesn't seem to like using constants in fopen() $filename = self::LOG_REALTIME_FILENAME; $fp = fopen($filename, 'w'); $this->realtime_log_file = $fp; } if (!is_resource($this->realtime_log_file)) { break; } $entry = $this->_format_log(array($message), array($message_number)); if ($this->realtime_log_wrap) { $temp = "<<< START >>>\r\n"; $entry.= $temp; fseek($this->realtime_log_file, ftell($this->realtime_log_file) - strlen($temp)); } $this->realtime_log_size+= strlen($entry); if ($this->realtime_log_size > self::LOG_MAX_SIZE) { fseek($this->realtime_log_file, 0); $this->realtime_log_size = strlen($entry); $this->realtime_log_wrap = true; } fputs($this->realtime_log_file, $entry); } } /** * Sends channel data * * Spans multiple SSH_MSG_CHANNEL_DATAs if appropriate * * @param int $client_channel * @param string $data * @return bool * @access private */ function _send_channel_packet($client_channel, $data) { while (strlen($data)) { if (!$this->window_size_client_to_server[$client_channel]) { $this->bitmap^= self::MASK_WINDOW_ADJUST; // using an invalid channel will let the buffers be built up for the valid channels $this->_get_channel_packet(-1); $this->bitmap^= self::MASK_WINDOW_ADJUST; } /* The maximum amount of data allowed is determined by the maximum packet size for the channel, and the current window size, whichever is smaller. -- http://tools.ietf.org/html/rfc4254#section-5.2 */ $max_size = min( $this->packet_size_client_to_server[$client_channel], $this->window_size_client_to_server[$client_channel] ); $temp = $this->_string_shift($data, $max_size); $packet = pack( 'CN2a*', NET_SSH2_MSG_CHANNEL_DATA, $this->server_channels[$client_channel], strlen($temp), $temp ); $this->window_size_client_to_server[$client_channel]-= strlen($temp); if (!$this->_send_binary_packet($packet)) { return false; } } return true; } /** * Closes and flushes a channel * * \phpseclib\Net\SSH2 doesn't properly close most channels. For exec() channels are normally closed by the server * and for SFTP channels are presumably closed when the client disconnects. This functions is intended * for SCP more than anything. * * @param int $client_channel * @param bool $want_reply * @return bool * @access private */ function _close_channel($client_channel, $want_reply = false) { // see http://tools.ietf.org/html/rfc4254#section-5.3 $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$client_channel])); if (!$want_reply) { $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel])); } $this->channel_status[$client_channel] = NET_SSH2_MSG_CHANNEL_CLOSE; $this->curTimeout = 0; while (!is_bool($this->_get_channel_packet($client_channel))) { } if ($want_reply) { $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel])); } if ($this->bitmap & self::MASK_SHELL) { $this->bitmap&= ~self::MASK_SHELL; } } /** * Disconnect * * @param int $reason * @return bool * @access private */ function _disconnect($reason) { if ($this->bitmap & self::MASK_CONNECTED) { $data = pack('CNNa*Na*', NET_SSH2_MSG_DISCONNECT, $reason, 0, '', 0, ''); $this->_send_binary_packet($data); } $this->bitmap = 0; if (is_resource($this->fsock) && get_resource_type($this->fsock) == 'stream') { fclose($this->fsock); } return false; } /** * String Shift * * Inspired by array_shift * * @param string $string * @param int $index * @return string * @access private */ function _string_shift(&$string, $index = 1) { $substr = substr($string, 0, $index); $string = substr($string, $index); return $substr; } /** * Define Array * * Takes any number of arrays whose indices are integers and whose values are strings and defines a bunch of * named constants from it, using the value as the name of the constant and the index as the value of the constant. * If any of the constants that would be defined already exists, none of the constants will be defined. * * @param array $array * @access private */ function _define_array() { $args = func_get_args(); foreach ($args as $arg) { foreach ($arg as $key => $value) { if (!defined($value)) { define($value, $key); } else { break 2; } } } } /** * Returns a log of the packets that have been sent and received. * * Returns a string if NET_SSH2_LOGGING == self::LOG_COMPLEX, an array if NET_SSH2_LOGGING == self::LOG_SIMPLE and false if !defined('NET_SSH2_LOGGING') * * @access public * @return array|false|string */ function getLog() { if (!defined('NET_SSH2_LOGGING')) { return false; } switch (NET_SSH2_LOGGING) { case self::LOG_SIMPLE: return $this->message_number_log; case self::LOG_COMPLEX: $log = $this->_format_log($this->message_log, $this->message_number_log); return PHP_SAPI == 'cli' ? $log : '<pre>' . $log . '</pre>'; default: return false; } } /** * Formats a log for printing * * @param array $message_log * @param array $message_number_log * @access private * @return string */ function _format_log($message_log, $message_number_log) { $output = ''; for ($i = 0; $i < count($message_log); $i++) { $output.= $message_number_log[$i] . "\r\n"; $current_log = $message_log[$i]; $j = 0; do { if (strlen($current_log)) { $output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 '; } $fragment = $this->_string_shift($current_log, $this->log_short_width); $hex = substr(preg_replace_callback('#.#s', array($this, '_format_log_helper'), $fragment), strlen($this->log_boundary)); // replace non ASCII printable characters with dots // http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters // also replace < with a . since < messes up the output on web browsers $raw = preg_replace('#[^\x20-\x7E]|<#', '.', $fragment); $output.= str_pad($hex, $this->log_long_width - $this->log_short_width, ' ') . $raw . "\r\n"; $j++; } while (strlen($current_log)); $output.= "\r\n"; } return $output; } /** * Helper function for _format_log * * For use with preg_replace_callback() * * @param array $matches * @access private * @return string */ function _format_log_helper($matches) { return $this->log_boundary . str_pad(dechex(ord($matches[0])), 2, '0', STR_PAD_LEFT); } /** * Helper function for agent->_on_channel_open() * * Used when channels are created to inform agent * of said channel opening. Must be called after * channel open confirmation received * * @access private */ function _on_channel_open() { if (isset($this->agent)) { $this->agent->_on_channel_open($this); } } /** * Returns the first value of the intersection of two arrays or false if * the intersection is empty. The order is defined by the first parameter. * * @param array $array1 * @param array $array2 * @return mixed False if intersection is empty, else intersected value. * @access private */ function _array_intersect_first($array1, $array2) { foreach ($array1 as $value) { if (in_array($value, $array2)) { return $value; } } return false; } /** * Returns all errors * * @return string[] * @access public */ function getErrors() { return $this->errors; } /** * Returns the last error * * @return string * @access public */ function getLastError() { $count = count($this->errors); if ($count > 0) { return $this->errors[$count - 1]; } } /** * Return the server identification. * * @return string * @access public */ function getServerIdentification() { $this->_connect(); return $this->server_identifier; } /** * Return a list of the key exchange algorithms the server supports. * * @return array * @access public */ function getKexAlgorithms() { $this->_connect(); return $this->kex_algorithms; } /** * Return a list of the host key (public key) algorithms the server supports. * * @return array * @access public */ function getServerHostKeyAlgorithms() { $this->_connect(); return $this->server_host_key_algorithms; } /** * Return a list of the (symmetric key) encryption algorithms the server supports, when receiving stuff from the client. * * @return array * @access public */ function getEncryptionAlgorithmsClient2Server() { $this->_connect(); return $this->encryption_algorithms_client_to_server; } /** * Return a list of the (symmetric key) encryption algorithms the server supports, when sending stuff to the client. * * @return array * @access public */ function getEncryptionAlgorithmsServer2Client() { $this->_connect(); return $this->encryption_algorithms_server_to_client; } /** * Return a list of the MAC algorithms the server supports, when receiving stuff from the client. * * @return array * @access public */ function getMACAlgorithmsClient2Server() { $this->_connect(); return $this->mac_algorithms_client_to_server; } /** * Return a list of the MAC algorithms the server supports, when sending stuff to the client. * * @return array * @access public */ function getMACAlgorithmsServer2Client() { $this->_connect(); return $this->mac_algorithms_server_to_client; } /** * Return a list of the compression algorithms the server supports, when receiving stuff from the client. * * @return array * @access public */ function getCompressionAlgorithmsClient2Server() { $this->_connect(); return $this->compression_algorithms_client_to_server; } /** * Return a list of the compression algorithms the server supports, when sending stuff to the client. * * @return array * @access public */ function getCompressionAlgorithmsServer2Client() { $this->_connect(); return $this->compression_algorithms_server_to_client; } /** * Return a list of the languages the server supports, when sending stuff to the client. * * @return array * @access public */ function getLanguagesServer2Client() { $this->_connect(); return $this->languages_server_to_client; } /** * Return a list of the languages the server supports, when receiving stuff from the client. * * @return array * @access public */ function getLanguagesClient2Server() { $this->_connect(); return $this->languages_client_to_server; } /** * Returns a list of algorithms the server supports * * @return array * @access public */ public function getServerAlgorithms() { $this->_connect(); return array( 'kex' => $this->kex_algorithms, 'hostkey' => $this->server_host_key_algorithms, 'client_to_server' => array( 'crypt' => $this->encryption_algorithms_client_to_server, 'mac' => $this->mac_algorithms_client_to_server, 'comp' => $this->compression_algorithms_client_to_server, 'lang' => $this->languages_client_to_server ), 'server_to_client' => array( 'crypt' => $this->encryption_algorithms_server_to_client, 'mac' => $this->mac_algorithms_server_to_client, 'comp' => $this->compression_algorithms_server_to_client, 'lang' => $this->languages_server_to_client ) ); } /** * Returns a list of KEX algorithms that phpseclib supports * * @return array * @access public */ function getSupportedKEXAlgorithms() { $kex_algorithms = array( // Elliptic Curve Diffie-Hellman Key Agreement (ECDH) using // Curve25519. See doc/curve25519-sha256@libssh.org.txt in the // libssh repository for more information. 'curve25519-sha256@libssh.org', 'diffie-hellman-group-exchange-sha256',// RFC 4419 'diffie-hellman-group-exchange-sha1', // RFC 4419 // Diffie-Hellman Key Agreement (DH) using integer modulo prime // groups. 'diffie-hellman-group14-sha1', // REQUIRED 'diffie-hellman-group1-sha1', // REQUIRED ); if (!function_exists('sodium_crypto_box_publickey_from_secretkey')) { $kex_algorithms = array_diff( $kex_algorithms, array('curve25519-sha256@libssh.org') ); } return $kex_algorithms; } /** * Returns a list of host key algorithms that phpseclib supports * * @return array * @access public */ function getSupportedHostKeyAlgorithms() { return array( 'rsa-sha2-256', // RFC 8332 'rsa-sha2-512', // RFC 8332 'ssh-rsa', // RECOMMENDED sign Raw RSA Key 'ssh-dss' // REQUIRED sign Raw DSS Key ); } /** * Returns a list of symmetric key algorithms that phpseclib supports * * @return array * @access public */ function getSupportedEncryptionAlgorithms() { $algos = array( // from <http://tools.ietf.org/html/rfc4345#section-4>: 'arcfour256', 'arcfour128', //'arcfour', // OPTIONAL the ARCFOUR stream cipher with a 128-bit key // CTR modes from <http://tools.ietf.org/html/rfc4344#section-4>: 'aes128-ctr', // RECOMMENDED AES (Rijndael) in SDCTR mode, with 128-bit key 'aes192-ctr', // RECOMMENDED AES with 192-bit key 'aes256-ctr', // RECOMMENDED AES with 256-bit key 'twofish128-ctr', // OPTIONAL Twofish in SDCTR mode, with 128-bit key 'twofish192-ctr', // OPTIONAL Twofish with 192-bit key 'twofish256-ctr', // OPTIONAL Twofish with 256-bit key 'aes128-cbc', // RECOMMENDED AES with a 128-bit key 'aes192-cbc', // OPTIONAL AES with a 192-bit key 'aes256-cbc', // OPTIONAL AES in CBC mode, with a 256-bit key 'twofish128-cbc', // OPTIONAL Twofish with a 128-bit key 'twofish192-cbc', // OPTIONAL Twofish with a 192-bit key 'twofish256-cbc', 'twofish-cbc', // OPTIONAL alias for "twofish256-cbc" // (this is being retained for historical reasons) 'blowfish-ctr', // OPTIONAL Blowfish in SDCTR mode 'blowfish-cbc', // OPTIONAL Blowfish in CBC mode '3des-ctr', // RECOMMENDED Three-key 3DES in SDCTR mode '3des-cbc', // REQUIRED three-key 3DES in CBC mode //'none' // OPTIONAL no encryption; NOT RECOMMENDED ); $engines = array( Base::ENGINE_OPENSSL, Base::ENGINE_MCRYPT, Base::ENGINE_INTERNAL ); $ciphers = array(); foreach ($engines as $engine) { foreach ($algos as $algo) { $obj = $this->_encryption_algorithm_to_crypt_instance($algo); if ($obj instanceof Rijndael) { $obj->setKeyLength(preg_replace('#[^\d]#', '', $algo)); } switch ($algo) { case 'arcfour128': case 'arcfour256': if ($engine == Base::ENGINE_INTERNAL) { $algos = array_diff($algos, array($algo)); $ciphers[] = $algo; } else { continue 2; } } if ($obj->isValidEngine($engine)) { $algos = array_diff($algos, array($algo)); $ciphers[] = $algo; } } } return $ciphers; } /** * Returns a list of MAC algorithms that phpseclib supports * * @return array * @access public */ function getSupportedMACAlgorithms() { return array( // from <http://www.ietf.org/rfc/rfc6668.txt>: 'hmac-sha2-256',// RECOMMENDED HMAC-SHA256 (digest length = key length = 32) 'hmac-sha1-96', // RECOMMENDED first 96 bits of HMAC-SHA1 (digest length = 12, key length = 20) 'hmac-sha1', // REQUIRED HMAC-SHA1 (digest length = key length = 20) 'hmac-md5-96', // OPTIONAL first 96 bits of HMAC-MD5 (digest length = 12, key length = 16) 'hmac-md5', // OPTIONAL HMAC-MD5 (digest length = key length = 16) //'none' // OPTIONAL no MAC; NOT RECOMMENDED ); } /** * Returns a list of compression algorithms that phpseclib supports * * @return array * @access public */ function getSupportedCompressionAlgorithms() { return array( 'none' // REQUIRED no compression //'zlib' // OPTIONAL ZLIB (LZ77) compression ); } /** * Return list of negotiated algorithms * * Uses the same format as https://www.php.net/ssh2-methods-negotiated * * @return array * @access public */ function getAlgorithmsNegotiated() { $this->_connect(); return array( 'kex' => $this->kex_algorithm, 'hostkey' => $this->signature_format, 'client_to_server' => array( 'crypt' => $this->encrypt->name, 'mac' => $this->hmac_create->name, 'comp' => 'none', ), 'server_to_client' => array( 'crypt' => $this->decrypt->name, 'mac' => $this->hmac_check->name, 'comp' => 'none', ) ); } /** * Accepts an associative array with up to four parameters as described at * <https://www.php.net/manual/en/function.ssh2-connect.php> * * @param array $methods * @access public */ function setPreferredAlgorithms($methods) { $preferred = $methods; if (isset($preferred['kex'])) { $preferred['kex'] = array_intersect( $preferred['kex'], $this->getSupportedKEXAlgorithms() ); } if (isset($preferred['hostkey'])) { $preferred['hostkey'] = array_intersect( $preferred['hostkey'], $this->getSupportedHostKeyAlgorithms() ); } $keys = array('client_to_server', 'server_to_client'); foreach ($keys as $key) { if (isset($preferred[$key])) { $a = &$preferred[$key]; if (isset($a['crypt'])) { $a['crypt'] = array_intersect( $a['crypt'], $this->getSupportedEncryptionAlgorithms() ); } if (isset($a['comp'])) { $a['comp'] = array_intersect( $a['comp'], $this->getSupportedCompressionAlgorithms() ); } if (isset($a['mac'])) { $a['mac'] = array_intersect( $a['mac'], $this->getSupportedMACAlgorithms() ); } } } $keys = array( 'kex', 'hostkey', 'client_to_server/crypt', 'client_to_server/comp', 'client_to_server/mac', 'server_to_client/crypt', 'server_to_client/comp', 'server_to_client/mac', ); foreach ($keys as $key) { $p = $preferred; $m = $methods; $subkeys = explode('/', $key); foreach ($subkeys as $subkey) { if (!isset($p[$subkey])) { continue 2; } $p = $p[$subkey]; $m = $m[$subkey]; } if (count($p) != count($m)) { $diff = array_diff($m, $p); $msg = count($diff) == 1 ? ' is not a supported algorithm' : ' are not supported algorithms'; user_error(implode(', ', $diff) . $msg); return false; } } $this->preferred = $preferred; } /** * Returns the banner message. * * Quoting from the RFC, "in some jurisdictions, sending a warning message before * authentication may be relevant for getting legal protection." * * @return string * @access public */ function getBannerMessage() { return $this->banner_message; } /** * Returns the server public host key. * * Caching this the first time you connect to a server and checking the result on subsequent connections * is recommended. Returns false if the server signature is not signed correctly with the public host key. * * @return mixed * @access public */ function getServerPublicHostKey() { if (!($this->bitmap & self::MASK_CONSTRUCTOR)) { if (!$this->_connect()) { return false; } } $signature = $this->signature; $server_public_host_key = $this->server_public_host_key; if (strlen($server_public_host_key) < 4) { return false; } extract(unpack('Nlength', $this->_string_shift($server_public_host_key, 4))); $this->_string_shift($server_public_host_key, $length); if ($this->signature_validated) { return $this->bitmap ? $this->signature_format . ' ' . base64_encode($this->server_public_host_key) : false; } $this->signature_validated = true; switch ($this->signature_format) { case 'ssh-dss': $zero = new BigInteger(); if (strlen($server_public_host_key) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $p = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); if (strlen($server_public_host_key) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $q = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); if (strlen($server_public_host_key) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $g = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); if (strlen($server_public_host_key) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $y = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); /* The value for 'dss_signature_blob' is encoded as a string containing r, followed by s (which are 160-bit integers, without lengths or padding, unsigned, and in network byte order). */ $temp = unpack('Nlength', $this->_string_shift($signature, 4)); if ($temp['length'] != 40) { user_error('Invalid signature'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } $r = new BigInteger($this->_string_shift($signature, 20), 256); $s = new BigInteger($this->_string_shift($signature, 20), 256); switch (true) { case $r->equals($zero): case $r->compare($q) >= 0: case $s->equals($zero): case $s->compare($q) >= 0: user_error('Invalid signature'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } $w = $s->modInverse($q); $u1 = $w->multiply(new BigInteger(sha1($this->exchange_hash), 16)); list(, $u1) = $u1->divide($q); $u2 = $w->multiply($r); list(, $u2) = $u2->divide($q); $g = $g->modPow($u1, $p); $y = $y->modPow($u2, $p); $v = $g->multiply($y); list(, $v) = $v->divide($p); list(, $v) = $v->divide($q); if (!$v->equals($r)) { user_error('Bad server signature'); return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE); } break; case 'ssh-rsa': case 'rsa-sha2-256': case 'rsa-sha2-512': if (strlen($server_public_host_key) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $e = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); if (strlen($server_public_host_key) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $rawN = $this->_string_shift($server_public_host_key, $temp['length']); $n = new BigInteger($rawN, -256); $nLength = strlen(ltrim($rawN, "\0")); /* if (strlen($signature) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($signature, 4)); $signature = $this->_string_shift($signature, $temp['length']); $rsa = new RSA(); switch ($this->signature_format) { case 'rsa-sha2-512': $hash = 'sha512'; break; case 'rsa-sha2-256': $hash = 'sha256'; break; //case 'ssh-rsa': default: $hash = 'sha1'; } $rsa->setHash($hash); $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); $rsa->loadKey(array('e' => $e, 'n' => $n), RSA::PUBLIC_FORMAT_RAW); if (!$rsa->verify($this->exchange_hash, $signature)) { user_error('Bad server signature'); return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE); } */ if (strlen($signature) < 4) { return false; } $temp = unpack('Nlength', $this->_string_shift($signature, 4)); $s = new BigInteger($this->_string_shift($signature, $temp['length']), 256); // validate an RSA signature per "8.2 RSASSA-PKCS1-v1_5", "5.2.2 RSAVP1", and "9.1 EMSA-PSS" in the // following URL: // ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf // also, see SSHRSA.c (rsa2_verifysig) in PuTTy's source. if ($s->compare(new BigInteger()) < 0 || $s->compare($n->subtract(new BigInteger(1))) > 0) { user_error('Invalid signature'); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); } $s = $s->modPow($e, $n); $s = $s->toBytes(); switch ($this->signature_format) { case 'rsa-sha2-512': $hash = 'sha512'; break; case 'rsa-sha2-256': $hash = 'sha256'; break; //case 'ssh-rsa': default: $hash = 'sha1'; } $hashObj = new Hash($hash); switch ($this->signature_format) { case 'rsa-sha2-512': $h = pack('N5a*', 0x00305130, 0x0D060960, 0x86480165, 0x03040203, 0x05000440, $hashObj->hash($this->exchange_hash)); break; case 'rsa-sha2-256': $h = pack('N5a*', 0x00303130, 0x0D060960, 0x86480165, 0x03040201, 0x05000420, $hashObj->hash($this->exchange_hash)); break; //case 'ssh-rsa': default: $hash = 'sha1'; $h = pack('N4a*', 0x00302130, 0x0906052B, 0x0E03021A, 0x05000414, $hashObj->hash($this->exchange_hash)); } $h = chr(0x01) . str_repeat(chr(0xFF), $nLength - 2 - strlen($h)) . $h; if ($s != $h) { user_error('Bad server signature'); return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE); } break; default: user_error('Unsupported signature format'); return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE); } return $this->signature_format . ' ' . base64_encode($this->server_public_host_key); } /** * Returns the exit status of an SSH command or false. * * @return false|int * @access public */ function getExitStatus() { if (is_null($this->exit_status)) { return false; } return $this->exit_status; } /** * Returns the number of columns for the terminal window size. * * @return int * @access public */ function getWindowColumns() { return $this->windowColumns; } /** * Returns the number of rows for the terminal window size. * * @return int * @access public */ function getWindowRows() { return $this->windowRows; } /** * Sets the number of columns for the terminal window size. * * @param int $value * @access public */ function setWindowColumns($value) { $this->windowColumns = $value; } /** * Sets the number of rows for the terminal window size. * * @param int $value * @access public */ function setWindowRows($value) { $this->windowRows = $value; } /** * Sets the number of columns and rows for the terminal window size. * * @param int $columns * @param int $rows * @access public */ function setWindowSize($columns = 80, $rows = 24) { $this->windowColumns = $columns; $this->windowRows = $rows; } } <?php /** * Pure-PHP arbitrary precision integer arithmetic library. * * Supports base-2, base-10, base-16, and base-256 numbers. Uses the GMP or BCMath extensions, if available, * and an internal implementation, otherwise. * * PHP version 5 * * {@internal (all DocBlock comments regarding implementation - such as the one that follows - refer to the * {@link self::MODE_INTERNAL self::MODE_INTERNAL} mode) * * BigInteger uses base-2**26 to perform operations such as multiplication and division and * base-2**52 (ie. two base 2**26 digits) to perform addition and subtraction. Because the largest possible * value when multiplying two base-2**26 numbers together is a base-2**52 number, double precision floating * point numbers - numbers that should be supported on most hardware and whose significand is 53 bits - are * used. As a consequence, bitwise operators such as >> and << cannot be used, nor can the modulo operator %, * which only supports integers. Although this fact will slow this library down, the fact that such a high * base is being used should more than compensate. * * Numbers are stored in {@link http://en.wikipedia.org/wiki/Endianness little endian} format. ie. * (new \phpseclib\Math\BigInteger(pow(2, 26)))->value = array(0, 1) * * Useful resources are as follows: * * - {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf Handbook of Applied Cryptography (HAC)} * - {@link http://math.libtomcrypt.com/files/tommath.pdf Multi-Precision Math (MPM)} * - Java's BigInteger classes. See /j2se/src/share/classes/java/math in jdk-1_5_0-src-jrl.zip * * Here's an example of how to use this library: * <code> * <?php * $a = new \phpseclib\Math\BigInteger(2); * $b = new \phpseclib\Math\BigInteger(3); * * $c = $a->add($b); * * echo $c->toString(); // outputs 5 * ?> * </code> * * @category Math * @package BigInteger * @author Jim Wigginton <terrafrost@php.net> * @copyright 2006 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License */ namespace phpseclib\Math; use phpseclib\Crypt\Random; /** * Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 * numbers. * * @package BigInteger * @author Jim Wigginton <terrafrost@php.net> * @access public */ class BigInteger { /**#@+ * Reduction constants * * @access private * @see BigInteger::_reduce() */ /** * @see BigInteger::_montgomery() * @see BigInteger::_prepMontgomery() */ const MONTGOMERY = 0; /** * @see BigInteger::_barrett() */ const BARRETT = 1; /** * @see BigInteger::_mod2() */ const POWEROF2 = 2; /** * @see BigInteger::_remainder() */ const CLASSIC = 3; /** * @see BigInteger::__clone() */ const NONE = 4; /**#@-*/ /**#@+ * Array constants * * Rather than create a thousands and thousands of new BigInteger objects in repeated function calls to add() and * multiply() or whatever, we'll just work directly on arrays, taking them in as parameters and returning them. * * @access private */ /** * $result[self::VALUE] contains the value. */ const VALUE = 0; /** * $result[self::SIGN] contains the sign. */ const SIGN = 1; /**#@-*/ /**#@+ * @access private * @see BigInteger::_montgomery() * @see BigInteger::_barrett() */ /** * Cache constants * * $cache[self::VARIABLE] tells us whether or not the cached data is still valid. */ const VARIABLE = 0; /** * $cache[self::DATA] contains the cached data. */ const DATA = 1; /**#@-*/ /**#@+ * Mode constants. * * @access private * @see BigInteger::__construct() */ /** * To use the pure-PHP implementation */ const MODE_INTERNAL = 1; /** * To use the BCMath library * * (if enabled; otherwise, the internal implementation will be used) */ const MODE_BCMATH = 2; /** * To use the GMP library * * (if present; otherwise, either the BCMath or the internal implementation will be used) */ const MODE_GMP = 3; /**#@-*/ /** * Karatsuba Cutoff * * At what point do we switch between Karatsuba multiplication and schoolbook long multiplication? * * @access private */ const KARATSUBA_CUTOFF = 25; /**#@+ * Static properties used by the pure-PHP implementation. * * @see __construct() */ protected static $base; protected static $baseFull; protected static $maxDigit; protected static $msb; /** * $max10 in greatest $max10Len satisfying * $max10 = 10**$max10Len <= 2**$base. */ protected static $max10; /** * $max10Len in greatest $max10Len satisfying * $max10 = 10**$max10Len <= 2**$base. */ protected static $max10Len; protected static $maxDigit2; /**#@-*/ /** * Holds the BigInteger's value. * * @var array * @access private */ var $value; /** * Holds the BigInteger's magnitude. * * @var bool * @access private */ var $is_negative = false; /** * Precision * * @see self::setPrecision() * @access private */ var $precision = -1; /** * Precision Bitmask * * @see self::setPrecision() * @access private */ var $bitmask = false; /** * Mode independent value used for serialization. * * If the bcmath or gmp extensions are installed $this->value will be a non-serializable resource, hence the need for * a variable that'll be serializable regardless of whether or not extensions are being used. Unlike $this->value, * however, $this->hex is only calculated when $this->__sleep() is called. * * @see self::__sleep() * @see self::__wakeup() * @var string * @access private */ var $hex; /** * Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers. * * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using * two's compliment. The sole exception to this is -10, which is treated the same as 10 is. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger('0x32', 16); // 50 in base-16 * * echo $a->toString(); // outputs 50 * ?> * </code> * * @param $x base-10 number or base-$base number if $base set. * @param int $base * @return \phpseclib\Math\BigInteger * @access public */ function __construct($x = 0, $base = 10) { if (!defined('MATH_BIGINTEGER_MODE')) { switch (true) { case extension_loaded('gmp'): define('MATH_BIGINTEGER_MODE', self::MODE_GMP); break; case extension_loaded('bcmath'): define('MATH_BIGINTEGER_MODE', self::MODE_BCMATH); break; default: define('MATH_BIGINTEGER_MODE', self::MODE_INTERNAL); } } if (extension_loaded('openssl') && !defined('MATH_BIGINTEGER_OPENSSL_DISABLE') && !defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work $versions = array(); // avoid generating errors (even with suppression) when phpinfo() is disabled (common in production systems) if (strpos(ini_get('disable_functions'), 'phpinfo') === false) { ob_start(); @phpinfo(); $content = ob_get_contents(); ob_end_clean(); preg_match_all('#OpenSSL (Header|Library) Version(.*)#im', $content, $matches); if (!empty($matches[1])) { for ($i = 0; $i < count($matches[1]); $i++) { $fullVersion = trim(str_replace('=>', '', strip_tags($matches[2][$i]))); // Remove letter part in OpenSSL version if (!preg_match('/(\d+\.\d+\.\d+)/i', $fullVersion, $m)) { $versions[$matches[1][$i]] = $fullVersion; } else { $versions[$matches[1][$i]] = $m[0]; } } } } // it doesn't appear that OpenSSL versions were reported upon until PHP 5.3+ switch (true) { case !isset($versions['Header']): case !isset($versions['Library']): case $versions['Header'] == $versions['Library']: case version_compare($versions['Header'], '1.0.0') >= 0 && version_compare($versions['Library'], '1.0.0') >= 0: define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); break; default: define('MATH_BIGINTEGER_OPENSSL_DISABLE', true); } } if (!defined('PHP_INT_SIZE')) { define('PHP_INT_SIZE', 4); } if (empty(self::$base) && MATH_BIGINTEGER_MODE == self::MODE_INTERNAL) { switch (PHP_INT_SIZE) { case 8: // use 64-bit integers if int size is 8 bytes self::$base = 31; self::$baseFull = 0x80000000; self::$maxDigit = 0x7FFFFFFF; self::$msb = 0x40000000; self::$max10 = 1000000000; self::$max10Len = 9; self::$maxDigit2 = pow(2, 62); break; //case 4: // use 64-bit floats if int size is 4 bytes default: self::$base = 26; self::$baseFull = 0x4000000; self::$maxDigit = 0x3FFFFFF; self::$msb = 0x2000000; self::$max10 = 10000000; self::$max10Len = 7; self::$maxDigit2 = pow(2, 52); // pow() prevents truncation } } switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: switch (true) { case is_resource($x) && get_resource_type($x) == 'GMP integer': // PHP 5.6 switched GMP from using resources to objects case $x instanceof \GMP: $this->value = $x; return; } $this->value = gmp_init(0); break; case self::MODE_BCMATH: $this->value = '0'; break; default: $this->value = array(); } // '0' counts as empty() but when the base is 256 '0' is equal to ord('0') or 48 // '0' is the only value like this per http://php.net/empty if (empty($x) && (abs($base) != 256 || $x !== '0')) { return; } switch ($base) { case -256: if (ord($x[0]) & 0x80) { $x = ~$x; $this->is_negative = true; } case 256: switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $this->value = function_exists('gmp_import') ? gmp_import($x) : gmp_init('0x' . bin2hex($x)); if ($this->is_negative) { $this->value = gmp_neg($this->value); } break; case self::MODE_BCMATH: // round $len to the nearest 4 (thanks, DavidMJ!) $len = (strlen($x) + 3) & 0xFFFFFFFC; $x = str_pad($x, $len, chr(0), STR_PAD_LEFT); for ($i = 0; $i < $len; $i+= 4) { $this->value = bcmul($this->value, '4294967296', 0); // 4294967296 == 2**32 $this->value = bcadd($this->value, 0x1000000 * ord($x[$i]) + ((ord($x[$i + 1]) << 16) | (ord($x[$i + 2]) << 8) | ord($x[$i + 3])), 0); } if ($this->is_negative) { $this->value = '-' . $this->value; } break; // converts a base-2**8 (big endian / msb) number to base-2**26 (little endian / lsb) default: while (strlen($x)) { $this->value[] = $this->_bytes2int($this->_base256_rshift($x, self::$base)); } } if ($this->is_negative) { if (MATH_BIGINTEGER_MODE != self::MODE_INTERNAL) { $this->is_negative = false; } $temp = $this->add(new static('-1')); $this->value = $temp->value; } break; case 16: case -16: if ($base > 0 && $x[0] == '-') { $this->is_negative = true; $x = substr($x, 1); } $x = preg_replace('#^(?:0x)?([A-Fa-f0-9]*).*#', '$1', $x); $is_negative = false; if ($base < 0 && hexdec($x[0]) >= 8) { $this->is_negative = $is_negative = true; $x = bin2hex(~pack('H*', $x)); } switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp = $this->is_negative ? '-0x' . $x : '0x' . $x; $this->value = gmp_init($temp); $this->is_negative = false; break; case self::MODE_BCMATH: $x = (strlen($x) & 1) ? '0' . $x : $x; $temp = new static(pack('H*', $x), 256); $this->value = $this->is_negative ? '-' . $temp->value : $temp->value; $this->is_negative = false; break; default: $x = (strlen($x) & 1) ? '0' . $x : $x; $temp = new static(pack('H*', $x), 256); $this->value = $temp->value; } if ($is_negative) { $temp = $this->add(new static('-1')); $this->value = $temp->value; } break; case 10: case -10: // (?<!^)(?:-).*: find any -'s that aren't at the beginning and then any characters that follow that // (?<=^|-)0*: find any 0's that are preceded by the start of the string or by a - (ie. octals) // [^-0-9].*: find any non-numeric characters and then any characters that follow that $x = preg_replace('#(?<!^)(?:-).*|(?<=^|-)0*|[^-0-9].*#', '', $x); if (!strlen($x) || $x == '-') { $x = '0'; } switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $this->value = gmp_init($x); break; case self::MODE_BCMATH: // explicitly casting $x to a string is necessary, here, since doing $x[0] on -1 yields different // results then doing it on '-1' does (modInverse does $x[0]) $this->value = $x === '-' ? '0' : (string) $x; break; default: $temp = new static(); $multiplier = new static(); $multiplier->value = array(self::$max10); if ($x[0] == '-') { $this->is_negative = true; $x = substr($x, 1); } $x = str_pad($x, strlen($x) + ((self::$max10Len - 1) * strlen($x)) % self::$max10Len, 0, STR_PAD_LEFT); while (strlen($x)) { $temp = $temp->multiply($multiplier); $temp = $temp->add(new static($this->_int2bytes(substr($x, 0, self::$max10Len)), 256)); $x = substr($x, self::$max10Len); } $this->value = $temp->value; } break; case 2: // base-2 support originally implemented by Lluis Pamies - thanks! case -2: if ($base > 0 && $x[0] == '-') { $this->is_negative = true; $x = substr($x, 1); } $x = preg_replace('#^([01]*).*#', '$1', $x); $x = str_pad($x, strlen($x) + (3 * strlen($x)) % 4, 0, STR_PAD_LEFT); $str = '0x'; while (strlen($x)) { $part = substr($x, 0, 4); $str.= dechex(bindec($part)); $x = substr($x, 4); } if ($this->is_negative) { $str = '-' . $str; } $temp = new static($str, 8 * $base); // ie. either -16 or +16 $this->value = $temp->value; $this->is_negative = $temp->is_negative; break; default: // base not supported, so we'll let $this == 0 } } /** * Converts a BigInteger to a byte string (eg. base-256). * * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're * saved as two's compliment. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger('65'); * * echo $a->toBytes(); // outputs chr(65) * ?> * </code> * * @param bool $twos_compliment * @return string * @access public * @internal Converts a base-2**26 number to base-2**8 */ function toBytes($twos_compliment = false) { if ($twos_compliment) { $comparison = $this->compare(new static()); if ($comparison == 0) { return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; } $temp = $comparison < 0 ? $this->add(new static(1)) : $this->copy(); $bytes = $temp->toBytes(); if (!strlen($bytes)) { // eg. if the number we're trying to convert is -1 $bytes = chr(0); } if ($this->precision <= 0 && (ord($bytes[0]) & 0x80)) { $bytes = chr(0) . $bytes; } return $comparison < 0 ? ~$bytes : $bytes; } switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: if (gmp_cmp($this->value, gmp_init(0)) == 0) { return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; } if (function_exists('gmp_export')) { $temp = gmp_export($this->value); } else { $temp = gmp_strval(gmp_abs($this->value), 16); $temp = (strlen($temp) & 1) ? '0' . $temp : $temp; $temp = pack('H*', $temp); } return $this->precision > 0 ? substr(str_pad($temp, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) : ltrim($temp, chr(0)); case self::MODE_BCMATH: if ($this->value === '0') { return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; } $value = ''; $current = $this->value; if ($current[0] == '-') { $current = substr($current, 1); } while (bccomp($current, '0', 0) > 0) { $temp = bcmod($current, '16777216'); $value = chr($temp >> 16) . chr($temp >> 8) . chr($temp) . $value; $current = bcdiv($current, '16777216', 0); } return $this->precision > 0 ? substr(str_pad($value, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) : ltrim($value, chr(0)); } if (!count($this->value)) { return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; } $result = $this->_int2bytes($this->value[count($this->value) - 1]); $temp = $this->copy(); for ($i = count($temp->value) - 2; $i >= 0; --$i) { $temp->_base256_lshift($result, self::$base); $result = $result | str_pad($temp->_int2bytes($temp->value[$i]), strlen($result), chr(0), STR_PAD_LEFT); } return $this->precision > 0 ? str_pad(substr($result, -(($this->precision + 7) >> 3)), ($this->precision + 7) >> 3, chr(0), STR_PAD_LEFT) : $result; } /** * Converts a BigInteger to a hex string (eg. base-16)). * * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're * saved as two's compliment. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger('65'); * * echo $a->toHex(); // outputs '41' * ?> * </code> * * @param bool $twos_compliment * @return string * @access public * @internal Converts a base-2**26 number to base-2**8 */ function toHex($twos_compliment = false) { return bin2hex($this->toBytes($twos_compliment)); } /** * Converts a BigInteger to a bit string (eg. base-2). * * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're * saved as two's compliment. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger('65'); * * echo $a->toBits(); // outputs '1000001' * ?> * </code> * * @param bool $twos_compliment * @return string * @access public * @internal Converts a base-2**26 number to base-2**2 */ function toBits($twos_compliment = false) { $hex = $this->toHex($twos_compliment); $bits = ''; for ($i = strlen($hex) - 8, $start = strlen($hex) & 7; $i >= $start; $i-=8) { $bits = str_pad(decbin(hexdec(substr($hex, $i, 8))), 32, '0', STR_PAD_LEFT) . $bits; } if ($start) { // hexdec('') == 0 $bits = str_pad(decbin(hexdec(substr($hex, 0, $start))), 8, '0', STR_PAD_LEFT) . $bits; } $result = $this->precision > 0 ? substr($bits, -$this->precision) : ltrim($bits, '0'); if ($twos_compliment && $this->compare(new static()) > 0 && $this->precision <= 0) { return '0' . $result; } return $result; } /** * Converts a BigInteger to a base-10 number. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger('50'); * * echo $a->toString(); // outputs 50 * ?> * </code> * * @return string * @access public * @internal Converts a base-2**26 number to base-10**7 (which is pretty much base-10) */ function toString() { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: return gmp_strval($this->value); case self::MODE_BCMATH: if ($this->value === '0') { return '0'; } return ltrim($this->value, '0'); } if (!count($this->value)) { return '0'; } $temp = $this->copy(); $temp->bitmask = false; $temp->is_negative = false; $divisor = new static(); $divisor->value = array(self::$max10); $result = ''; while (count($temp->value)) { list($temp, $mod) = $temp->divide($divisor); $result = str_pad(isset($mod->value[0]) ? $mod->value[0] : '', self::$max10Len, '0', STR_PAD_LEFT) . $result; } $result = ltrim($result, '0'); if (empty($result)) { $result = '0'; } if ($this->is_negative) { $result = '-' . $result; } return $result; } /** * Copy an object * * PHP5 passes objects by reference while PHP4 passes by value. As such, we need a function to guarantee * that all objects are passed by value, when appropriate. More information can be found here: * * {@link http://php.net/language.oop5.basic#51624} * * @access public * @see self::__clone() * @return \phpseclib\Math\BigInteger */ function copy() { $temp = new static(); $temp->value = $this->value; $temp->is_negative = $this->is_negative; $temp->precision = $this->precision; $temp->bitmask = $this->bitmask; return $temp; } /** * __toString() magic method * * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call * toString(). * * @access public * @internal Implemented per a suggestion by Techie-Michael - thanks! */ function __toString() { return $this->toString(); } /** * __clone() magic method * * Although you can call BigInteger::__toString() directly in PHP5, you cannot call BigInteger::__clone() directly * in PHP5. You can in PHP4 since it's not a magic method, but in PHP5, you have to call it by using the PHP5 * only syntax of $y = clone $x. As such, if you're trying to write an application that works on both PHP4 and * PHP5, call BigInteger::copy(), instead. * * @access public * @see self::copy() * @return \phpseclib\Math\BigInteger */ function __clone() { return $this->copy(); } /** * __sleep() magic method * * Will be called, automatically, when serialize() is called on a BigInteger object. * * @see self::__wakeup() * @access public */ function __sleep() { $this->hex = $this->toHex(true); $vars = array('hex'); if ($this->precision > 0) { $vars[] = 'precision'; } return $vars; } /** * __wakeup() magic method * * Will be called, automatically, when unserialize() is called on a BigInteger object. * * @see self::__sleep() * @access public */ function __wakeup() { $temp = new static($this->hex, -16); $this->value = $temp->value; $this->is_negative = $temp->is_negative; if ($this->precision > 0) { // recalculate $this->bitmask $this->setPrecision($this->precision); } } /** * __debugInfo() magic method * * Will be called, automatically, when print_r() or var_dump() are called * * @access public */ function __debugInfo() { $opts = array(); switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $engine = 'gmp'; break; case self::MODE_BCMATH: $engine = 'bcmath'; break; case self::MODE_INTERNAL: $engine = 'internal'; $opts[] = PHP_INT_SIZE == 8 ? '64-bit' : '32-bit'; } if (MATH_BIGINTEGER_MODE != self::MODE_GMP && defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { $opts[] = 'OpenSSL'; } if (!empty($opts)) { $engine.= ' (' . implode('.', $opts) . ')'; } return array( 'value' => '0x' . $this->toHex(true), 'engine' => $engine ); } /** * Adds two BigIntegers. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger('10'); * $b = new \phpseclib\Math\BigInteger('20'); * * $c = $a->add($b); * * echo $c->toString(); // outputs 30 * ?> * </code> * * @param \phpseclib\Math\BigInteger $y * @return \phpseclib\Math\BigInteger * @access public * @internal Performs base-2**52 addition */ function add($y) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp = new static(); $temp->value = gmp_add($this->value, $y->value); return $this->_normalize($temp); case self::MODE_BCMATH: $temp = new static(); $temp->value = bcadd($this->value, $y->value, 0); return $this->_normalize($temp); } $temp = $this->_add($this->value, $this->is_negative, $y->value, $y->is_negative); $result = new static(); $result->value = $temp[self::VALUE]; $result->is_negative = $temp[self::SIGN]; return $this->_normalize($result); } /** * Performs addition. * * @param array $x_value * @param bool $x_negative * @param array $y_value * @param bool $y_negative * @return array * @access private */ function _add($x_value, $x_negative, $y_value, $y_negative) { $x_size = count($x_value); $y_size = count($y_value); if ($x_size == 0) { return array( self::VALUE => $y_value, self::SIGN => $y_negative ); } elseif ($y_size == 0) { return array( self::VALUE => $x_value, self::SIGN => $x_negative ); } // subtract, if appropriate if ($x_negative != $y_negative) { if ($x_value == $y_value) { return array( self::VALUE => array(), self::SIGN => false ); } $temp = $this->_subtract($x_value, false, $y_value, false); $temp[self::SIGN] = $this->_compare($x_value, false, $y_value, false) > 0 ? $x_negative : $y_negative; return $temp; } if ($x_size < $y_size) { $size = $x_size; $value = $y_value; } else { $size = $y_size; $value = $x_value; } $value[count($value)] = 0; // just in case the carry adds an extra digit $carry = 0; for ($i = 0, $j = 1; $j < $size; $i+=2, $j+=2) { $sum = $x_value[$j] * self::$baseFull + $x_value[$i] + $y_value[$j] * self::$baseFull + $y_value[$i] + $carry; $carry = $sum >= self::$maxDigit2; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1 $sum = $carry ? $sum - self::$maxDigit2 : $sum; $temp = self::$base === 26 ? intval($sum / 0x4000000) : ($sum >> 31); $value[$i] = (int) ($sum - self::$baseFull * $temp); // eg. a faster alternative to fmod($sum, 0x4000000) $value[$j] = $temp; } if ($j == $size) { // ie. if $y_size is odd $sum = $x_value[$i] + $y_value[$i] + $carry; $carry = $sum >= self::$baseFull; $value[$i] = $carry ? $sum - self::$baseFull : $sum; ++$i; // ie. let $i = $j since we've just done $value[$i] } if ($carry) { for (; $value[$i] == self::$maxDigit; ++$i) { $value[$i] = 0; } ++$value[$i]; } return array( self::VALUE => $this->_trim($value), self::SIGN => $x_negative ); } /** * Subtracts two BigIntegers. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger('10'); * $b = new \phpseclib\Math\BigInteger('20'); * * $c = $a->subtract($b); * * echo $c->toString(); // outputs -10 * ?> * </code> * * @param \phpseclib\Math\BigInteger $y * @return \phpseclib\Math\BigInteger * @access public * @internal Performs base-2**52 subtraction */ function subtract($y) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp = new static(); $temp->value = gmp_sub($this->value, $y->value); return $this->_normalize($temp); case self::MODE_BCMATH: $temp = new static(); $temp->value = bcsub($this->value, $y->value, 0); return $this->_normalize($temp); } $temp = $this->_subtract($this->value, $this->is_negative, $y->value, $y->is_negative); $result = new static(); $result->value = $temp[self::VALUE]; $result->is_negative = $temp[self::SIGN]; return $this->_normalize($result); } /** * Performs subtraction. * * @param array $x_value * @param bool $x_negative * @param array $y_value * @param bool $y_negative * @return array * @access private */ function _subtract($x_value, $x_negative, $y_value, $y_negative) { $x_size = count($x_value); $y_size = count($y_value); if ($x_size == 0) { return array( self::VALUE => $y_value, self::SIGN => !$y_negative ); } elseif ($y_size == 0) { return array( self::VALUE => $x_value, self::SIGN => $x_negative ); } // add, if appropriate (ie. -$x - +$y or +$x - -$y) if ($x_negative != $y_negative) { $temp = $this->_add($x_value, false, $y_value, false); $temp[self::SIGN] = $x_negative; return $temp; } $diff = $this->_compare($x_value, $x_negative, $y_value, $y_negative); if (!$diff) { return array( self::VALUE => array(), self::SIGN => false ); } // switch $x and $y around, if appropriate. if ((!$x_negative && $diff < 0) || ($x_negative && $diff > 0)) { $temp = $x_value; $x_value = $y_value; $y_value = $temp; $x_negative = !$x_negative; $x_size = count($x_value); $y_size = count($y_value); } // at this point, $x_value should be at least as big as - if not bigger than - $y_value $carry = 0; for ($i = 0, $j = 1; $j < $y_size; $i+=2, $j+=2) { $sum = $x_value[$j] * self::$baseFull + $x_value[$i] - $y_value[$j] * self::$baseFull - $y_value[$i] - $carry; $carry = $sum < 0; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1 $sum = $carry ? $sum + self::$maxDigit2 : $sum; $temp = self::$base === 26 ? intval($sum / 0x4000000) : ($sum >> 31); $x_value[$i] = (int) ($sum - self::$baseFull * $temp); $x_value[$j] = $temp; } if ($j == $y_size) { // ie. if $y_size is odd $sum = $x_value[$i] - $y_value[$i] - $carry; $carry = $sum < 0; $x_value[$i] = $carry ? $sum + self::$baseFull : $sum; ++$i; } if ($carry) { for (; !$x_value[$i]; ++$i) { $x_value[$i] = self::$maxDigit; } --$x_value[$i]; } return array( self::VALUE => $this->_trim($x_value), self::SIGN => $x_negative ); } /** * Multiplies two BigIntegers * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger('10'); * $b = new \phpseclib\Math\BigInteger('20'); * * $c = $a->multiply($b); * * echo $c->toString(); // outputs 200 * ?> * </code> * * @param \phpseclib\Math\BigInteger $x * @return \phpseclib\Math\BigInteger * @access public */ function multiply($x) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp = new static(); $temp->value = gmp_mul($this->value, $x->value); return $this->_normalize($temp); case self::MODE_BCMATH: $temp = new static(); $temp->value = bcmul($this->value, $x->value, 0); return $this->_normalize($temp); } $temp = $this->_multiply($this->value, $this->is_negative, $x->value, $x->is_negative); $product = new static(); $product->value = $temp[self::VALUE]; $product->is_negative = $temp[self::SIGN]; return $this->_normalize($product); } /** * Performs multiplication. * * @param array $x_value * @param bool $x_negative * @param array $y_value * @param bool $y_negative * @return array * @access private */ function _multiply($x_value, $x_negative, $y_value, $y_negative) { //if ( $x_value == $y_value ) { // return array( // self::VALUE => $this->_square($x_value), // self::SIGN => $x_sign != $y_value // ); //} $x_length = count($x_value); $y_length = count($y_value); if (!$x_length || !$y_length) { // a 0 is being multiplied return array( self::VALUE => array(), self::SIGN => false ); } return array( self::VALUE => min($x_length, $y_length) < 2 * self::KARATSUBA_CUTOFF ? $this->_trim($this->_regularMultiply($x_value, $y_value)) : $this->_trim($this->_karatsuba($x_value, $y_value)), self::SIGN => $x_negative != $y_negative ); } /** * Performs long multiplication on two BigIntegers * * Modeled after 'multiply' in MutableBigInteger.java. * * @param array $x_value * @param array $y_value * @return array * @access private */ function _regularMultiply($x_value, $y_value) { $x_length = count($x_value); $y_length = count($y_value); if (!$x_length || !$y_length) { // a 0 is being multiplied return array(); } if ($x_length < $y_length) { $temp = $x_value; $x_value = $y_value; $y_value = $temp; $x_length = count($x_value); $y_length = count($y_value); } $product_value = $this->_array_repeat(0, $x_length + $y_length); // the following for loop could be removed if the for loop following it // (the one with nested for loops) initially set $i to 0, but // doing so would also make the result in one set of unnecessary adds, // since on the outermost loops first pass, $product->value[$k] is going // to always be 0 $carry = 0; for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0 $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0 $carry = self::$base === 26 ? intval($temp / 0x4000000) : ($temp >> 31); $product_value[$j] = (int) ($temp - self::$baseFull * $carry); } $product_value[$j] = $carry; // the above for loop is what the previous comment was talking about. the // following for loop is the "one with nested for loops" for ($i = 1; $i < $y_length; ++$i) { $carry = 0; for ($j = 0, $k = $i; $j < $x_length; ++$j, ++$k) { $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry; $carry = self::$base === 26 ? intval($temp / 0x4000000) : ($temp >> 31); $product_value[$k] = (int) ($temp - self::$baseFull * $carry); } $product_value[$k] = $carry; } return $product_value; } /** * Performs Karatsuba multiplication on two BigIntegers * * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=120 MPM 5.2.3}. * * @param array $x_value * @param array $y_value * @return array * @access private */ function _karatsuba($x_value, $y_value) { $m = min(count($x_value) >> 1, count($y_value) >> 1); if ($m < self::KARATSUBA_CUTOFF) { return $this->_regularMultiply($x_value, $y_value); } $x1 = array_slice($x_value, $m); $x0 = array_slice($x_value, 0, $m); $y1 = array_slice($y_value, $m); $y0 = array_slice($y_value, 0, $m); $z2 = $this->_karatsuba($x1, $y1); $z0 = $this->_karatsuba($x0, $y0); $z1 = $this->_add($x1, false, $x0, false); $temp = $this->_add($y1, false, $y0, false); $z1 = $this->_karatsuba($z1[self::VALUE], $temp[self::VALUE]); $temp = $this->_add($z2, false, $z0, false); $z1 = $this->_subtract($z1, false, $temp[self::VALUE], false); $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2); $z1[self::VALUE] = array_merge(array_fill(0, $m, 0), $z1[self::VALUE]); $xy = $this->_add($z2, false, $z1[self::VALUE], $z1[self::SIGN]); $xy = $this->_add($xy[self::VALUE], $xy[self::SIGN], $z0, false); return $xy[self::VALUE]; } /** * Performs squaring * * @param array $x * @return array * @access private */ function _square($x = false) { return count($x) < 2 * self::KARATSUBA_CUTOFF ? $this->_trim($this->_baseSquare($x)) : $this->_trim($this->_karatsubaSquare($x)); } /** * Performs traditional squaring on two BigIntegers * * Squaring can be done faster than multiplying a number by itself can be. See * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=7 HAC 14.2.4} / * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=141 MPM 5.3} for more information. * * @param array $value * @return array * @access private */ function _baseSquare($value) { if (empty($value)) { return array(); } $square_value = $this->_array_repeat(0, 2 * count($value)); for ($i = 0, $max_index = count($value) - 1; $i <= $max_index; ++$i) { $i2 = $i << 1; $temp = $square_value[$i2] + $value[$i] * $value[$i]; $carry = self::$base === 26 ? intval($temp / 0x4000000) : ($temp >> 31); $square_value[$i2] = (int) ($temp - self::$baseFull * $carry); // note how we start from $i+1 instead of 0 as we do in multiplication. for ($j = $i + 1, $k = $i2 + 1; $j <= $max_index; ++$j, ++$k) { $temp = $square_value[$k] + 2 * $value[$j] * $value[$i] + $carry; $carry = self::$base === 26 ? intval($temp / 0x4000000) : ($temp >> 31); $square_value[$k] = (int) ($temp - self::$baseFull * $carry); } // the following line can yield values larger 2**15. at this point, PHP should switch // over to floats. $square_value[$i + $max_index + 1] = $carry; } return $square_value; } /** * Performs Karatsuba "squaring" on two BigIntegers * * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=151 MPM 5.3.4}. * * @param array $value * @return array * @access private */ function _karatsubaSquare($value) { $m = count($value) >> 1; if ($m < self::KARATSUBA_CUTOFF) { return $this->_baseSquare($value); } $x1 = array_slice($value, $m); $x0 = array_slice($value, 0, $m); $z2 = $this->_karatsubaSquare($x1); $z0 = $this->_karatsubaSquare($x0); $z1 = $this->_add($x1, false, $x0, false); $z1 = $this->_karatsubaSquare($z1[self::VALUE]); $temp = $this->_add($z2, false, $z0, false); $z1 = $this->_subtract($z1, false, $temp[self::VALUE], false); $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2); $z1[self::VALUE] = array_merge(array_fill(0, $m, 0), $z1[self::VALUE]); $xx = $this->_add($z2, false, $z1[self::VALUE], $z1[self::SIGN]); $xx = $this->_add($xx[self::VALUE], $xx[self::SIGN], $z0, false); return $xx[self::VALUE]; } /** * Divides two BigIntegers. * * Returns an array whose first element contains the quotient and whose second element contains the * "common residue". If the remainder would be positive, the "common residue" and the remainder are the * same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder * and the divisor (basically, the "common residue" is the first positive modulo). * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger('10'); * $b = new \phpseclib\Math\BigInteger('20'); * * list($quotient, $remainder) = $a->divide($b); * * echo $quotient->toString(); // outputs 0 * echo "\r\n"; * echo $remainder->toString(); // outputs 10 * ?> * </code> * * @param \phpseclib\Math\BigInteger $y * @return array * @access public * @internal This function is based off of {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=9 HAC 14.20}. */ function divide($y) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $quotient = new static(); $remainder = new static(); list($quotient->value, $remainder->value) = gmp_div_qr($this->value, $y->value); if (gmp_sign($remainder->value) < 0) { $remainder->value = gmp_add($remainder->value, gmp_abs($y->value)); } return array($this->_normalize($quotient), $this->_normalize($remainder)); case self::MODE_BCMATH: $quotient = new static(); $remainder = new static(); $quotient->value = bcdiv($this->value, $y->value, 0); $remainder->value = bcmod($this->value, $y->value); if ($remainder->value[0] == '-') { $remainder->value = bcadd($remainder->value, $y->value[0] == '-' ? substr($y->value, 1) : $y->value, 0); } return array($this->_normalize($quotient), $this->_normalize($remainder)); } if (count($y->value) == 1) { list($q, $r) = $this->_divide_digit($this->value, $y->value[0]); $quotient = new static(); $remainder = new static(); $quotient->value = $q; $remainder->value = array($r); $quotient->is_negative = $this->is_negative != $y->is_negative; return array($this->_normalize($quotient), $this->_normalize($remainder)); } static $zero; if (!isset($zero)) { $zero = new static(); } $x = $this->copy(); $y = $y->copy(); $x_sign = $x->is_negative; $y_sign = $y->is_negative; $x->is_negative = $y->is_negative = false; $diff = $x->compare($y); if (!$diff) { $temp = new static(); $temp->value = array(1); $temp->is_negative = $x_sign != $y_sign; return array($this->_normalize($temp), $this->_normalize(new static())); } if ($diff < 0) { // if $x is negative, "add" $y. if ($x_sign) { $x = $y->subtract($x); } return array($this->_normalize(new static()), $this->_normalize($x)); } // normalize $x and $y as described in HAC 14.23 / 14.24 $msb = $y->value[count($y->value) - 1]; for ($shift = 0; !($msb & self::$msb); ++$shift) { $msb <<= 1; } $x->_lshift($shift); $y->_lshift($shift); $y_value = &$y->value; $x_max = count($x->value) - 1; $y_max = count($y->value) - 1; $quotient = new static(); $quotient_value = &$quotient->value; $quotient_value = $this->_array_repeat(0, $x_max - $y_max + 1); static $temp, $lhs, $rhs; if (!isset($temp)) { $temp = new static(); $lhs = new static(); $rhs = new static(); } $temp_value = &$temp->value; $rhs_value = &$rhs->value; // $temp = $y << ($x_max - $y_max-1) in base 2**26 $temp_value = array_merge($this->_array_repeat(0, $x_max - $y_max), $y_value); while ($x->compare($temp) >= 0) { // calculate the "common residue" ++$quotient_value[$x_max - $y_max]; $x = $x->subtract($temp); $x_max = count($x->value) - 1; } for ($i = $x_max; $i >= $y_max + 1; --$i) { $x_value = &$x->value; $x_window = array( isset($x_value[$i]) ? $x_value[$i] : 0, isset($x_value[$i - 1]) ? $x_value[$i - 1] : 0, isset($x_value[$i - 2]) ? $x_value[$i - 2] : 0 ); $y_window = array( $y_value[$y_max], ($y_max > 0) ? $y_value[$y_max - 1] : 0 ); $q_index = $i - $y_max - 1; if ($x_window[0] == $y_window[0]) { $quotient_value[$q_index] = self::$maxDigit; } else { $quotient_value[$q_index] = $this->_safe_divide( $x_window[0] * self::$baseFull + $x_window[1], $y_window[0] ); } $temp_value = array($y_window[1], $y_window[0]); $lhs->value = array($quotient_value[$q_index]); $lhs = $lhs->multiply($temp); $rhs_value = array($x_window[2], $x_window[1], $x_window[0]); while ($lhs->compare($rhs) > 0) { --$quotient_value[$q_index]; $lhs->value = array($quotient_value[$q_index]); $lhs = $lhs->multiply($temp); } $adjust = $this->_array_repeat(0, $q_index); $temp_value = array($quotient_value[$q_index]); $temp = $temp->multiply($y); $temp_value = &$temp->value; if (count($temp_value)) { $temp_value = array_merge($adjust, $temp_value); } $x = $x->subtract($temp); if ($x->compare($zero) < 0) { $temp_value = array_merge($adjust, $y_value); $x = $x->add($temp); --$quotient_value[$q_index]; } $x_max = count($x_value) - 1; } // unnormalize the remainder $x->_rshift($shift); $quotient->is_negative = $x_sign != $y_sign; // calculate the "common residue", if appropriate if ($x_sign) { $y->_rshift($shift); $x = $y->subtract($x); } return array($this->_normalize($quotient), $this->_normalize($x)); } /** * Divides a BigInteger by a regular integer * * abc / x = a00 / x + b0 / x + c / x * * @param array $dividend * @param array $divisor * @return array * @access private */ function _divide_digit($dividend, $divisor) { $carry = 0; $result = array(); for ($i = count($dividend) - 1; $i >= 0; --$i) { $temp = self::$baseFull * $carry + $dividend[$i]; $result[$i] = $this->_safe_divide($temp, $divisor); $carry = (int) ($temp - $divisor * $result[$i]); } return array($result, $carry); } /** * Performs modular exponentiation. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger('10'); * $b = new \phpseclib\Math\BigInteger('20'); * $c = new \phpseclib\Math\BigInteger('30'); * * $c = $a->modPow($b, $c); * * echo $c->toString(); // outputs 10 * ?> * </code> * * @param \phpseclib\Math\BigInteger $e * @param \phpseclib\Math\BigInteger $n * @return \phpseclib\Math\BigInteger * @access public * @internal The most naive approach to modular exponentiation has very unreasonable requirements, and * and although the approach involving repeated squaring does vastly better, it, too, is impractical * for our purposes. The reason being that division - by far the most complicated and time-consuming * of the basic operations (eg. +,-,*,/) - occurs multiple times within it. * * Modular reductions resolve this issue. Although an individual modular reduction takes more time * then an individual division, when performed in succession (with the same modulo), they're a lot faster. * * The two most commonly used modular reductions are Barrett and Montgomery reduction. Montgomery reduction, * although faster, only works when the gcd of the modulo and of the base being used is 1. In RSA, when the * base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because * the product of two odd numbers is odd), but what about when RSA isn't used? * * In contrast, Barrett reduction has no such constraint. As such, some bigint implementations perform a * Barrett reduction after every operation in the modpow function. Others perform Barrett reductions when the * modulo is even and Montgomery reductions when the modulo is odd. BigInteger.java's modPow method, however, * uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and * the other, a power of two - and recombine them, later. This is the method that this modPow function uses. * {@link http://islab.oregonstate.edu/papers/j34monex.pdf Montgomery Reduction with Even Modulus} elaborates. */ function modPow($e, $n) { $n = $this->bitmask !== false && $this->bitmask->compare($n) < 0 ? $this->bitmask : $n->abs(); if ($e->compare(new static()) < 0) { $e = $e->abs(); $temp = $this->modInverse($n); if ($temp === false) { return false; } return $this->_normalize($temp->modPow($e, $n)); } if (MATH_BIGINTEGER_MODE == self::MODE_GMP) { $temp = new static(); $temp->value = gmp_powm($this->value, $e->value, $n->value); return $this->_normalize($temp); } if ($this->compare(new static()) < 0 || $this->compare($n) > 0) { list(, $temp) = $this->divide($n); return $temp->modPow($e, $n); } if (defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { $components = array( 'modulus' => $n->toBytes(true), 'publicExponent' => $e->toBytes(true) ); $components = array( 'modulus' => pack('Ca*a*', 2, $this->_encodeASN1Length(strlen($components['modulus'])), $components['modulus']), 'publicExponent' => pack('Ca*a*', 2, $this->_encodeASN1Length(strlen($components['publicExponent'])), $components['publicExponent']) ); $RSAPublicKey = pack( 'Ca*a*a*', 48, $this->_encodeASN1Length(strlen($components['modulus']) + strlen($components['publicExponent'])), $components['modulus'], $components['publicExponent'] ); $rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA $RSAPublicKey = chr(0) . $RSAPublicKey; $RSAPublicKey = chr(3) . $this->_encodeASN1Length(strlen($RSAPublicKey)) . $RSAPublicKey; $encapsulated = pack( 'Ca*a*', 48, $this->_encodeASN1Length(strlen($rsaOID . $RSAPublicKey)), $rsaOID . $RSAPublicKey ); $RSAPublicKey = "-----BEGIN PUBLIC KEY-----\r\n" . chunk_split(base64_encode($encapsulated)) . '-----END PUBLIC KEY-----'; $plaintext = str_pad($this->toBytes(), strlen($n->toBytes(true)) - 1, "\0", STR_PAD_LEFT); if (openssl_public_encrypt($plaintext, $result, $RSAPublicKey, OPENSSL_NO_PADDING)) { return new static($result, 256); } } if (MATH_BIGINTEGER_MODE == self::MODE_BCMATH) { $temp = new static(); $temp->value = bcpowmod($this->value, $e->value, $n->value, 0); return $this->_normalize($temp); } if (empty($e->value)) { $temp = new static(); $temp->value = array(1); return $this->_normalize($temp); } if ($e->value == array(1)) { list(, $temp) = $this->divide($n); return $this->_normalize($temp); } if ($e->value == array(2)) { $temp = new static(); $temp->value = $this->_square($this->value); list(, $temp) = $temp->divide($n); return $this->_normalize($temp); } return $this->_normalize($this->_slidingWindow($e, $n, self::BARRETT)); // the following code, although not callable, can be run independently of the above code // although the above code performed better in my benchmarks the following could might // perform better under different circumstances. in lieu of deleting it it's just been // made uncallable // is the modulo odd? if ($n->value[0] & 1) { return $this->_normalize($this->_slidingWindow($e, $n, self::MONTGOMERY)); } // if it's not, it's even // find the lowest set bit (eg. the max pow of 2 that divides $n) for ($i = 0; $i < count($n->value); ++$i) { if ($n->value[$i]) { $temp = decbin($n->value[$i]); $j = strlen($temp) - strrpos($temp, '1') - 1; $j+= 26 * $i; break; } } // at this point, 2^$j * $n/(2^$j) == $n $mod1 = $n->copy(); $mod1->_rshift($j); $mod2 = new static(); $mod2->value = array(1); $mod2->_lshift($j); $part1 = ($mod1->value != array(1)) ? $this->_slidingWindow($e, $mod1, self::MONTGOMERY) : new static(); $part2 = $this->_slidingWindow($e, $mod2, self::POWEROF2); $y1 = $mod2->modInverse($mod1); $y2 = $mod1->modInverse($mod2); $result = $part1->multiply($mod2); $result = $result->multiply($y1); $temp = $part2->multiply($mod1); $temp = $temp->multiply($y2); $result = $result->add($temp); list(, $result) = $result->divide($n); return $this->_normalize($result); } /** * Performs modular exponentiation. * * Alias for modPow(). * * @param \phpseclib\Math\BigInteger $e * @param \phpseclib\Math\BigInteger $n * @return \phpseclib\Math\BigInteger * @access public */ function powMod($e, $n) { return $this->modPow($e, $n); } /** * Sliding Window k-ary Modular Exponentiation * * Based on {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=27 HAC 14.85} / * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=210 MPM 7.7}. In a departure from those algorithims, * however, this function performs a modular reduction after every multiplication and squaring operation. * As such, this function has the same preconditions that the reductions being used do. * * @param \phpseclib\Math\BigInteger $e * @param \phpseclib\Math\BigInteger $n * @param int $mode * @return \phpseclib\Math\BigInteger * @access private */ function _slidingWindow($e, $n, $mode) { static $window_ranges = array(7, 25, 81, 241, 673, 1793); // from BigInteger.java's oddModPow function //static $window_ranges = array(0, 7, 36, 140, 450, 1303, 3529); // from MPM 7.3.1 $e_value = $e->value; $e_length = count($e_value) - 1; $e_bits = decbin($e_value[$e_length]); for ($i = $e_length - 1; $i >= 0; --$i) { $e_bits.= str_pad(decbin($e_value[$i]), self::$base, '0', STR_PAD_LEFT); } $e_length = strlen($e_bits); // calculate the appropriate window size. // $window_size == 3 if $window_ranges is between 25 and 81, for example. for ($i = 0, $window_size = 1; $i < count($window_ranges) && $e_length > $window_ranges[$i]; ++$window_size, ++$i) { } $n_value = $n->value; // precompute $this^0 through $this^$window_size $powers = array(); $powers[1] = $this->_prepareReduce($this->value, $n_value, $mode); $powers[2] = $this->_squareReduce($powers[1], $n_value, $mode); // we do every other number since substr($e_bits, $i, $j+1) (see below) is supposed to end // in a 1. ie. it's supposed to be odd. $temp = 1 << ($window_size - 1); for ($i = 1; $i < $temp; ++$i) { $i2 = $i << 1; $powers[$i2 + 1] = $this->_multiplyReduce($powers[$i2 - 1], $powers[2], $n_value, $mode); } $result = array(1); $result = $this->_prepareReduce($result, $n_value, $mode); for ($i = 0; $i < $e_length;) { if (!$e_bits[$i]) { $result = $this->_squareReduce($result, $n_value, $mode); ++$i; } else { for ($j = $window_size - 1; $j > 0; --$j) { if (!empty($e_bits[$i + $j])) { break; } } // eg. the length of substr($e_bits, $i, $j + 1) for ($k = 0; $k <= $j; ++$k) { $result = $this->_squareReduce($result, $n_value, $mode); } $result = $this->_multiplyReduce($result, $powers[bindec(substr($e_bits, $i, $j + 1))], $n_value, $mode); $i += $j + 1; } } $temp = new static(); $temp->value = $this->_reduce($result, $n_value, $mode); return $temp; } /** * Modular reduction * * For most $modes this will return the remainder. * * @see self::_slidingWindow() * @access private * @param array $x * @param array $n * @param int $mode * @return array */ function _reduce($x, $n, $mode) { switch ($mode) { case self::MONTGOMERY: return $this->_montgomery($x, $n); case self::BARRETT: return $this->_barrett($x, $n); case self::POWEROF2: $lhs = new static(); $lhs->value = $x; $rhs = new static(); $rhs->value = $n; return $x->_mod2($n); case self::CLASSIC: $lhs = new static(); $lhs->value = $x; $rhs = new static(); $rhs->value = $n; list(, $temp) = $lhs->divide($rhs); return $temp->value; case self::NONE: return $x; default: // an invalid $mode was provided } } /** * Modular reduction preperation * * @see self::_slidingWindow() * @access private * @param array $x * @param array $n * @param int $mode * @return array */ function _prepareReduce($x, $n, $mode) { if ($mode == self::MONTGOMERY) { return $this->_prepMontgomery($x, $n); } return $this->_reduce($x, $n, $mode); } /** * Modular multiply * * @see self::_slidingWindow() * @access private * @param array $x * @param array $y * @param array $n * @param int $mode * @return array */ function _multiplyReduce($x, $y, $n, $mode) { if ($mode == self::MONTGOMERY) { return $this->_montgomeryMultiply($x, $y, $n); } $temp = $this->_multiply($x, false, $y, false); return $this->_reduce($temp[self::VALUE], $n, $mode); } /** * Modular square * * @see self::_slidingWindow() * @access private * @param array $x * @param array $n * @param int $mode * @return array */ function _squareReduce($x, $n, $mode) { if ($mode == self::MONTGOMERY) { return $this->_montgomeryMultiply($x, $x, $n); } return $this->_reduce($this->_square($x), $n, $mode); } /** * Modulos for Powers of Two * * Calculates $x%$n, where $n = 2**$e, for some $e. Since this is basically the same as doing $x & ($n-1), * we'll just use this function as a wrapper for doing that. * * @see self::_slidingWindow() * @access private * @param \phpseclib\Math\BigInteger * @return \phpseclib\Math\BigInteger */ function _mod2($n) { $temp = new static(); $temp->value = array(1); return $this->bitwise_and($n->subtract($temp)); } /** * Barrett Modular Reduction * * See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=14 HAC 14.3.3} / * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=165 MPM 6.2.5} for more information. Modified slightly, * so as not to require negative numbers (initially, this script didn't support negative numbers). * * Employs "folding", as described at * {@link http://www.cosic.esat.kuleuven.be/publications/thesis-149.pdf#page=66 thesis-149.pdf#page=66}. To quote from * it, "the idea [behind folding] is to find a value x' such that x (mod m) = x' (mod m), with x' being smaller than x." * * Unfortunately, the "Barrett Reduction with Folding" algorithm described in thesis-149.pdf is not, as written, all that * usable on account of (1) its not using reasonable radix points as discussed in * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=162 MPM 6.2.2} and (2) the fact that, even with reasonable * radix points, it only works when there are an even number of digits in the denominator. The reason for (2) is that * (x >> 1) + (x >> 1) != x / 2 + x / 2. If x is even, they're the same, but if x is odd, they're not. See the in-line * comments for details. * * @see self::_slidingWindow() * @access private * @param array $n * @param array $m * @return array */ function _barrett($n, $m) { static $cache = array( self::VARIABLE => array(), self::DATA => array() ); $m_length = count($m); // if ($this->_compare($n, $this->_square($m)) >= 0) { if (count($n) > 2 * $m_length) { $lhs = new static(); $rhs = new static(); $lhs->value = $n; $rhs->value = $m; list(, $temp) = $lhs->divide($rhs); return $temp->value; } // if (m.length >> 1) + 2 <= m.length then m is too small and n can't be reduced if ($m_length < 5) { return $this->_regularBarrett($n, $m); } // n = 2 * m.length if (($key = array_search($m, $cache[self::VARIABLE])) === false) { $key = count($cache[self::VARIABLE]); $cache[self::VARIABLE][] = $m; $lhs = new static(); $lhs_value = &$lhs->value; $lhs_value = $this->_array_repeat(0, $m_length + ($m_length >> 1)); $lhs_value[] = 1; $rhs = new static(); $rhs->value = $m; list($u, $m1) = $lhs->divide($rhs); $u = $u->value; $m1 = $m1->value; $cache[self::DATA][] = array( 'u' => $u, // m.length >> 1 (technically (m.length >> 1) + 1) 'm1'=> $m1 // m.length ); } else { extract($cache[self::DATA][$key]); } $cutoff = $m_length + ($m_length >> 1); $lsd = array_slice($n, 0, $cutoff); // m.length + (m.length >> 1) $msd = array_slice($n, $cutoff); // m.length >> 1 $lsd = $this->_trim($lsd); $temp = $this->_multiply($msd, false, $m1, false); $n = $this->_add($lsd, false, $temp[self::VALUE], false); // m.length + (m.length >> 1) + 1 if ($m_length & 1) { return $this->_regularBarrett($n[self::VALUE], $m); } // (m.length + (m.length >> 1) + 1) - (m.length - 1) == (m.length >> 1) + 2 $temp = array_slice($n[self::VALUE], $m_length - 1); // if even: ((m.length >> 1) + 2) + (m.length >> 1) == m.length + 2 // if odd: ((m.length >> 1) + 2) + (m.length >> 1) == (m.length - 1) + 2 == m.length + 1 $temp = $this->_multiply($temp, false, $u, false); // if even: (m.length + 2) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) + 1 // if odd: (m.length + 1) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) $temp = array_slice($temp[self::VALUE], ($m_length >> 1) + 1); // if even: (m.length - (m.length >> 1) + 1) + m.length = 2 * m.length - (m.length >> 1) + 1 // if odd: (m.length - (m.length >> 1)) + m.length = 2 * m.length - (m.length >> 1) $temp = $this->_multiply($temp, false, $m, false); // at this point, if m had an odd number of digits, we'd be subtracting a 2 * m.length - (m.length >> 1) digit // number from a m.length + (m.length >> 1) + 1 digit number. ie. there'd be an extra digit and the while loop // following this comment would loop a lot (hence our calling _regularBarrett() in that situation). $result = $this->_subtract($n[self::VALUE], false, $temp[self::VALUE], false); while ($this->_compare($result[self::VALUE], $result[self::SIGN], $m, false) >= 0) { $result = $this->_subtract($result[self::VALUE], $result[self::SIGN], $m, false); } return $result[self::VALUE]; } /** * (Regular) Barrett Modular Reduction * * For numbers with more than four digits BigInteger::_barrett() is faster. The difference between that and this * is that this function does not fold the denominator into a smaller form. * * @see self::_slidingWindow() * @access private * @param array $x * @param array $n * @return array */ function _regularBarrett($x, $n) { static $cache = array( self::VARIABLE => array(), self::DATA => array() ); $n_length = count($n); if (count($x) > 2 * $n_length) { $lhs = new static(); $rhs = new static(); $lhs->value = $x; $rhs->value = $n; list(, $temp) = $lhs->divide($rhs); return $temp->value; } if (($key = array_search($n, $cache[self::VARIABLE])) === false) { $key = count($cache[self::VARIABLE]); $cache[self::VARIABLE][] = $n; $lhs = new static(); $lhs_value = &$lhs->value; $lhs_value = $this->_array_repeat(0, 2 * $n_length); $lhs_value[] = 1; $rhs = new static(); $rhs->value = $n; list($temp, ) = $lhs->divide($rhs); // m.length $cache[self::DATA][] = $temp->value; } // 2 * m.length - (m.length - 1) = m.length + 1 $temp = array_slice($x, $n_length - 1); // (m.length + 1) + m.length = 2 * m.length + 1 $temp = $this->_multiply($temp, false, $cache[self::DATA][$key], false); // (2 * m.length + 1) - (m.length - 1) = m.length + 2 $temp = array_slice($temp[self::VALUE], $n_length + 1); // m.length + 1 $result = array_slice($x, 0, $n_length + 1); // m.length + 1 $temp = $this->_multiplyLower($temp, false, $n, false, $n_length + 1); // $temp == array_slice($temp->_multiply($temp, false, $n, false)->value, 0, $n_length + 1) if ($this->_compare($result, false, $temp[self::VALUE], $temp[self::SIGN]) < 0) { $corrector_value = $this->_array_repeat(0, $n_length + 1); $corrector_value[count($corrector_value)] = 1; $result = $this->_add($result, false, $corrector_value, false); $result = $result[self::VALUE]; } // at this point, we're subtracting a number with m.length + 1 digits from another number with m.length + 1 digits $result = $this->_subtract($result, false, $temp[self::VALUE], $temp[self::SIGN]); while ($this->_compare($result[self::VALUE], $result[self::SIGN], $n, false) > 0) { $result = $this->_subtract($result[self::VALUE], $result[self::SIGN], $n, false); } return $result[self::VALUE]; } /** * Performs long multiplication up to $stop digits * * If you're going to be doing array_slice($product->value, 0, $stop), some cycles can be saved. * * @see self::_regularBarrett() * @param array $x_value * @param bool $x_negative * @param array $y_value * @param bool $y_negative * @param int $stop * @return array * @access private */ function _multiplyLower($x_value, $x_negative, $y_value, $y_negative, $stop) { $x_length = count($x_value); $y_length = count($y_value); if (!$x_length || !$y_length) { // a 0 is being multiplied return array( self::VALUE => array(), self::SIGN => false ); } if ($x_length < $y_length) { $temp = $x_value; $x_value = $y_value; $y_value = $temp; $x_length = count($x_value); $y_length = count($y_value); } $product_value = $this->_array_repeat(0, $x_length + $y_length); // the following for loop could be removed if the for loop following it // (the one with nested for loops) initially set $i to 0, but // doing so would also make the result in one set of unnecessary adds, // since on the outermost loops first pass, $product->value[$k] is going // to always be 0 $carry = 0; for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0, $k = $i $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0 $carry = self::$base === 26 ? intval($temp / 0x4000000) : ($temp >> 31); $product_value[$j] = (int) ($temp - self::$baseFull * $carry); } if ($j < $stop) { $product_value[$j] = $carry; } // the above for loop is what the previous comment was talking about. the // following for loop is the "one with nested for loops" for ($i = 1; $i < $y_length; ++$i) { $carry = 0; for ($j = 0, $k = $i; $j < $x_length && $k < $stop; ++$j, ++$k) { $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry; $carry = self::$base === 26 ? intval($temp / 0x4000000) : ($temp >> 31); $product_value[$k] = (int) ($temp - self::$baseFull * $carry); } if ($k < $stop) { $product_value[$k] = $carry; } } return array( self::VALUE => $this->_trim($product_value), self::SIGN => $x_negative != $y_negative ); } /** * Montgomery Modular Reduction * * ($x->_prepMontgomery($n))->_montgomery($n) yields $x % $n. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=170 MPM 6.3} provides insights on how this can be * improved upon (basically, by using the comba method). gcd($n, 2) must be equal to one for this function * to work correctly. * * @see self::_prepMontgomery() * @see self::_slidingWindow() * @access private * @param array $x * @param array $n * @return array */ function _montgomery($x, $n) { static $cache = array( self::VARIABLE => array(), self::DATA => array() ); if (($key = array_search($n, $cache[self::VARIABLE])) === false) { $key = count($cache[self::VARIABLE]); $cache[self::VARIABLE][] = $x; $cache[self::DATA][] = $this->_modInverse67108864($n); } $k = count($n); $result = array(self::VALUE => $x); for ($i = 0; $i < $k; ++$i) { $temp = $result[self::VALUE][$i] * $cache[self::DATA][$key]; $temp = $temp - self::$baseFull * (self::$base === 26 ? intval($temp / 0x4000000) : ($temp >> 31)); $temp = $this->_regularMultiply(array($temp), $n); $temp = array_merge($this->_array_repeat(0, $i), $temp); $result = $this->_add($result[self::VALUE], false, $temp, false); } $result[self::VALUE] = array_slice($result[self::VALUE], $k); if ($this->_compare($result, false, $n, false) >= 0) { $result = $this->_subtract($result[self::VALUE], false, $n, false); } return $result[self::VALUE]; } /** * Montgomery Multiply * * Interleaves the montgomery reduction and long multiplication algorithms together as described in * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=13 HAC 14.36} * * @see self::_prepMontgomery() * @see self::_montgomery() * @access private * @param array $x * @param array $y * @param array $m * @return array */ function _montgomeryMultiply($x, $y, $m) { $temp = $this->_multiply($x, false, $y, false); return $this->_montgomery($temp[self::VALUE], $m); // the following code, although not callable, can be run independently of the above code // although the above code performed better in my benchmarks the following could might // perform better under different circumstances. in lieu of deleting it it's just been // made uncallable static $cache = array( self::VARIABLE => array(), self::DATA => array() ); if (($key = array_search($m, $cache[self::VARIABLE])) === false) { $key = count($cache[self::VARIABLE]); $cache[self::VARIABLE][] = $m; $cache[self::DATA][] = $this->_modInverse67108864($m); } $n = max(count($x), count($y), count($m)); $x = array_pad($x, $n, 0); $y = array_pad($y, $n, 0); $m = array_pad($m, $n, 0); $a = array(self::VALUE => $this->_array_repeat(0, $n + 1)); for ($i = 0; $i < $n; ++$i) { $temp = $a[self::VALUE][0] + $x[$i] * $y[0]; $temp = $temp - self::$baseFull * (self::$base === 26 ? intval($temp / 0x4000000) : ($temp >> 31)); $temp = $temp * $cache[self::DATA][$key]; $temp = $temp - self::$baseFull * (self::$base === 26 ? intval($temp / 0x4000000) : ($temp >> 31)); $temp = $this->_add($this->_regularMultiply(array($x[$i]), $y), false, $this->_regularMultiply(array($temp), $m), false); $a = $this->_add($a[self::VALUE], false, $temp[self::VALUE], false); $a[self::VALUE] = array_slice($a[self::VALUE], 1); } if ($this->_compare($a[self::VALUE], false, $m, false) >= 0) { $a = $this->_subtract($a[self::VALUE], false, $m, false); } return $a[self::VALUE]; } /** * Prepare a number for use in Montgomery Modular Reductions * * @see self::_montgomery() * @see self::_slidingWindow() * @access private * @param array $x * @param array $n * @return array */ function _prepMontgomery($x, $n) { $lhs = new static(); $lhs->value = array_merge($this->_array_repeat(0, count($n)), $x); $rhs = new static(); $rhs->value = $n; list(, $temp) = $lhs->divide($rhs); return $temp->value; } /** * Modular Inverse of a number mod 2**26 (eg. 67108864) * * Based off of the bnpInvDigit function implemented and justified in the following URL: * * {@link http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js} * * The following URL provides more info: * * {@link http://groups.google.com/group/sci.crypt/msg/7a137205c1be7d85} * * As for why we do all the bitmasking... strange things can happen when converting from floats to ints. For * instance, on some computers, var_dump((int) -4294967297) yields int(-1) and on others, it yields * int(-2147483648). To avoid problems stemming from this, we use bitmasks to guarantee that ints aren't * auto-converted to floats. The outermost bitmask is present because without it, there's no guarantee that * the "residue" returned would be the so-called "common residue". We use fmod, in the last step, because the * maximum possible $x is 26 bits and the maximum $result is 16 bits. Thus, we have to be able to handle up to * 40 bits, which only 64-bit floating points will support. * * Thanks to Pedro Gimeno Fortea for input! * * @see self::_montgomery() * @access private * @param array $x * @return int */ function _modInverse67108864($x) // 2**26 == 67,108,864 { $x = -$x[0]; $result = $x & 0x3; // x**-1 mod 2**2 $result = ($result * (2 - $x * $result)) & 0xF; // x**-1 mod 2**4 $result = ($result * (2 - ($x & 0xFF) * $result)) & 0xFF; // x**-1 mod 2**8 $result = ($result * ((2 - ($x & 0xFFFF) * $result) & 0xFFFF)) & 0xFFFF; // x**-1 mod 2**16 $result = fmod($result * (2 - fmod($x * $result, self::$baseFull)), self::$baseFull); // x**-1 mod 2**26 return $result & self::$maxDigit; } /** * Calculates modular inverses. * * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger(30); * $b = new \phpseclib\Math\BigInteger(17); * * $c = $a->modInverse($b); * echo $c->toString(); // outputs 4 * * echo "\r\n"; * * $d = $a->multiply($c); * list(, $d) = $d->divide($b); * echo $d; // outputs 1 (as per the definition of modular inverse) * ?> * </code> * * @param \phpseclib\Math\BigInteger $n * @return \phpseclib\Math\BigInteger|false * @access public * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=21 HAC 14.64} for more information. */ function modInverse($n) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp = new static(); $temp->value = gmp_invert($this->value, $n->value); return ($temp->value === false) ? false : $this->_normalize($temp); } static $zero, $one; if (!isset($zero)) { $zero = new static(); $one = new static(1); } // $x mod -$n == $x mod $n. $n = $n->abs(); if ($this->compare($zero) < 0) { $temp = $this->abs(); $temp = $temp->modInverse($n); return $this->_normalize($n->subtract($temp)); } extract($this->extendedGCD($n)); if (!$gcd->equals($one)) { return false; } $x = $x->compare($zero) < 0 ? $x->add($n) : $x; return $this->compare($zero) < 0 ? $this->_normalize($n->subtract($x)) : $this->_normalize($x); } /** * Calculates the greatest common divisor and Bezout's identity. * * Say you have 693 and 609. The GCD is 21. Bezout's identity states that there exist integers x and y such that * 693*x + 609*y == 21. In point of fact, there are actually an infinite number of x and y combinations and which * combination is returned is dependent upon which mode is in use. See * {@link http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity Bezout's identity - Wikipedia} for more information. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger(693); * $b = new \phpseclib\Math\BigInteger(609); * * extract($a->extendedGCD($b)); * * echo $gcd->toString() . "\r\n"; // outputs 21 * echo $a->toString() * $x->toString() + $b->toString() * $y->toString(); // outputs 21 * ?> * </code> * * @param \phpseclib\Math\BigInteger $n * @return \phpseclib\Math\BigInteger * @access public * @internal Calculates the GCD using the binary xGCD algorithim described in * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=19 HAC 14.61}. As the text above 14.61 notes, * the more traditional algorithim requires "relatively costly multiple-precision divisions". */ function extendedGCD($n) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: extract(gmp_gcdext($this->value, $n->value)); return array( 'gcd' => $this->_normalize(new static($g)), 'x' => $this->_normalize(new static($s)), 'y' => $this->_normalize(new static($t)) ); case self::MODE_BCMATH: // it might be faster to use the binary xGCD algorithim here, as well, but (1) that algorithim works // best when the base is a power of 2 and (2) i don't think it'd make much difference, anyway. as is, // the basic extended euclidean algorithim is what we're using. $u = $this->value; $v = $n->value; $a = '1'; $b = '0'; $c = '0'; $d = '1'; while (bccomp($v, '0', 0) != 0) { $q = bcdiv($u, $v, 0); $temp = $u; $u = $v; $v = bcsub($temp, bcmul($v, $q, 0), 0); $temp = $a; $a = $c; $c = bcsub($temp, bcmul($a, $q, 0), 0); $temp = $b; $b = $d; $d = bcsub($temp, bcmul($b, $q, 0), 0); } return array( 'gcd' => $this->_normalize(new static($u)), 'x' => $this->_normalize(new static($a)), 'y' => $this->_normalize(new static($b)) ); } $y = $n->copy(); $x = $this->copy(); $g = new static(); $g->value = array(1); while (!(($x->value[0] & 1)|| ($y->value[0] & 1))) { $x->_rshift(1); $y->_rshift(1); $g->_lshift(1); } $u = $x->copy(); $v = $y->copy(); $a = new static(); $b = new static(); $c = new static(); $d = new static(); $a->value = $d->value = $g->value = array(1); $b->value = $c->value = array(); while (!empty($u->value)) { while (!($u->value[0] & 1)) { $u->_rshift(1); if ((!empty($a->value) && ($a->value[0] & 1)) || (!empty($b->value) && ($b->value[0] & 1))) { $a = $a->add($y); $b = $b->subtract($x); } $a->_rshift(1); $b->_rshift(1); } while (!($v->value[0] & 1)) { $v->_rshift(1); if ((!empty($d->value) && ($d->value[0] & 1)) || (!empty($c->value) && ($c->value[0] & 1))) { $c = $c->add($y); $d = $d->subtract($x); } $c->_rshift(1); $d->_rshift(1); } if ($u->compare($v) >= 0) { $u = $u->subtract($v); $a = $a->subtract($c); $b = $b->subtract($d); } else { $v = $v->subtract($u); $c = $c->subtract($a); $d = $d->subtract($b); } } return array( 'gcd' => $this->_normalize($g->multiply($v)), 'x' => $this->_normalize($c), 'y' => $this->_normalize($d) ); } /** * Calculates the greatest common divisor * * Say you have 693 and 609. The GCD is 21. * * Here's an example: * <code> * <?php * $a = new \phpseclib\Math\BigInteger(693); * $b = new \phpseclib\Math\BigInteger(609); * * $gcd = a->extendedGCD($b); * * echo $gcd->toString() . "\r\n"; // outputs 21 * ?> * </code> * * @param \phpseclib\Math\BigInteger $n * @return \phpseclib\Math\BigInteger * @access public */ function gcd($n) { extract($this->extendedGCD($n)); return $gcd; } /** * Absolute value. * * @return \phpseclib\Math\BigInteger * @access public */ function abs() { $temp = new static(); switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp->value = gmp_abs($this->value); break; case self::MODE_BCMATH: $temp->value = (bccomp($this->value, '0', 0) < 0) ? substr($this->value, 1) : $this->value; break; default: $temp->value = $this->value; } return $temp; } /** * Compares two numbers. * * Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is * demonstrated thusly: * * $x > $y: $x->compare($y) > 0 * $x < $y: $x->compare($y) < 0 * $x == $y: $x->compare($y) == 0 * * Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y). * * @param \phpseclib\Math\BigInteger $y * @return int < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal. * @access public * @see self::equals() * @internal Could return $this->subtract($x), but that's not as fast as what we do do. */ function compare($y) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $r = gmp_cmp($this->value, $y->value); if ($r < -1) { $r = -1; } if ($r > 1) { $r = 1; } return $r; case self::MODE_BCMATH: return bccomp($this->value, $y->value, 0); } return $this->_compare($this->value, $this->is_negative, $y->value, $y->is_negative); } /** * Compares two numbers. * * @param array $x_value * @param bool $x_negative * @param array $y_value * @param bool $y_negative * @return int * @see self::compare() * @access private */ function _compare($x_value, $x_negative, $y_value, $y_negative) { if ($x_negative != $y_negative) { return (!$x_negative && $y_negative) ? 1 : -1; } $result = $x_negative ? -1 : 1; if (count($x_value) != count($y_value)) { return (count($x_value) > count($y_value)) ? $result : -$result; } $size = max(count($x_value), count($y_value)); $x_value = array_pad($x_value, $size, 0); $y_value = array_pad($y_value, $size, 0); for ($i = count($x_value) - 1; $i >= 0; --$i) { if ($x_value[$i] != $y_value[$i]) { return ($x_value[$i] > $y_value[$i]) ? $result : -$result; } } return 0; } /** * Tests the equality of two numbers. * * If you need to see if one number is greater than or less than another number, use BigInteger::compare() * * @param \phpseclib\Math\BigInteger $x * @return bool * @access public * @see self::compare() */ function equals($x) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: return gmp_cmp($this->value, $x->value) == 0; default: return $this->value === $x->value && $this->is_negative == $x->is_negative; } } /** * Set Precision * * Some bitwise operations give different results depending on the precision being used. Examples include left * shift, not, and rotates. * * @param int $bits * @access public */ function setPrecision($bits) { $this->precision = $bits; if (MATH_BIGINTEGER_MODE != self::MODE_BCMATH) { $this->bitmask = new static(chr((1 << ($bits & 0x7)) - 1) . str_repeat(chr(0xFF), $bits >> 3), 256); } else { $this->bitmask = new static(bcpow('2', $bits, 0)); } $temp = $this->_normalize($this); $this->value = $temp->value; } /** * Logical And * * @param \phpseclib\Math\BigInteger $x * @access public * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat> * @return \phpseclib\Math\BigInteger */ function bitwise_and($x) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp = new static(); $temp->value = gmp_and($this->value, $x->value); return $this->_normalize($temp); case self::MODE_BCMATH: $left = $this->toBytes(); $right = $x->toBytes(); $length = max(strlen($left), strlen($right)); $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); return $this->_normalize(new static($left & $right, 256)); } $result = $this->copy(); $length = min(count($x->value), count($this->value)); $result->value = array_slice($result->value, 0, $length); for ($i = 0; $i < $length; ++$i) { $result->value[$i]&= $x->value[$i]; } return $this->_normalize($result); } /** * Logical Or * * @param \phpseclib\Math\BigInteger $x * @access public * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat> * @return \phpseclib\Math\BigInteger */ function bitwise_or($x) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp = new static(); $temp->value = gmp_or($this->value, $x->value); return $this->_normalize($temp); case self::MODE_BCMATH: $left = $this->toBytes(); $right = $x->toBytes(); $length = max(strlen($left), strlen($right)); $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); return $this->_normalize(new static($left | $right, 256)); } $length = max(count($this->value), count($x->value)); $result = $this->copy(); $result->value = array_pad($result->value, $length, 0); $x->value = array_pad($x->value, $length, 0); for ($i = 0; $i < $length; ++$i) { $result->value[$i]|= $x->value[$i]; } return $this->_normalize($result); } /** * Logical Exclusive-Or * * @param \phpseclib\Math\BigInteger $x * @access public * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat> * @return \phpseclib\Math\BigInteger */ function bitwise_xor($x) { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp = new static(); $temp->value = gmp_xor(gmp_abs($this->value), gmp_abs($x->value)); return $this->_normalize($temp); case self::MODE_BCMATH: $left = $this->toBytes(); $right = $x->toBytes(); $length = max(strlen($left), strlen($right)); $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); return $this->_normalize(new static($left ^ $right, 256)); } $length = max(count($this->value), count($x->value)); $result = $this->copy(); $result->is_negative = false; $result->value = array_pad($result->value, $length, 0); $x->value = array_pad($x->value, $length, 0); for ($i = 0; $i < $length; ++$i) { $result->value[$i]^= $x->value[$i]; } return $this->_normalize($result); } /** * Logical Not * * @access public * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat> * @return \phpseclib\Math\BigInteger */ function bitwise_not() { // calculuate "not" without regard to $this->precision // (will always result in a smaller number. ie. ~1 isn't 1111 1110 - it's 0) $temp = $this->toBytes(); if ($temp == '') { return $this->_normalize(new static()); } $pre_msb = decbin(ord($temp[0])); $temp = ~$temp; $msb = decbin(ord($temp[0])); if (strlen($msb) == 8) { $msb = substr($msb, strpos($msb, '0')); } $temp[0] = chr(bindec($msb)); // see if we need to add extra leading 1's $current_bits = strlen($pre_msb) + 8 * strlen($temp) - 8; $new_bits = $this->precision - $current_bits; if ($new_bits <= 0) { return $this->_normalize(new static($temp, 256)); } // generate as many leading 1's as we need to. $leading_ones = chr((1 << ($new_bits & 0x7)) - 1) . str_repeat(chr(0xFF), $new_bits >> 3); $this->_base256_lshift($leading_ones, $current_bits); $temp = str_pad($temp, strlen($leading_ones), chr(0), STR_PAD_LEFT); return $this->_normalize(new static($leading_ones | $temp, 256)); } /** * Logical Right Shift * * Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift. * * @param int $shift * @return \phpseclib\Math\BigInteger * @access public * @internal The only version that yields any speed increases is the internal version. */ function bitwise_rightShift($shift) { $temp = new static(); switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: static $two; if (!isset($two)) { $two = gmp_init('2'); } $temp->value = gmp_div_q($this->value, gmp_pow($two, $shift)); break; case self::MODE_BCMATH: $temp->value = bcdiv($this->value, bcpow('2', $shift, 0), 0); break; default: // could just replace _lshift with this, but then all _lshift() calls would need to be rewritten // and I don't want to do that... $temp->value = $this->value; $temp->_rshift($shift); } return $this->_normalize($temp); } /** * Logical Left Shift * * Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift. * * @param int $shift * @return \phpseclib\Math\BigInteger * @access public * @internal The only version that yields any speed increases is the internal version. */ function bitwise_leftShift($shift) { $temp = new static(); switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: static $two; if (!isset($two)) { $two = gmp_init('2'); } $temp->value = gmp_mul($this->value, gmp_pow($two, $shift)); break; case self::MODE_BCMATH: $temp->value = bcmul($this->value, bcpow('2', $shift, 0), 0); break; default: // could just replace _rshift with this, but then all _lshift() calls would need to be rewritten // and I don't want to do that... $temp->value = $this->value; $temp->_lshift($shift); } return $this->_normalize($temp); } /** * Logical Left Rotate * * Instead of the top x bits being dropped they're appended to the shifted bit string. * * @param int $shift * @return \phpseclib\Math\BigInteger * @access public */ function bitwise_leftRotate($shift) { $bits = $this->toBytes(); if ($this->precision > 0) { $precision = $this->precision; if (MATH_BIGINTEGER_MODE == self::MODE_BCMATH) { $mask = $this->bitmask->subtract(new static(1)); $mask = $mask->toBytes(); } else { $mask = $this->bitmask->toBytes(); } } else { $temp = ord($bits[0]); for ($i = 0; $temp >> $i; ++$i) { } $precision = 8 * strlen($bits) - 8 + $i; $mask = chr((1 << ($precision & 0x7)) - 1) . str_repeat(chr(0xFF), $precision >> 3); } if ($shift < 0) { $shift+= $precision; } $shift%= $precision; if (!$shift) { return $this->copy(); } $left = $this->bitwise_leftShift($shift); $left = $left->bitwise_and(new static($mask, 256)); $right = $this->bitwise_rightShift($precision - $shift); $result = MATH_BIGINTEGER_MODE != self::MODE_BCMATH ? $left->bitwise_or($right) : $left->add($right); return $this->_normalize($result); } /** * Logical Right Rotate * * Instead of the bottom x bits being dropped they're prepended to the shifted bit string. * * @param int $shift * @return \phpseclib\Math\BigInteger * @access public */ function bitwise_rightRotate($shift) { return $this->bitwise_leftRotate(-$shift); } /** * Generates a random BigInteger * * Byte length is equal to $length. Uses \phpseclib\Crypt\Random if it's loaded and mt_rand if it's not. * * @param int $length * @return \phpseclib\Math\BigInteger * @access private */ function _random_number_helper($size) { if (class_exists('\phpseclib\Crypt\Random')) { $random = Random::string($size); } else { $random = ''; if ($size & 1) { $random.= chr(mt_rand(0, 255)); } $blocks = $size >> 1; for ($i = 0; $i < $blocks; ++$i) { // mt_rand(-2147483648, 0x7FFFFFFF) always produces -2147483648 on some systems $random.= pack('n', mt_rand(0, 0xFFFF)); } } return new static($random, 256); } /** * Generate a random number * * Returns a random number between $min and $max where $min and $max * can be defined using one of the two methods: * * $min->random($max) * $max->random($min) * * @param \phpseclib\Math\BigInteger $arg1 * @param \phpseclib\Math\BigInteger $arg2 * @return \phpseclib\Math\BigInteger * @access public * @internal The API for creating random numbers used to be $a->random($min, $max), where $a was a BigInteger object. * That method is still supported for BC purposes. */ function random($arg1, $arg2 = false) { if ($arg1 === false) { return false; } if ($arg2 === false) { $max = $arg1; $min = $this; } else { $min = $arg1; $max = $arg2; } $compare = $max->compare($min); if (!$compare) { return $this->_normalize($min); } elseif ($compare < 0) { // if $min is bigger then $max, swap $min and $max $temp = $max; $max = $min; $min = $temp; } static $one; if (!isset($one)) { $one = new static(1); } $max = $max->subtract($min->subtract($one)); $size = strlen(ltrim($max->toBytes(), chr(0))); /* doing $random % $max doesn't work because some numbers will be more likely to occur than others. eg. if $max is 140 and $random's max is 255 then that'd mean both $random = 5 and $random = 145 would produce 5 whereas the only value of random that could produce 139 would be 139. ie. not all numbers would be equally likely. some would be more likely than others. creating a whole new random number until you find one that is within the range doesn't work because, for sufficiently small ranges, the likelihood that you'd get a number within that range would be pretty small. eg. with $random's max being 255 and if your $max being 1 the probability would be pretty high that $random would be greater than $max. phpseclib works around this using the technique described here: http://crypto.stackexchange.com/questions/5708/creating-a-small-number-from-a-cryptographically-secure-random-string */ $random_max = new static(chr(1) . str_repeat("\0", $size), 256); $random = $this->_random_number_helper($size); list($max_multiple) = $random_max->divide($max); $max_multiple = $max_multiple->multiply($max); while ($random->compare($max_multiple) >= 0) { $random = $random->subtract($max_multiple); $random_max = $random_max->subtract($max_multiple); $random = $random->bitwise_leftShift(8); $random = $random->add($this->_random_number_helper(1)); $random_max = $random_max->bitwise_leftShift(8); list($max_multiple) = $random_max->divide($max); $max_multiple = $max_multiple->multiply($max); } list(, $random) = $random->divide($max); return $this->_normalize($random->add($min)); } /** * Generate a random prime number. * * If there's not a prime within the given range, false will be returned. * If more than $timeout seconds have elapsed, give up and return false. * * @param \phpseclib\Math\BigInteger $arg1 * @param \phpseclib\Math\BigInteger $arg2 * @param int $timeout * @return Math_BigInteger|false * @access public * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}. */ function randomPrime($arg1, $arg2 = false, $timeout = false) { if ($arg1 === false) { return false; } if ($arg2 === false) { $max = $arg1; $min = $this; } else { $min = $arg1; $max = $arg2; } $compare = $max->compare($min); if (!$compare) { return $min->isPrime() ? $min : false; } elseif ($compare < 0) { // if $min is bigger then $max, swap $min and $max $temp = $max; $max = $min; $min = $temp; } static $one, $two; if (!isset($one)) { $one = new static(1); $two = new static(2); } $start = time(); $x = $this->random($min, $max); // gmp_nextprime() requires PHP 5 >= 5.2.0 per <http://php.net/gmp-nextprime>. if (MATH_BIGINTEGER_MODE == self::MODE_GMP && extension_loaded('gmp')) { $p = new static(); $p->value = gmp_nextprime($x->value); if ($p->compare($max) <= 0) { return $p; } if (!$min->equals($x)) { $x = $x->subtract($one); } return $x->randomPrime($min, $x); } if ($x->equals($two)) { return $x; } $x->_make_odd(); if ($x->compare($max) > 0) { // if $x > $max then $max is even and if $min == $max then no prime number exists between the specified range if ($min->equals($max)) { return false; } $x = $min->copy(); $x->_make_odd(); } $initial_x = $x->copy(); while (true) { if ($timeout !== false && time() - $start > $timeout) { return false; } if ($x->isPrime()) { return $x; } $x = $x->add($two); if ($x->compare($max) > 0) { $x = $min->copy(); if ($x->equals($two)) { return $x; } $x->_make_odd(); } if ($x->equals($initial_x)) { return false; } } } /** * Make the current number odd * * If the current number is odd it'll be unchanged. If it's even, one will be added to it. * * @see self::randomPrime() * @access private */ function _make_odd() { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: gmp_setbit($this->value, 0); break; case self::MODE_BCMATH: if ($this->value[strlen($this->value) - 1] % 2 == 0) { $this->value = bcadd($this->value, '1'); } break; default: $this->value[0] |= 1; } } /** * Checks a numer to see if it's prime * * Assuming the $t parameter is not set, this function has an error rate of 2**-80. The main motivation for the * $t parameter is distributability. BigInteger::randomPrime() can be distributed across multiple pageloads * on a website instead of just one. * * @param \phpseclib\Math\BigInteger $t * @return bool * @access public * @internal Uses the * {@link http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test Miller-Rabin primality test}. See * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=8 HAC 4.24}. */ function isPrime($t = false) { $length = strlen($this->toBytes()); if (!$t) { // see HAC 4.49 "Note (controlling the error probability)" // @codingStandardsIgnoreStart if ($length >= 163) { $t = 2; } // floor(1300 / 8) else if ($length >= 106) { $t = 3; } // floor( 850 / 8) else if ($length >= 81 ) { $t = 4; } // floor( 650 / 8) else if ($length >= 68 ) { $t = 5; } // floor( 550 / 8) else if ($length >= 56 ) { $t = 6; } // floor( 450 / 8) else if ($length >= 50 ) { $t = 7; } // floor( 400 / 8) else if ($length >= 43 ) { $t = 8; } // floor( 350 / 8) else if ($length >= 37 ) { $t = 9; } // floor( 300 / 8) else if ($length >= 31 ) { $t = 12; } // floor( 250 / 8) else if ($length >= 25 ) { $t = 15; } // floor( 200 / 8) else if ($length >= 18 ) { $t = 18; } // floor( 150 / 8) else { $t = 27; } // @codingStandardsIgnoreEnd } // ie. gmp_testbit($this, 0) // ie. isEven() or !isOdd() switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: return gmp_prob_prime($this->value, $t) != 0; case self::MODE_BCMATH: if ($this->value === '2') { return true; } if ($this->value[strlen($this->value) - 1] % 2 == 0) { return false; } break; default: if ($this->value == array(2)) { return true; } if (~$this->value[0] & 1) { return false; } } static $primes, $zero, $one, $two; if (!isset($primes)) { $primes = array( 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 ); if (MATH_BIGINTEGER_MODE != self::MODE_INTERNAL) { for ($i = 0; $i < count($primes); ++$i) { $primes[$i] = new static($primes[$i]); } } $zero = new static(); $one = new static(1); $two = new static(2); } if ($this->equals($one)) { return false; } // see HAC 4.4.1 "Random search for probable primes" if (MATH_BIGINTEGER_MODE != self::MODE_INTERNAL) { foreach ($primes as $prime) { list(, $r) = $this->divide($prime); if ($r->equals($zero)) { return $this->equals($prime); } } } else { $value = $this->value; foreach ($primes as $prime) { list(, $r) = $this->_divide_digit($value, $prime); if (!$r) { return count($value) == 1 && $value[0] == $prime; } } } $n = $this->copy(); $n_1 = $n->subtract($one); $n_2 = $n->subtract($two); $r = $n_1->copy(); $r_value = $r->value; // ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s)); if (MATH_BIGINTEGER_MODE == self::MODE_BCMATH) { $s = 0; // if $n was 1, $r would be 0 and this would be an infinite loop, hence our $this->equals($one) check earlier while ($r->value[strlen($r->value) - 1] % 2 == 0) { $r->value = bcdiv($r->value, '2', 0); ++$s; } } else { for ($i = 0, $r_length = count($r_value); $i < $r_length; ++$i) { $temp = ~$r_value[$i] & 0xFFFFFF; for ($j = 1; ($temp >> $j) & 1; ++$j) { } if ($j != 25) { break; } } $s = 26 * $i + $j; $r->_rshift($s); } for ($i = 0; $i < $t; ++$i) { $a = $this->random($two, $n_2); $y = $a->modPow($r, $n); if (!$y->equals($one) && !$y->equals($n_1)) { for ($j = 1; $j < $s && !$y->equals($n_1); ++$j) { $y = $y->modPow($two, $n); if ($y->equals($one)) { return false; } } if (!$y->equals($n_1)) { return false; } } } return true; } /** * Logical Left Shift * * Shifts BigInteger's by $shift bits. * * @param int $shift * @access private */ function _lshift($shift) { if ($shift == 0) { return; } $num_digits = (int) ($shift / self::$base); $shift %= self::$base; $shift = 1 << $shift; $carry = 0; for ($i = 0; $i < count($this->value); ++$i) { $temp = $this->value[$i] * $shift + $carry; $carry = self::$base === 26 ? intval($temp / 0x4000000) : ($temp >> 31); $this->value[$i] = (int) ($temp - $carry * self::$baseFull); } if ($carry) { $this->value[count($this->value)] = $carry; } while ($num_digits--) { array_unshift($this->value, 0); } } /** * Logical Right Shift * * Shifts BigInteger's by $shift bits. * * @param int $shift * @access private */ function _rshift($shift) { if ($shift == 0) { return; } $num_digits = (int) ($shift / self::$base); $shift %= self::$base; $carry_shift = self::$base - $shift; $carry_mask = (1 << $shift) - 1; if ($num_digits) { $this->value = array_slice($this->value, $num_digits); } $carry = 0; for ($i = count($this->value) - 1; $i >= 0; --$i) { $temp = $this->value[$i] >> $shift | $carry; $carry = ($this->value[$i] & $carry_mask) << $carry_shift; $this->value[$i] = $temp; } $this->value = $this->_trim($this->value); } /** * Normalize * * Removes leading zeros and truncates (if necessary) to maintain the appropriate precision * * @param \phpseclib\Math\BigInteger * @return \phpseclib\Math\BigInteger * @see self::_trim() * @access private */ function _normalize($result) { $result->precision = $this->precision; $result->bitmask = $this->bitmask; switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: if ($this->bitmask !== false) { $flip = gmp_cmp($result->value, gmp_init(0)) < 0; if ($flip) { $result->value = gmp_neg($result->value); } $result->value = gmp_and($result->value, $result->bitmask->value); if ($flip) { $result->value = gmp_neg($result->value); } } return $result; case self::MODE_BCMATH: if (!empty($result->bitmask->value)) { $result->value = bcmod($result->value, $result->bitmask->value); } return $result; } $value = &$result->value; if (!count($value)) { $result->is_negative = false; return $result; } $value = $this->_trim($value); if (!empty($result->bitmask->value)) { $length = min(count($value), count($this->bitmask->value)); $value = array_slice($value, 0, $length); for ($i = 0; $i < $length; ++$i) { $value[$i] = $value[$i] & $this->bitmask->value[$i]; } } return $result; } /** * Trim * * Removes leading zeros * * @param array $value * @return \phpseclib\Math\BigInteger * @access private */ function _trim($value) { for ($i = count($value) - 1; $i >= 0; --$i) { if ($value[$i]) { break; } unset($value[$i]); } return $value; } /** * Array Repeat * * @param $input Array * @param $multiplier mixed * @return array * @access private */ function _array_repeat($input, $multiplier) { return ($multiplier) ? array_fill(0, $multiplier, $input) : array(); } /** * Logical Left Shift * * Shifts binary strings $shift bits, essentially multiplying by 2**$shift. * * @param $x String * @param $shift Integer * @return string * @access private */ function _base256_lshift(&$x, $shift) { if ($shift == 0) { return; } $num_bytes = $shift >> 3; // eg. floor($shift/8) $shift &= 7; // eg. $shift % 8 $carry = 0; for ($i = strlen($x) - 1; $i >= 0; --$i) { $temp = ord($x[$i]) << $shift | $carry; $x[$i] = chr($temp); $carry = $temp >> 8; } $carry = ($carry != 0) ? chr($carry) : ''; $x = $carry . $x . str_repeat(chr(0), $num_bytes); } /** * Logical Right Shift * * Shifts binary strings $shift bits, essentially dividing by 2**$shift and returning the remainder. * * @param $x String * @param $shift Integer * @return string * @access private */ function _base256_rshift(&$x, $shift) { if ($shift == 0) { $x = ltrim($x, chr(0)); return ''; } $num_bytes = $shift >> 3; // eg. floor($shift/8) $shift &= 7; // eg. $shift % 8 $remainder = ''; if ($num_bytes) { $start = $num_bytes > strlen($x) ? -strlen($x) : -$num_bytes; $remainder = substr($x, $start); $x = substr($x, 0, -$num_bytes); } $carry = 0; $carry_shift = 8 - $shift; for ($i = 0; $i < strlen($x); ++$i) { $temp = (ord($x[$i]) >> $shift) | $carry; $carry = (ord($x[$i]) << $carry_shift) & 0xFF; $x[$i] = chr($temp); } $x = ltrim($x, chr(0)); $remainder = chr($carry >> $carry_shift) . $remainder; return ltrim($remainder, chr(0)); } // one quirk about how the following functions are implemented is that PHP defines N to be an unsigned long // at 32-bits, while java's longs are 64-bits. /** * Converts 32-bit integers to bytes. * * @param int $x * @return string * @access private */ function _int2bytes($x) { return ltrim(pack('N', $x), chr(0)); } /** * Converts bytes to 32-bit integers * * @param string $x * @return int * @access private */ function _bytes2int($x) { $temp = unpack('Nint', str_pad($x, 4, chr(0), STR_PAD_LEFT)); return $temp['int']; } /** * DER-encode an integer * * The ability to DER-encode integers is needed to create RSA public keys for use with OpenSSL * * @see self::modPow() * @access private * @param int $length * @return string */ function _encodeASN1Length($length) { if ($length <= 0x7F) { return chr($length); } $temp = ltrim(pack('N', $length), chr(0)); return pack('Ca*', 0x80 | strlen($temp), $temp); } /** * Single digit division * * Even if int64 is being used the division operator will return a float64 value * if the dividend is not evenly divisible by the divisor. Since a float64 doesn't * have the precision of int64 this is a problem so, when int64 is being used, * we'll guarantee that the dividend is divisible by first subtracting the remainder. * * @access private * @param int $x * @param int $y * @return int */ function _safe_divide($x, $y) { if (self::$base === 26) { return (int) ($x / $y); } // self::$base === 31 return ($x - ($x % $y)) / $y; } } # minimalist openssl.cnf file for use with phpseclib HOME = . RANDFILE = $ENV::HOME/.rnd [ v3_ca ] <?php /** * Pure-PHP ssh-agent client. * * PHP version 5 * * Here are some examples of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $agent = new \phpseclib\System\SSH\Agent(); * * $ssh = new \phpseclib\Net\SSH2('www.domain.tld'); * if (!$ssh->login('username', $agent)) { * exit('Login Failed'); * } * * echo $ssh->exec('pwd'); * echo $ssh->exec('ls -la'); * ?> * </code> * * @category System * @package SSH\Agent * @author Jim Wigginton <terrafrost@php.net> * @copyright 2014 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net * @internal See http://api.libssh.org/rfc/PROTOCOL.agent */ namespace phpseclib\System\SSH; use phpseclib\Crypt\RSA; use phpseclib\System\SSH\Agent\Identity; /** * Pure-PHP ssh-agent client identity factory * * requestIdentities() method pumps out \phpseclib\System\SSH\Agent\Identity objects * * @package SSH\Agent * @author Jim Wigginton <terrafrost@php.net> * @access public */ class Agent { /**#@+ * Message numbers * * @access private */ // to request SSH1 keys you have to use SSH_AGENTC_REQUEST_RSA_IDENTITIES (1) const SSH_AGENTC_REQUEST_IDENTITIES = 11; // this is the SSH2 response; the SSH1 response is SSH_AGENT_RSA_IDENTITIES_ANSWER (2). const SSH_AGENT_IDENTITIES_ANSWER = 12; // the SSH1 request is SSH_AGENTC_RSA_CHALLENGE (3) const SSH_AGENTC_SIGN_REQUEST = 13; // the SSH1 response is SSH_AGENT_RSA_RESPONSE (4) const SSH_AGENT_SIGN_RESPONSE = 14; /**#@-*/ /**@+ * Agent forwarding status * * @access private */ // no forwarding requested and not active const FORWARD_NONE = 0; // request agent forwarding when opportune const FORWARD_REQUEST = 1; // forwarding has been request and is active const FORWARD_ACTIVE = 2; /**#@-*/ /** * Unused */ const SSH_AGENT_FAILURE = 5; /** * Socket Resource * * @var resource * @access private */ var $fsock; /** * Agent forwarding status * * @access private */ var $forward_status = self::FORWARD_NONE; /** * Buffer for accumulating forwarded authentication * agent data arriving on SSH data channel destined * for agent unix socket * * @access private */ var $socket_buffer = ''; /** * Tracking the number of bytes we are expecting * to arrive for the agent socket on the SSH data * channel */ var $expected_bytes = 0; /** * Default Constructor * * @return \phpseclib\System\SSH\Agent * @access public */ function __construct($address = null) { if (!$address) { switch (true) { case isset($_SERVER['SSH_AUTH_SOCK']): $address = $_SERVER['SSH_AUTH_SOCK']; break; case isset($_ENV['SSH_AUTH_SOCK']): $address = $_ENV['SSH_AUTH_SOCK']; break; default: user_error('SSH_AUTH_SOCK not found'); return false; } } $this->fsock = fsockopen('unix://' . $address, 0, $errno, $errstr); if (!$this->fsock) { user_error("Unable to connect to ssh-agent (Error $errno: $errstr)"); } } /** * Request Identities * * See "2.5.2 Requesting a list of protocol 2 keys" * Returns an array containing zero or more \phpseclib\System\SSH\Agent\Identity objects * * @return array * @access public */ function requestIdentities() { if (!$this->fsock) { return array(); } $packet = pack('NC', 1, self::SSH_AGENTC_REQUEST_IDENTITIES); if (strlen($packet) != fputs($this->fsock, $packet)) { user_error('Connection closed while requesting identities'); return array(); } $length = current(unpack('N', fread($this->fsock, 4))); $type = ord(fread($this->fsock, 1)); if ($type != self::SSH_AGENT_IDENTITIES_ANSWER) { user_error('Unable to request identities'); return array(); } $identities = array(); $keyCount = current(unpack('N', fread($this->fsock, 4))); for ($i = 0; $i < $keyCount; $i++) { $length = current(unpack('N', fread($this->fsock, 4))); $key_blob = fread($this->fsock, $length); $key_str = 'ssh-rsa ' . base64_encode($key_blob); $length = current(unpack('N', fread($this->fsock, 4))); if ($length) { $key_str.= ' ' . fread($this->fsock, $length); } $length = current(unpack('N', substr($key_blob, 0, 4))); $key_type = substr($key_blob, 4, $length); switch ($key_type) { case 'ssh-rsa': $key = new RSA(); $key->loadKey($key_str); break; case 'ssh-dss': // not currently supported break; } // resources are passed by reference by default if (isset($key)) { $identity = new Identity($this->fsock); $identity->setPublicKey($key); $identity->setPublicKeyBlob($key_blob); $identities[] = $identity; unset($key); } } return $identities; } /** * Signal that agent forwarding should * be requested when a channel is opened * * @param Net_SSH2 $ssh * @return bool * @access public */ function startSSHForwarding($ssh) { if ($this->forward_status == self::FORWARD_NONE) { $this->forward_status = self::FORWARD_REQUEST; } } /** * Request agent forwarding of remote server * * @param Net_SSH2 $ssh * @return bool * @access private */ function _request_forwarding($ssh) { $request_channel = $ssh->_get_open_channel(); if ($request_channel === false) { return false; } $packet = pack( 'CNNa*C', NET_SSH2_MSG_CHANNEL_REQUEST, $ssh->server_channels[$request_channel], strlen('auth-agent-req@openssh.com'), 'auth-agent-req@openssh.com', 1 ); $ssh->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_REQUEST; if (!$ssh->_send_binary_packet($packet)) { return false; } $response = $ssh->_get_channel_packet($request_channel); if ($response === false) { return false; } $ssh->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_OPEN; $this->forward_status = self::FORWARD_ACTIVE; return true; } /** * On successful channel open * * This method is called upon successful channel * open to give the SSH Agent an opportunity * to take further action. i.e. request agent forwarding * * @param Net_SSH2 $ssh * @access private */ function _on_channel_open($ssh) { if ($this->forward_status == self::FORWARD_REQUEST) { $this->_request_forwarding($ssh); } } /** * Forward data to SSH Agent and return data reply * * @param string $data * @return data from SSH Agent * @access private */ function _forward_data($data) { if ($this->expected_bytes > 0) { $this->socket_buffer.= $data; $this->expected_bytes -= strlen($data); } else { $agent_data_bytes = current(unpack('N', $data)); $current_data_bytes = strlen($data); $this->socket_buffer = $data; if ($current_data_bytes != $agent_data_bytes + 4) { $this->expected_bytes = ($agent_data_bytes + 4) - $current_data_bytes; return false; } } if (strlen($this->socket_buffer) != fwrite($this->fsock, $this->socket_buffer)) { user_error('Connection closed attempting to forward data to SSH agent'); } $this->socket_buffer = ''; $this->expected_bytes = 0; $agent_reply_bytes = current(unpack('N', fread($this->fsock, 4))); $agent_reply_data = fread($this->fsock, $agent_reply_bytes); $agent_reply_data = current(unpack('a*', $agent_reply_data)); return pack('Na*', $agent_reply_bytes, $agent_reply_data); } } <?php /** * Pure-PHP ssh-agent client. * * PHP version 5 * * @category System * @package SSH\Agent * @author Jim Wigginton <terrafrost@php.net> * @copyright 2009 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net * @internal See http://api.libssh.org/rfc/PROTOCOL.agent */ namespace phpseclib\System\SSH\Agent; use phpseclib\System\SSH\Agent; /** * Pure-PHP ssh-agent client identity object * * Instantiation should only be performed by \phpseclib\System\SSH\Agent class. * This could be thought of as implementing an interface that phpseclib\Crypt\RSA * implements. ie. maybe a Net_SSH_Auth_PublicKey interface or something. * The methods in this interface would be getPublicKey and sign since those are the * methods phpseclib looks for to perform public key authentication. * * @package SSH\Agent * @author Jim Wigginton <terrafrost@php.net> * @access internal */ class Identity { /**@+ * Signature Flags * * See https://tools.ietf.org/html/draft-miller-ssh-agent-00#section-5.3 * * @access private */ const SSH_AGENT_RSA2_256 = 2; const SSH_AGENT_RSA2_512 = 4; /**#@-*/ /** * Key Object * * @var \phpseclib\Crypt\RSA * @access private * @see self::getPublicKey() */ var $key; /** * Key Blob * * @var string * @access private * @see self::sign() */ var $key_blob; /** * Socket Resource * * @var resource * @access private * @see self::sign() */ var $fsock; /** * Signature flags * * @var int * @access private * @see self::sign() * @see self::setHash() */ var $flags = 0; /** * Default Constructor. * * @param resource $fsock * @return \phpseclib\System\SSH\Agent\Identity * @access private */ function __construct($fsock) { $this->fsock = $fsock; } /** * Set Public Key * * Called by \phpseclib\System\SSH\Agent::requestIdentities() * * @param \phpseclib\Crypt\RSA $key * @access private */ function setPublicKey($key) { $this->key = $key; $this->key->setPublicKey(); } /** * Set Public Key * * Called by \phpseclib\System\SSH\Agent::requestIdentities(). The key blob could be extracted from $this->key * but this saves a small amount of computation. * * @param string $key_blob * @access private */ function setPublicKeyBlob($key_blob) { $this->key_blob = $key_blob; } /** * Get Public Key * * Wrapper for $this->key->getPublicKey() * * @param int $format optional * @return mixed * @access public */ function getPublicKey($format = null) { return !isset($format) ? $this->key->getPublicKey() : $this->key->getPublicKey($format); } /** * Set Signature Mode * * Doesn't do anything as ssh-agent doesn't let you pick and choose the signature mode. ie. * ssh-agent's only supported mode is \phpseclib\Crypt\RSA::SIGNATURE_PKCS1 * * @param int $mode * @access public */ function setSignatureMode($mode) { } /** * Set Hash * * ssh-agent doesn't support using hashes for RSA other than SHA1 * * @param string $hash * @access public */ function setHash($hash) { $this->flags = 0; switch ($hash) { case 'sha1': break; case 'sha256': $this->flags = self::SSH_AGENT_RSA2_256; break; case 'sha512': $this->flags = self::SSH_AGENT_RSA2_512; break; default: user_error('The only supported hashes for RSA are sha1, sha256 and sha512'); } } /** * Create a signature * * See "2.6.2 Protocol 2 private key signature request" * * @param string $message * @return string * @access public */ function sign($message) { // the last parameter (currently 0) is for flags and ssh-agent only defines one flag (for ssh-dss): SSH_AGENT_OLD_SIGNATURE $packet = pack('CNa*Na*N', Agent::SSH_AGENTC_SIGN_REQUEST, strlen($this->key_blob), $this->key_blob, strlen($message), $message, $this->flags); $packet = pack('Na*', strlen($packet), $packet); if (strlen($packet) != fputs($this->fsock, $packet)) { user_error('Connection closed during signing'); } $length = current(unpack('N', fread($this->fsock, 4))); $type = ord(fread($this->fsock, 1)); if ($type != Agent::SSH_AGENT_SIGN_RESPONSE) { user_error('Unable to retrieve signature'); } $signature_blob = fread($this->fsock, $length - 1); $length = current(unpack('N', $this->_string_shift($signature_blob, 4))); if ($length != strlen($signature_blob)) { user_error('Malformed signature blob'); } $length = current(unpack('N', $this->_string_shift($signature_blob, 4))); if ($length > strlen($signature_blob) + 4) { user_error('Malformed signature blob'); } $type = $this->_string_shift($signature_blob, $length); $this->_string_shift($signature_blob, 4); return $signature_blob; } /** * String Shift * * Inspired by array_shift * * @param string $string * @param int $index * @return string * @access private */ function _string_shift(&$string, $index = 1) { $substr = substr($string, 0, $index); $string = substr($string, $index); return $substr; } } <?php /** * Bootstrapping File for phpseclib * * @license http://www.opensource.org/licenses/mit-license.html MIT License */ if (extension_loaded('mbstring')) { // 2 - MB_OVERLOAD_STRING if (ini_get('mbstring.func_overload') & 2) { throw new \UnexpectedValueException( 'Overloading of string functions using mbstring.func_overload ' . 'is not supported by phpseclib.' ); } } <?php /** * Pure-PHP implementation of Triple DES. * * Uses mcrypt, if available, and an internal implementation, otherwise. Operates in the EDE3 mode (encrypt-decrypt-encrypt). * * PHP version 5 * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $des = new \phpseclib\Crypt\TripleDES(); * * $des->setKey('abcdefghijklmnopqrstuvwx'); * * $size = 10 * 1024; * $plaintext = ''; * for ($i = 0; $i < $size; $i++) { * $plaintext.= 'a'; * } * * echo $des->decrypt($des->encrypt($plaintext)); * ?> * </code> * * @category Crypt * @package TripleDES * @author Jim Wigginton <terrafrost@php.net> * @copyright 2007 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; /** * Pure-PHP implementation of Triple DES. * * @package TripleDES * @author Jim Wigginton <terrafrost@php.net> * @access public */ class TripleDES extends DES { /** * Encrypt / decrypt using inner chaining * * Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (self::MODE_CBC3). */ const MODE_3CBC = -2; /** * Encrypt / decrypt using outer chaining * * Outer chaining is used by SSH-2 and when the mode is set to \phpseclib\Crypt\Base::MODE_CBC. */ const MODE_CBC3 = Base::MODE_CBC; /** * Key Length (in bytes) * * @see \phpseclib\Crypt\TripleDES::setKeyLength() * @var int * @access private */ var $key_length = 24; /** * The default salt used by setPassword() * * @see \phpseclib\Crypt\Base::password_default_salt * @see \phpseclib\Crypt\Base::setPassword() * @var string * @access private */ var $password_default_salt = 'phpseclib'; /** * The mcrypt specific name of the cipher * * @see \phpseclib\Crypt\DES::cipher_name_mcrypt * @see \phpseclib\Crypt\Base::cipher_name_mcrypt * @var string * @access private */ var $cipher_name_mcrypt = 'tripledes'; /** * Optimizing value while CFB-encrypting * * @see \phpseclib\Crypt\Base::cfb_init_len * @var int * @access private */ var $cfb_init_len = 750; /** * max possible size of $key * * @see self::setKey() * @see \phpseclib\Crypt\DES::setKey() * @var string * @access private */ var $key_length_max = 24; /** * Internal flag whether using self::MODE_3CBC or not * * @var bool * @access private */ var $mode_3cbc; /** * The \phpseclib\Crypt\DES objects * * Used only if $mode_3cbc === true * * @var array * @access private */ var $des; /** * Default Constructor. * * Determines whether or not the mcrypt extension should be used. * * $mode could be: * * - \phpseclib\Crypt\Base::MODE_ECB * * - \phpseclib\Crypt\Base::MODE_CBC * * - \phpseclib\Crypt\Base::MODE_CTR * * - \phpseclib\Crypt\Base::MODE_CFB * * - \phpseclib\Crypt\Base::MODE_OFB * * - \phpseclib\Crypt\TripleDES::MODE_3CBC * * If not explicitly set, \phpseclib\Crypt\Base::MODE_CBC will be used. * * @see \phpseclib\Crypt\DES::__construct() * @see \phpseclib\Crypt\Base::__construct() * @param int $mode * @access public */ function __construct($mode = Base::MODE_CBC) { switch ($mode) { // In case of self::MODE_3CBC, we init as CRYPT_DES_MODE_CBC // and additional flag us internally as 3CBC case self::MODE_3CBC: parent::__construct(Base::MODE_CBC); $this->mode_3cbc = true; // This three $des'es will do the 3CBC work (if $key > 64bits) $this->des = array( new DES(Base::MODE_CBC), new DES(Base::MODE_CBC), new DES(Base::MODE_CBC), ); // we're going to be doing the padding, ourselves, so disable it in the \phpseclib\Crypt\DES objects $this->des[0]->disablePadding(); $this->des[1]->disablePadding(); $this->des[2]->disablePadding(); break; // If not 3CBC, we init as usual default: parent::__construct($mode); } } /** * Test for engine validity * * This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine() * * @see \phpseclib\Crypt\Base::__construct() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { if ($engine == self::ENGINE_OPENSSL) { $this->cipher_name_openssl_ecb = 'des-ede3'; $mode = $this->_openssl_translate_mode(); $this->cipher_name_openssl = $mode == 'ecb' ? 'des-ede3' : 'des-ede3-' . $mode; } return parent::isValidEngine($engine); } /** * Sets the initialization vector. (optional) * * SetIV is not required when \phpseclib\Crypt\Base::MODE_ECB is being used. If not explicitly set, it'll be assumed * to be all zero's. * * @see \phpseclib\Crypt\Base::setIV() * @access public * @param string $iv */ function setIV($iv) { parent::setIV($iv); if ($this->mode_3cbc) { $this->des[0]->setIV($iv); $this->des[1]->setIV($iv); $this->des[2]->setIV($iv); } } /** * Sets the key length. * * Valid key lengths are 64, 128 and 192 * * @see \phpseclib\Crypt\Base:setKeyLength() * @access public * @param int $length */ function setKeyLength($length) { $length >>= 3; switch (true) { case $length <= 8: $this->key_length = 8; break; case $length <= 16: $this->key_length = 16; break; default: $this->key_length = 24; } parent::setKeyLength($length); } /** * Sets the key. * * Keys can be of any length. Triple DES, itself, can use 128-bit (eg. strlen($key) == 16) or * 192-bit (eg. strlen($key) == 24) keys. This function pads and truncates $key as appropriate. * * DES also requires that every eighth bit be a parity bit, however, we'll ignore that. * * If the key is not explicitly set, it'll be assumed to be all null bytes. * * @access public * @see \phpseclib\Crypt\DES::setKey() * @see \phpseclib\Crypt\Base::setKey() * @param string $key */ function setKey($key) { $length = $this->explicit_key_length ? $this->key_length : strlen($key); if ($length > 8) { $key = str_pad(substr($key, 0, 24), 24, chr(0)); // if $key is between 64 and 128-bits, use the first 64-bits as the last, per this: // http://php.net/function.mcrypt-encrypt#47973 $key = $length <= 16 ? substr_replace($key, substr($key, 0, 8), 16) : substr($key, 0, 24); } else { $key = str_pad($key, 8, chr(0)); } parent::setKey($key); // And in case of self::MODE_3CBC: // if key <= 64bits we not need the 3 $des to work, // because we will then act as regular DES-CBC with just a <= 64bit key. // So only if the key > 64bits (> 8 bytes) we will call setKey() for the 3 $des. if ($this->mode_3cbc && $length > 8) { $this->des[0]->setKey(substr($key, 0, 8)); $this->des[1]->setKey(substr($key, 8, 8)); $this->des[2]->setKey(substr($key, 16, 8)); } } /** * Encrypts a message. * * @see \phpseclib\Crypt\Base::encrypt() * @access public * @param string $plaintext * @return string $cipertext */ function encrypt($plaintext) { // parent::en/decrypt() is able to do all the work for all modes and keylengths, // except for: self::MODE_3CBC (inner chaining CBC) with a key > 64bits // if the key is smaller then 8, do what we'd normally do if ($this->mode_3cbc && strlen($this->key) > 8) { return $this->des[2]->encrypt( $this->des[1]->decrypt( $this->des[0]->encrypt( $this->_pad($plaintext) ) ) ); } return parent::encrypt($plaintext); } /** * Decrypts a message. * * @see \phpseclib\Crypt\Base::decrypt() * @access public * @param string $ciphertext * @return string $plaintext */ function decrypt($ciphertext) { if ($this->mode_3cbc && strlen($this->key) > 8) { return $this->_unpad( $this->des[0]->decrypt( $this->des[1]->encrypt( $this->des[2]->decrypt( str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, "\0") ) ) ) ); } return parent::decrypt($ciphertext); } /** * Treat consecutive "packets" as if they are a continuous buffer. * * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets * will yield different outputs: * * <code> * echo $des->encrypt(substr($plaintext, 0, 8)); * echo $des->encrypt(substr($plaintext, 8, 8)); * </code> * <code> * echo $des->encrypt($plaintext); * </code> * * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates * another, as demonstrated with the following: * * <code> * $des->encrypt(substr($plaintext, 0, 8)); * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8))); * </code> * <code> * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8))); * </code> * * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different * outputs. The reason is due to the fact that the initialization vector's change after every encryption / * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant. * * Put another way, when the continuous buffer is enabled, the state of the \phpseclib\Crypt\DES() object changes after each * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them), * however, they are also less intuitive and more likely to cause you problems. * * @see \phpseclib\Crypt\Base::enableContinuousBuffer() * @see self::disableContinuousBuffer() * @access public */ function enableContinuousBuffer() { parent::enableContinuousBuffer(); if ($this->mode_3cbc) { $this->des[0]->enableContinuousBuffer(); $this->des[1]->enableContinuousBuffer(); $this->des[2]->enableContinuousBuffer(); } } /** * Treat consecutive packets as if they are a discontinuous buffer. * * The default behavior. * * @see \phpseclib\Crypt\Base::disableContinuousBuffer() * @see self::enableContinuousBuffer() * @access public */ function disableContinuousBuffer() { parent::disableContinuousBuffer(); if ($this->mode_3cbc) { $this->des[0]->disableContinuousBuffer(); $this->des[1]->disableContinuousBuffer(); $this->des[2]->disableContinuousBuffer(); } } /** * Creates the key schedule * * @see \phpseclib\Crypt\DES::_setupKey() * @see \phpseclib\Crypt\Base::_setupKey() * @access private */ function _setupKey() { switch (true) { // if $key <= 64bits we configure our internal pure-php cipher engine // to act as regular [1]DES, not as 3DES. mcrypt.so::tripledes does the same. case strlen($this->key) <= 8: $this->des_rounds = 1; break; // otherwise, if $key > 64bits, we configure our engine to work as 3DES. default: $this->des_rounds = 3; // (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately. if ($this->mode_3cbc) { $this->des[0]->_setupKey(); $this->des[1]->_setupKey(); $this->des[2]->_setupKey(); // because $des[0-2] will, now, do all the work we can return here // not need unnecessary stress parent::_setupKey() with our, now unused, $key. return; } } // setup our key parent::_setupKey(); } /** * Sets the internal crypt engine * * @see \phpseclib\Crypt\Base::__construct() * @see \phpseclib\Crypt\Base::setPreferredEngine() * @param int $engine * @access public * @return int */ function setPreferredEngine($engine) { if ($this->mode_3cbc) { $this->des[0]->setPreferredEngine($engine); $this->des[1]->setPreferredEngine($engine); $this->des[2]->setPreferredEngine($engine); } return parent::setPreferredEngine($engine); } } <?php /** * Base Class for all \phpseclib\Crypt\* cipher classes * * PHP version 5 * * Internally for phpseclib developers: * If you plan to add a new cipher class, please note following rules: * * - The new \phpseclib\Crypt\* cipher class should extend \phpseclib\Crypt\Base * * - Following methods are then required to be overridden/overloaded: * * - _encryptBlock() * * - _decryptBlock() * * - _setupKey() * * - All other methods are optional to be overridden/overloaded * * - Look at the source code of the current ciphers how they extend \phpseclib\Crypt\Base * and take one of them as a start up for the new cipher class. * * - Please read all the other comments/notes/hints here also for each class var/method * * @category Crypt * @package Base * @author Jim Wigginton <terrafrost@php.net> * @author Hans-Juergen Petrich <petrich@tronic-media.com> * @copyright 2007 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; /** * Base Class for all \phpseclib\Crypt\* cipher classes * * @package Base * @author Jim Wigginton <terrafrost@php.net> * @author Hans-Juergen Petrich <petrich@tronic-media.com> */ abstract class Base { /**#@+ * @access public * @see \phpseclib\Crypt\Base::encrypt() * @see \phpseclib\Crypt\Base::decrypt() */ /** * Encrypt / decrypt using the Counter mode. * * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode. * * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29 */ const MODE_CTR = -1; /** * Encrypt / decrypt using the Electronic Code Book mode. * * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29 */ const MODE_ECB = 1; /** * Encrypt / decrypt using the Code Book Chaining mode. * * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29 */ const MODE_CBC = 2; /** * Encrypt / decrypt using the Cipher Feedback mode. * * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29 */ const MODE_CFB = 3; /** * Encrypt / decrypt using the Cipher Feedback mode (8bit) */ const MODE_CFB8 = 38; /** * Encrypt / decrypt using the Output Feedback mode. * * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29 */ const MODE_OFB = 4; /** * Encrypt / decrypt using streaming mode. */ const MODE_STREAM = 5; /**#@-*/ /** * Whirlpool available flag * * @see \phpseclib\Crypt\Base::_hashInlineCryptFunction() * @var bool * @access private */ static $WHIRLPOOL_AVAILABLE; /**#@+ * @access private * @see \phpseclib\Crypt\Base::__construct() */ /** * Base value for the internal implementation $engine switch */ const ENGINE_INTERNAL = 1; /** * Base value for the mcrypt implementation $engine switch */ const ENGINE_MCRYPT = 2; /** * Base value for the mcrypt implementation $engine switch */ const ENGINE_OPENSSL = 3; /**#@-*/ /** * The Encryption Mode * * @see self::__construct() * @var int * @access private */ var $mode; /** * The Block Length of the block cipher * * @var int * @access private */ var $block_size = 16; /** * The Key * * @see self::setKey() * @var string * @access private */ var $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; /** * The Initialization Vector * * @see self::setIV() * @var string * @access private */ var $iv; /** * A "sliding" Initialization Vector * * @see self::enableContinuousBuffer() * @see self::_clearBuffers() * @var string * @access private */ var $encryptIV; /** * A "sliding" Initialization Vector * * @see self::enableContinuousBuffer() * @see self::_clearBuffers() * @var string * @access private */ var $decryptIV; /** * Continuous Buffer status * * @see self::enableContinuousBuffer() * @var bool * @access private */ var $continuousBuffer = false; /** * Encryption buffer for CTR, OFB and CFB modes * * @see self::encrypt() * @see self::_clearBuffers() * @var array * @access private */ var $enbuffer; /** * Decryption buffer for CTR, OFB and CFB modes * * @see self::decrypt() * @see self::_clearBuffers() * @var array * @access private */ var $debuffer; /** * mcrypt resource for encryption * * The mcrypt resource can be recreated every time something needs to be created or it can be created just once. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode. * * @see self::encrypt() * @var resource * @access private */ var $enmcrypt; /** * mcrypt resource for decryption * * The mcrypt resource can be recreated every time something needs to be created or it can be created just once. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode. * * @see self::decrypt() * @var resource * @access private */ var $demcrypt; /** * Does the enmcrypt resource need to be (re)initialized? * * @see \phpseclib\Crypt\Twofish::setKey() * @see \phpseclib\Crypt\Twofish::setIV() * @var bool * @access private */ var $enchanged = true; /** * Does the demcrypt resource need to be (re)initialized? * * @see \phpseclib\Crypt\Twofish::setKey() * @see \phpseclib\Crypt\Twofish::setIV() * @var bool * @access private */ var $dechanged = true; /** * mcrypt resource for CFB mode * * mcrypt's CFB mode, in (and only in) buffered context, * is broken, so phpseclib implements the CFB mode by it self, * even when the mcrypt php extension is available. * * In order to do the CFB-mode work (fast) phpseclib * use a separate ECB-mode mcrypt resource. * * @link http://phpseclib.sourceforge.net/cfb-demo.phps * @see self::encrypt() * @see self::decrypt() * @see self::_setupMcrypt() * @var resource * @access private */ var $ecb; /** * Optimizing value while CFB-encrypting * * Only relevant if $continuousBuffer enabled * and $engine == self::ENGINE_MCRYPT * * It's faster to re-init $enmcrypt if * $buffer bytes > $cfb_init_len than * using the $ecb resource furthermore. * * This value depends of the chosen cipher * and the time it would be needed for it's * initialization [by mcrypt_generic_init()] * which, typically, depends on the complexity * on its internaly Key-expanding algorithm. * * @see self::encrypt() * @var int * @access private */ var $cfb_init_len = 600; /** * Does internal cipher state need to be (re)initialized? * * @see self::setKey() * @see self::setIV() * @see self::disableContinuousBuffer() * @var bool * @access private */ var $changed = true; /** * Padding status * * @see self::enablePadding() * @var bool * @access private */ var $padding = true; /** * Is the mode one that is paddable? * * @see self::__construct() * @var bool * @access private */ var $paddable = false; /** * Holds which crypt engine internaly should be use, * which will be determined automatically on __construct() * * Currently available $engines are: * - self::ENGINE_OPENSSL (very fast, php-extension: openssl, extension_loaded('openssl') required) * - self::ENGINE_MCRYPT (fast, php-extension: mcrypt, extension_loaded('mcrypt') required) * - self::ENGINE_INTERNAL (slower, pure php-engine, no php-extension required) * * @see self::_setEngine() * @see self::encrypt() * @see self::decrypt() * @var int * @access private */ var $engine; /** * Holds the preferred crypt engine * * @see self::_setEngine() * @see self::setPreferredEngine() * @var int * @access private */ var $preferredEngine; /** * The mcrypt specific name of the cipher * * Only used if $engine == self::ENGINE_MCRYPT * * @link http://www.php.net/mcrypt_module_open * @link http://www.php.net/mcrypt_list_algorithms * @see self::_setupMcrypt() * @var string * @access private */ var $cipher_name_mcrypt; /** * The openssl specific name of the cipher * * Only used if $engine == self::ENGINE_OPENSSL * * @link http://www.php.net/openssl-get-cipher-methods * @var string * @access private */ var $cipher_name_openssl; /** * The openssl specific name of the cipher in ECB mode * * If OpenSSL does not support the mode we're trying to use (CTR) * it can still be emulated with ECB mode. * * @link http://www.php.net/openssl-get-cipher-methods * @var string * @access private */ var $cipher_name_openssl_ecb; /** * The default salt used by setPassword() * * @see self::setPassword() * @var string * @access private */ var $password_default_salt = 'phpseclib/salt'; /** * The name of the performance-optimized callback function * * Used by encrypt() / decrypt() * only if $engine == self::ENGINE_INTERNAL * * @see self::encrypt() * @see self::decrypt() * @see self::_setupInlineCrypt() * @see self::$use_inline_crypt * @var Callback * @access private */ var $inline_crypt; /** * Holds whether performance-optimized $inline_crypt() can/should be used. * * @see self::encrypt() * @see self::decrypt() * @see self::inline_crypt * @var mixed * @access private */ var $use_inline_crypt; /** * If OpenSSL can be used in ECB but not in CTR we can emulate CTR * * @see self::_openssl_ctr_process() * @var bool * @access private */ var $openssl_emulate_ctr = false; /** * Determines what options are passed to openssl_encrypt/decrypt * * @see self::isValidEngine() * @var mixed * @access private */ var $openssl_options; /** * Has the key length explicitly been set or should it be derived from the key, itself? * * @see self::setKeyLength() * @var bool * @access private */ var $explicit_key_length = false; /** * Don't truncate / null pad key * * @see self::_clearBuffers() * @var bool * @access private */ var $skip_key_adjustment = false; /** * Default Constructor. * * Determines whether or not the mcrypt extension should be used. * * $mode could be: * * - self::MODE_ECB * * - self::MODE_CBC * * - self::MODE_CTR * * - self::MODE_CFB * * - self::MODE_OFB * * If not explicitly set, self::MODE_CBC will be used. * * @param int $mode * @access public */ function __construct($mode = self::MODE_CBC) { // $mode dependent settings switch ($mode) { case self::MODE_ECB: $this->paddable = true; $this->mode = self::MODE_ECB; break; case self::MODE_CTR: case self::MODE_CFB: case self::MODE_CFB8: case self::MODE_OFB: case self::MODE_STREAM: $this->mode = $mode; break; case self::MODE_CBC: default: $this->paddable = true; $this->mode = self::MODE_CBC; } $this->_setEngine(); // Determining whether inline crypting can be used by the cipher if ($this->use_inline_crypt !== false) { $this->use_inline_crypt = version_compare(PHP_VERSION, '5.3.0') >= 0 || function_exists('create_function'); } } /** * Sets the initialization vector. (optional) * * SetIV is not required when self::MODE_ECB (or ie for AES: \phpseclib\Crypt\AES::MODE_ECB) is being used. If not explicitly set, it'll be assumed * to be all zero's. * * @access public * @param string $iv * @internal Can be overwritten by a sub class, but does not have to be */ function setIV($iv) { if ($this->mode == self::MODE_ECB) { return; } $this->iv = $iv; $this->changed = true; } /** * Sets the key length. * * Keys with explicitly set lengths need to be treated accordingly * * @access public * @param int $length */ function setKeyLength($length) { $this->explicit_key_length = true; $this->changed = true; $this->_setEngine(); } /** * Returns the current key length in bits * * @access public * @return int */ function getKeyLength() { return $this->key_length << 3; } /** * Returns the current block length in bits * * @access public * @return int */ function getBlockLength() { return $this->block_size << 3; } /** * Sets the key. * * The min/max length(s) of the key depends on the cipher which is used. * If the key not fits the length(s) of the cipher it will paded with null bytes * up to the closest valid key length. If the key is more than max length, * we trim the excess bits. * * If the key is not explicitly set, it'll be assumed to be all null bytes. * * @access public * @param string $key * @internal Could, but not must, extend by the child Crypt_* class */ function setKey($key) { if (!$this->explicit_key_length) { $this->setKeyLength(strlen($key) << 3); $this->explicit_key_length = false; } $this->key = $key; $this->changed = true; $this->_setEngine(); } /** * Sets the password. * * Depending on what $method is set to, setPassword()'s (optional) parameters are as follows: * {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2} or pbkdf1: * $hash, $salt, $count, $dkLen * * Where $hash (default = sha1) currently supports the following hashes: see: Crypt/Hash.php * * @see Crypt/Hash.php * @param string $password * @param string $method * @return bool * @access public * @internal Could, but not must, extend by the child Crypt_* class */ function setPassword($password, $method = 'pbkdf2') { $key = ''; switch ($method) { default: // 'pbkdf2' or 'pbkdf1' $func_args = func_get_args(); // Hash function $hash = isset($func_args[2]) ? $func_args[2] : 'sha1'; // WPA and WPA2 use the SSID as the salt $salt = isset($func_args[3]) ? $func_args[3] : $this->password_default_salt; // RFC2898#section-4.2 uses 1,000 iterations by default // WPA and WPA2 use 4,096. $count = isset($func_args[4]) ? $func_args[4] : 1000; // Keylength if (isset($func_args[5])) { $dkLen = $func_args[5]; } else { $dkLen = $method == 'pbkdf1' ? 2 * $this->key_length : $this->key_length; } switch (true) { case $method == 'pbkdf1': $hashObj = new Hash(); $hashObj->setHash($hash); if ($dkLen > $hashObj->getLength()) { user_error('Derived key too long'); return false; } $t = $password . $salt; for ($i = 0; $i < $count; ++$i) { $t = $hashObj->hash($t); } $key = substr($t, 0, $dkLen); $this->setKey(substr($key, 0, $dkLen >> 1)); $this->setIV(substr($key, $dkLen >> 1)); return true; // Determining if php[>=5.5.0]'s hash_pbkdf2() function avail- and useable case !function_exists('hash_pbkdf2'): case !function_exists('hash_algos'): case !in_array($hash, hash_algos()): $i = 1; $hmac = new Hash(); $hmac->setHash($hash); $hmac->setKey($password); while (strlen($key) < $dkLen) { $f = $u = $hmac->hash($salt . pack('N', $i++)); for ($j = 2; $j <= $count; ++$j) { $u = $hmac->hash($u); $f^= $u; } $key.= $f; } $key = substr($key, 0, $dkLen); break; default: $key = hash_pbkdf2($hash, $password, $salt, $count, $dkLen, true); } } $this->setKey($key); return true; } /** * Encrypts a message. * * $plaintext will be padded with additional bytes such that it's length is a multiple of the block size. Other cipher * implementations may or may not pad in the same manner. Other common approaches to padding and the reasons why it's * necessary are discussed in the following * URL: * * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html} * * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does. * strlen($plaintext) will still need to be a multiple of the block size, however, arbitrary values can be added to make it that * length. * * @see self::decrypt() * @access public * @param string $plaintext * @return string $ciphertext * @internal Could, but not must, extend by the child Crypt_* class */ function encrypt($plaintext) { if ($this->paddable) { $plaintext = $this->_pad($plaintext); } if ($this->engine === self::ENGINE_OPENSSL) { if ($this->changed) { $this->_clearBuffers(); $this->changed = false; } switch ($this->mode) { case self::MODE_STREAM: return openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options); case self::MODE_ECB: $result = @openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options); return !defined('OPENSSL_RAW_DATA') ? substr($result, 0, -$this->block_size) : $result; case self::MODE_CBC: $result = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->encryptIV); if (!defined('OPENSSL_RAW_DATA')) { $result = substr($result, 0, -$this->block_size); } if ($this->continuousBuffer) { $this->encryptIV = substr($result, -$this->block_size); } return $result; case self::MODE_CTR: return $this->_openssl_ctr_process($plaintext, $this->encryptIV, $this->enbuffer); case self::MODE_CFB: // cfb loosely routines inspired by openssl's: // {@link http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1} $ciphertext = ''; if ($this->continuousBuffer) { $iv = &$this->encryptIV; $pos = &$this->enbuffer['pos']; } else { $iv = $this->encryptIV; $pos = 0; } $len = strlen($plaintext); $i = 0; if ($pos) { $orig_pos = $pos; $max = $this->block_size - $pos; if ($len >= $max) { $i = $max; $len-= $max; $pos = 0; } else { $i = $len; $pos+= $len; $len = 0; } // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize $ciphertext = substr($iv, $orig_pos) ^ $plaintext; $iv = substr_replace($iv, $ciphertext, $orig_pos, $i); $plaintext = substr($plaintext, $i); } $overflow = $len % $this->block_size; if ($overflow) { $ciphertext.= openssl_encrypt(substr($plaintext, 0, -$overflow) . str_repeat("\0", $this->block_size), $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv); $iv = $this->_string_pop($ciphertext, $this->block_size); $size = $len - $overflow; $block = $iv ^ substr($plaintext, -$overflow); $iv = substr_replace($iv, $block, 0, $overflow); $ciphertext.= $block; $pos = $overflow; } elseif ($len) { $ciphertext = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv); $iv = substr($ciphertext, -$this->block_size); } return $ciphertext; case self::MODE_CFB8: $ciphertext = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->encryptIV); if ($this->continuousBuffer) { if (($len = strlen($ciphertext)) >= $this->block_size) { $this->encryptIV = substr($ciphertext, -$this->block_size); } else { $this->encryptIV = substr($this->encryptIV, $len - $this->block_size) . substr($ciphertext, -$len); } } return $ciphertext; case self::MODE_OFB: return $this->_openssl_ofb_process($plaintext, $this->encryptIV, $this->enbuffer); } } if ($this->engine === self::ENGINE_MCRYPT) { if ($this->changed) { $this->_setupMcrypt(); $this->changed = false; } if ($this->enchanged) { @mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV); $this->enchanged = false; } // re: {@link http://phpseclib.sourceforge.net/cfb-demo.phps} // using mcrypt's default handing of CFB the above would output two different things. using phpseclib's // rewritten CFB implementation the above outputs the same thing twice. if ($this->mode == self::MODE_CFB && $this->continuousBuffer) { $block_size = $this->block_size; $iv = &$this->encryptIV; $pos = &$this->enbuffer['pos']; $len = strlen($plaintext); $ciphertext = ''; $i = 0; if ($pos) { $orig_pos = $pos; $max = $block_size - $pos; if ($len >= $max) { $i = $max; $len-= $max; $pos = 0; } else { $i = $len; $pos+= $len; $len = 0; } $ciphertext = substr($iv, $orig_pos) ^ $plaintext; $iv = substr_replace($iv, $ciphertext, $orig_pos, $i); $this->enbuffer['enmcrypt_init'] = true; } if ($len >= $block_size) { if ($this->enbuffer['enmcrypt_init'] === false || $len > $this->cfb_init_len) { if ($this->enbuffer['enmcrypt_init'] === true) { @mcrypt_generic_init($this->enmcrypt, $this->key, $iv); $this->enbuffer['enmcrypt_init'] = false; } $ciphertext.= @mcrypt_generic($this->enmcrypt, substr($plaintext, $i, $len - $len % $block_size)); $iv = substr($ciphertext, -$block_size); $len%= $block_size; } else { while ($len >= $block_size) { $iv = @mcrypt_generic($this->ecb, $iv) ^ substr($plaintext, $i, $block_size); $ciphertext.= $iv; $len-= $block_size; $i+= $block_size; } } } if ($len) { $iv = @mcrypt_generic($this->ecb, $iv); $block = $iv ^ substr($plaintext, -$len); $iv = substr_replace($iv, $block, 0, $len); $ciphertext.= $block; $pos = $len; } return $ciphertext; } $ciphertext = @mcrypt_generic($this->enmcrypt, $plaintext); if (!$this->continuousBuffer) { @mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV); } return $ciphertext; } if ($this->changed) { $this->_setup(); $this->changed = false; } if ($this->use_inline_crypt) { $inline = $this->inline_crypt; return $inline('encrypt', $this, $plaintext); } $buffer = &$this->enbuffer; $block_size = $this->block_size; $ciphertext = ''; switch ($this->mode) { case self::MODE_ECB: for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { $ciphertext.= $this->_encryptBlock(substr($plaintext, $i, $block_size)); } break; case self::MODE_CBC: $xor = $this->encryptIV; for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { $block = substr($plaintext, $i, $block_size); $block = $this->_encryptBlock($block ^ $xor); $xor = $block; $ciphertext.= $block; } if ($this->continuousBuffer) { $this->encryptIV = $xor; } break; case self::MODE_CTR: $xor = $this->encryptIV; if (strlen($buffer['ciphertext'])) { for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { $block = substr($plaintext, $i, $block_size); if (strlen($block) > strlen($buffer['ciphertext'])) { $buffer['ciphertext'].= $this->_encryptBlock($xor); } $this->_increment_str($xor); $key = $this->_string_shift($buffer['ciphertext'], $block_size); $ciphertext.= $block ^ $key; } } else { for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { $block = substr($plaintext, $i, $block_size); $key = $this->_encryptBlock($xor); $this->_increment_str($xor); $ciphertext.= $block ^ $key; } } if ($this->continuousBuffer) { $this->encryptIV = $xor; if ($start = strlen($plaintext) % $block_size) { $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext']; } } break; case self::MODE_CFB: // cfb loosely routines inspired by openssl's: // {@link http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1} if ($this->continuousBuffer) { $iv = &$this->encryptIV; $pos = &$buffer['pos']; } else { $iv = $this->encryptIV; $pos = 0; } $len = strlen($plaintext); $i = 0; if ($pos) { $orig_pos = $pos; $max = $block_size - $pos; if ($len >= $max) { $i = $max; $len-= $max; $pos = 0; } else { $i = $len; $pos+= $len; $len = 0; } // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize $ciphertext = substr($iv, $orig_pos) ^ $plaintext; $iv = substr_replace($iv, $ciphertext, $orig_pos, $i); } while ($len >= $block_size) { $iv = $this->_encryptBlock($iv) ^ substr($plaintext, $i, $block_size); $ciphertext.= $iv; $len-= $block_size; $i+= $block_size; } if ($len) { $iv = $this->_encryptBlock($iv); $block = $iv ^ substr($plaintext, $i); $iv = substr_replace($iv, $block, 0, $len); $ciphertext.= $block; $pos = $len; } break; case self::MODE_CFB8: $ciphertext = ''; $len = strlen($plaintext); $iv = $this->encryptIV; for ($i = 0; $i < $len; ++$i) { $ciphertext .= ($c = $plaintext[$i] ^ $this->_encryptBlock($iv)); $iv = substr($iv, 1) . $c; } if ($this->continuousBuffer) { if ($len >= $block_size) { $this->encryptIV = substr($ciphertext, -$block_size); } else { $this->encryptIV = substr($this->encryptIV, $len - $block_size) . substr($ciphertext, -$len); } } break; case self::MODE_OFB: $xor = $this->encryptIV; if (strlen($buffer['xor'])) { for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { $block = substr($plaintext, $i, $block_size); if (strlen($block) > strlen($buffer['xor'])) { $xor = $this->_encryptBlock($xor); $buffer['xor'].= $xor; } $key = $this->_string_shift($buffer['xor'], $block_size); $ciphertext.= $block ^ $key; } } else { for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { $xor = $this->_encryptBlock($xor); $ciphertext.= substr($plaintext, $i, $block_size) ^ $xor; } $key = $xor; } if ($this->continuousBuffer) { $this->encryptIV = $xor; if ($start = strlen($plaintext) % $block_size) { $buffer['xor'] = substr($key, $start) . $buffer['xor']; } } break; case self::MODE_STREAM: $ciphertext = $this->_encryptBlock($plaintext); break; } return $ciphertext; } /** * Decrypts a message. * * If strlen($ciphertext) is not a multiple of the block size, null bytes will be added to the end of the string until * it is. * * @see self::encrypt() * @access public * @param string $ciphertext * @return string $plaintext * @internal Could, but not must, extend by the child Crypt_* class */ function decrypt($ciphertext) { if ($this->paddable) { // we pad with chr(0) since that's what mcrypt_generic does. to quote from {@link http://www.php.net/function.mcrypt-generic}: // "The data is padded with "\0" to make sure the length of the data is n * blocksize." $ciphertext = str_pad($ciphertext, strlen($ciphertext) + ($this->block_size - strlen($ciphertext) % $this->block_size) % $this->block_size, chr(0)); } if ($this->engine === self::ENGINE_OPENSSL) { if ($this->changed) { $this->_clearBuffers(); $this->changed = false; } switch ($this->mode) { case self::MODE_STREAM: $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options); break; case self::MODE_ECB: if (!defined('OPENSSL_RAW_DATA')) { $ciphertext.= @openssl_encrypt('', $this->cipher_name_openssl_ecb, $this->key, true); } $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options); break; case self::MODE_CBC: if (!defined('OPENSSL_RAW_DATA')) { $padding = str_repeat(chr($this->block_size), $this->block_size) ^ substr($ciphertext, -$this->block_size); $ciphertext.= substr(@openssl_encrypt($padding, $this->cipher_name_openssl_ecb, $this->key, true), 0, $this->block_size); $offset = 2 * $this->block_size; } else { $offset = $this->block_size; } $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->decryptIV); if ($this->continuousBuffer) { $this->decryptIV = substr($ciphertext, -$offset, $this->block_size); } break; case self::MODE_CTR: $plaintext = $this->_openssl_ctr_process($ciphertext, $this->decryptIV, $this->debuffer); break; case self::MODE_CFB: // cfb loosely routines inspired by openssl's: // {@link http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1} $plaintext = ''; if ($this->continuousBuffer) { $iv = &$this->decryptIV; $pos = &$this->buffer['pos']; } else { $iv = $this->decryptIV; $pos = 0; } $len = strlen($ciphertext); $i = 0; if ($pos) { $orig_pos = $pos; $max = $this->block_size - $pos; if ($len >= $max) { $i = $max; $len-= $max; $pos = 0; } else { $i = $len; $pos+= $len; $len = 0; } // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $this->blocksize $plaintext = substr($iv, $orig_pos) ^ $ciphertext; $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i); $ciphertext = substr($ciphertext, $i); } $overflow = $len % $this->block_size; if ($overflow) { $plaintext.= openssl_decrypt(substr($ciphertext, 0, -$overflow), $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv); if ($len - $overflow) { $iv = substr($ciphertext, -$overflow - $this->block_size, -$overflow); } $iv = openssl_encrypt(str_repeat("\0", $this->block_size), $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv); $plaintext.= $iv ^ substr($ciphertext, -$overflow); $iv = substr_replace($iv, substr($ciphertext, -$overflow), 0, $overflow); $pos = $overflow; } elseif ($len) { $plaintext.= openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv); $iv = substr($ciphertext, -$this->block_size); } break; case self::MODE_CFB8: $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->decryptIV); if ($this->continuousBuffer) { if (($len = strlen($ciphertext)) >= $this->block_size) { $this->decryptIV = substr($ciphertext, -$this->block_size); } else { $this->decryptIV = substr($this->decryptIV, $len - $this->block_size) . substr($ciphertext, -$len); } } break; case self::MODE_OFB: $plaintext = $this->_openssl_ofb_process($ciphertext, $this->decryptIV, $this->debuffer); } return $this->paddable ? $this->_unpad($plaintext) : $plaintext; } if ($this->engine === self::ENGINE_MCRYPT) { $block_size = $this->block_size; if ($this->changed) { $this->_setupMcrypt(); $this->changed = false; } if ($this->dechanged) { @mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV); $this->dechanged = false; } if ($this->mode == self::MODE_CFB && $this->continuousBuffer) { $iv = &$this->decryptIV; $pos = &$this->debuffer['pos']; $len = strlen($ciphertext); $plaintext = ''; $i = 0; if ($pos) { $orig_pos = $pos; $max = $block_size - $pos; if ($len >= $max) { $i = $max; $len-= $max; $pos = 0; } else { $i = $len; $pos+= $len; $len = 0; } // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize $plaintext = substr($iv, $orig_pos) ^ $ciphertext; $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i); } if ($len >= $block_size) { $cb = substr($ciphertext, $i, $len - $len % $block_size); $plaintext.= @mcrypt_generic($this->ecb, $iv . $cb) ^ $cb; $iv = substr($cb, -$block_size); $len%= $block_size; } if ($len) { $iv = @mcrypt_generic($this->ecb, $iv); $plaintext.= $iv ^ substr($ciphertext, -$len); $iv = substr_replace($iv, substr($ciphertext, -$len), 0, $len); $pos = $len; } return $plaintext; } $plaintext = @mdecrypt_generic($this->demcrypt, $ciphertext); if (!$this->continuousBuffer) { @mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV); } return $this->paddable ? $this->_unpad($plaintext) : $plaintext; } if ($this->changed) { $this->_setup(); $this->changed = false; } if ($this->use_inline_crypt) { $inline = $this->inline_crypt; return $inline('decrypt', $this, $ciphertext); } $block_size = $this->block_size; $buffer = &$this->debuffer; $plaintext = ''; switch ($this->mode) { case self::MODE_ECB: for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { $plaintext.= $this->_decryptBlock(substr($ciphertext, $i, $block_size)); } break; case self::MODE_CBC: $xor = $this->decryptIV; for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { $block = substr($ciphertext, $i, $block_size); $plaintext.= $this->_decryptBlock($block) ^ $xor; $xor = $block; } if ($this->continuousBuffer) { $this->decryptIV = $xor; } break; case self::MODE_CTR: $xor = $this->decryptIV; if (strlen($buffer['ciphertext'])) { for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { $block = substr($ciphertext, $i, $block_size); if (strlen($block) > strlen($buffer['ciphertext'])) { $buffer['ciphertext'].= $this->_encryptBlock($xor); $this->_increment_str($xor); } $key = $this->_string_shift($buffer['ciphertext'], $block_size); $plaintext.= $block ^ $key; } } else { for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { $block = substr($ciphertext, $i, $block_size); $key = $this->_encryptBlock($xor); $this->_increment_str($xor); $plaintext.= $block ^ $key; } } if ($this->continuousBuffer) { $this->decryptIV = $xor; if ($start = strlen($ciphertext) % $block_size) { $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext']; } } break; case self::MODE_CFB: if ($this->continuousBuffer) { $iv = &$this->decryptIV; $pos = &$buffer['pos']; } else { $iv = $this->decryptIV; $pos = 0; } $len = strlen($ciphertext); $i = 0; if ($pos) { $orig_pos = $pos; $max = $block_size - $pos; if ($len >= $max) { $i = $max; $len-= $max; $pos = 0; } else { $i = $len; $pos+= $len; $len = 0; } // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize $plaintext = substr($iv, $orig_pos) ^ $ciphertext; $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i); } while ($len >= $block_size) { $iv = $this->_encryptBlock($iv); $cb = substr($ciphertext, $i, $block_size); $plaintext.= $iv ^ $cb; $iv = $cb; $len-= $block_size; $i+= $block_size; } if ($len) { $iv = $this->_encryptBlock($iv); $plaintext.= $iv ^ substr($ciphertext, $i); $iv = substr_replace($iv, substr($ciphertext, $i), 0, $len); $pos = $len; } break; case self::MODE_CFB8: $plaintext = ''; $len = strlen($ciphertext); $iv = $this->decryptIV; for ($i = 0; $i < $len; ++$i) { $plaintext .= $ciphertext[$i] ^ $this->_encryptBlock($iv); $iv = substr($iv, 1) . $ciphertext[$i]; } if ($this->continuousBuffer) { if ($len >= $block_size) { $this->decryptIV = substr($ciphertext, -$block_size); } else { $this->decryptIV = substr($this->decryptIV, $len - $block_size) . substr($ciphertext, -$len); } } break; case self::MODE_OFB: $xor = $this->decryptIV; if (strlen($buffer['xor'])) { for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { $block = substr($ciphertext, $i, $block_size); if (strlen($block) > strlen($buffer['xor'])) { $xor = $this->_encryptBlock($xor); $buffer['xor'].= $xor; } $key = $this->_string_shift($buffer['xor'], $block_size); $plaintext.= $block ^ $key; } } else { for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { $xor = $this->_encryptBlock($xor); $plaintext.= substr($ciphertext, $i, $block_size) ^ $xor; } $key = $xor; } if ($this->continuousBuffer) { $this->decryptIV = $xor; if ($start = strlen($ciphertext) % $block_size) { $buffer['xor'] = substr($key, $start) . $buffer['xor']; } } break; case self::MODE_STREAM: $plaintext = $this->_decryptBlock($ciphertext); break; } return $this->paddable ? $this->_unpad($plaintext) : $plaintext; } /** * OpenSSL CTR Processor * * PHP's OpenSSL bindings do not operate in continuous mode so we'll wrap around it. Since the keystream * for CTR is the same for both encrypting and decrypting this function is re-used by both Base::encrypt() * and Base::decrypt(). Also, OpenSSL doesn't implement CTR for all of it's symmetric ciphers so this * function will emulate CTR with ECB when necessary. * * @see self::encrypt() * @see self::decrypt() * @param string $plaintext * @param string $encryptIV * @param array $buffer * @return string * @access private */ function _openssl_ctr_process($plaintext, &$encryptIV, &$buffer) { $ciphertext = ''; $block_size = $this->block_size; $key = $this->key; if ($this->openssl_emulate_ctr) { $xor = $encryptIV; if (strlen($buffer['ciphertext'])) { for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { $block = substr($plaintext, $i, $block_size); if (strlen($block) > strlen($buffer['ciphertext'])) { $result = @openssl_encrypt($xor, $this->cipher_name_openssl_ecb, $key, $this->openssl_options); $result = !defined('OPENSSL_RAW_DATA') ? substr($result, 0, -$this->block_size) : $result; $buffer['ciphertext'].= $result; } $this->_increment_str($xor); $otp = $this->_string_shift($buffer['ciphertext'], $block_size); $ciphertext.= $block ^ $otp; } } else { for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { $block = substr($plaintext, $i, $block_size); $otp = @openssl_encrypt($xor, $this->cipher_name_openssl_ecb, $key, $this->openssl_options); $otp = !defined('OPENSSL_RAW_DATA') ? substr($otp, 0, -$this->block_size) : $otp; $this->_increment_str($xor); $ciphertext.= $block ^ $otp; } } if ($this->continuousBuffer) { $encryptIV = $xor; if ($start = strlen($plaintext) % $block_size) { $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext']; } } return $ciphertext; } if (strlen($buffer['ciphertext'])) { $ciphertext = $plaintext ^ $this->_string_shift($buffer['ciphertext'], strlen($plaintext)); $plaintext = substr($plaintext, strlen($ciphertext)); if (!strlen($plaintext)) { return $ciphertext; } } $overflow = strlen($plaintext) % $block_size; if ($overflow) { $plaintext2 = $this->_string_pop($plaintext, $overflow); // ie. trim $plaintext to a multiple of $block_size and put rest of $plaintext in $plaintext2 $encrypted = openssl_encrypt($plaintext . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV); $temp = $this->_string_pop($encrypted, $block_size); $ciphertext.= $encrypted . ($plaintext2 ^ $temp); if ($this->continuousBuffer) { $buffer['ciphertext'] = substr($temp, $overflow); $encryptIV = $temp; } } elseif (!strlen($buffer['ciphertext'])) { $ciphertext.= openssl_encrypt($plaintext . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV); $temp = $this->_string_pop($ciphertext, $block_size); if ($this->continuousBuffer) { $encryptIV = $temp; } } if ($this->continuousBuffer) { if (!defined('OPENSSL_RAW_DATA')) { $encryptIV.= @openssl_encrypt('', $this->cipher_name_openssl_ecb, $key, $this->openssl_options); } $encryptIV = openssl_decrypt($encryptIV, $this->cipher_name_openssl_ecb, $key, $this->openssl_options); if ($overflow) { $this->_increment_str($encryptIV); } } return $ciphertext; } /** * OpenSSL OFB Processor * * PHP's OpenSSL bindings do not operate in continuous mode so we'll wrap around it. Since the keystream * for OFB is the same for both encrypting and decrypting this function is re-used by both Base::encrypt() * and Base::decrypt(). * * @see self::encrypt() * @see self::decrypt() * @param string $plaintext * @param string $encryptIV * @param array $buffer * @return string * @access private */ function _openssl_ofb_process($plaintext, &$encryptIV, &$buffer) { if (strlen($buffer['xor'])) { $ciphertext = $plaintext ^ $buffer['xor']; $buffer['xor'] = substr($buffer['xor'], strlen($ciphertext)); $plaintext = substr($plaintext, strlen($ciphertext)); } else { $ciphertext = ''; } $block_size = $this->block_size; $len = strlen($plaintext); $key = $this->key; $overflow = $len % $block_size; if (strlen($plaintext)) { if ($overflow) { $ciphertext.= openssl_encrypt(substr($plaintext, 0, -$overflow) . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV); $xor = $this->_string_pop($ciphertext, $block_size); if ($this->continuousBuffer) { $encryptIV = $xor; } $ciphertext.= $this->_string_shift($xor, $overflow) ^ substr($plaintext, -$overflow); if ($this->continuousBuffer) { $buffer['xor'] = $xor; } } else { $ciphertext = openssl_encrypt($plaintext, $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV); if ($this->continuousBuffer) { $encryptIV = substr($ciphertext, -$block_size) ^ substr($plaintext, -$block_size); } } } return $ciphertext; } /** * phpseclib <-> OpenSSL Mode Mapper * * May need to be overwritten by classes extending this one in some cases * * @return int * @access private */ function _openssl_translate_mode() { switch ($this->mode) { case self::MODE_ECB: return 'ecb'; case self::MODE_CBC: return 'cbc'; case self::MODE_CTR: return 'ctr'; case self::MODE_CFB: return 'cfb'; case self::MODE_CFB8: return 'cfb8'; case self::MODE_OFB: return 'ofb'; } } /** * Pad "packets". * * Block ciphers working by encrypting between their specified [$this->]block_size at a time * If you ever need to encrypt or decrypt something that isn't of the proper length, it becomes necessary to * pad the input so that it is of the proper length. * * Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH, * where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is * transmitted separately) * * @see self::disablePadding() * @access public */ function enablePadding() { $this->padding = true; } /** * Do not pad packets. * * @see self::enablePadding() * @access public */ function disablePadding() { $this->padding = false; } /** * Treat consecutive "packets" as if they are a continuous buffer. * * Say you have a 32-byte plaintext $plaintext. Using the default behavior, the two following code snippets * will yield different outputs: * * <code> * echo $rijndael->encrypt(substr($plaintext, 0, 16)); * echo $rijndael->encrypt(substr($plaintext, 16, 16)); * </code> * <code> * echo $rijndael->encrypt($plaintext); * </code> * * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates * another, as demonstrated with the following: * * <code> * $rijndael->encrypt(substr($plaintext, 0, 16)); * echo $rijndael->decrypt($rijndael->encrypt(substr($plaintext, 16, 16))); * </code> * <code> * echo $rijndael->decrypt($rijndael->encrypt(substr($plaintext, 16, 16))); * </code> * * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different * outputs. The reason is due to the fact that the initialization vector's change after every encryption / * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant. * * Put another way, when the continuous buffer is enabled, the state of the \phpseclib\Crypt\*() object changes after each * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them), * however, they are also less intuitive and more likely to cause you problems. * * @see self::disableContinuousBuffer() * @access public * @internal Could, but not must, extend by the child Crypt_* class */ function enableContinuousBuffer() { if ($this->mode == self::MODE_ECB) { return; } $this->continuousBuffer = true; $this->_setEngine(); } /** * Treat consecutive packets as if they are a discontinuous buffer. * * The default behavior. * * @see self::enableContinuousBuffer() * @access public * @internal Could, but not must, extend by the child Crypt_* class */ function disableContinuousBuffer() { if ($this->mode == self::MODE_ECB) { return; } if (!$this->continuousBuffer) { return; } $this->continuousBuffer = false; $this->changed = true; $this->_setEngine(); } /** * Test for engine validity * * @see self::__construct() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { switch ($engine) { case self::ENGINE_OPENSSL: if ($this->mode == self::MODE_STREAM && $this->continuousBuffer) { return false; } $this->openssl_emulate_ctr = false; $result = $this->cipher_name_openssl && extension_loaded('openssl') && // PHP 5.3.0 - 5.3.2 did not let you set IV's version_compare(PHP_VERSION, '5.3.3', '>='); if (!$result) { return false; } // prior to PHP 5.4.0 OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING were not defined. instead of expecting an integer // $options openssl_encrypt expected a boolean $raw_data. if (!defined('OPENSSL_RAW_DATA')) { $this->openssl_options = true; } else { $this->openssl_options = OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING; } $methods = openssl_get_cipher_methods(); if (in_array($this->cipher_name_openssl, $methods)) { return true; } // not all of openssl's symmetric cipher's support ctr. for those // that don't we'll emulate it switch ($this->mode) { case self::MODE_CTR: if (in_array($this->cipher_name_openssl_ecb, $methods)) { $this->openssl_emulate_ctr = true; return true; } } return false; case self::ENGINE_MCRYPT: return $this->cipher_name_mcrypt && extension_loaded('mcrypt') && in_array($this->cipher_name_mcrypt, @mcrypt_list_algorithms()); case self::ENGINE_INTERNAL: return true; } return false; } /** * Sets the preferred crypt engine * * Currently, $engine could be: * * - \phpseclib\Crypt\Base::ENGINE_OPENSSL [very fast] * * - \phpseclib\Crypt\Base::ENGINE_MCRYPT [fast] * * - \phpseclib\Crypt\Base::ENGINE_INTERNAL [slow] * * If the preferred crypt engine is not available the fastest available one will be used * * @see self::__construct() * @param int $engine * @access public */ function setPreferredEngine($engine) { switch ($engine) { //case self::ENGINE_OPENSSL; case self::ENGINE_MCRYPT: case self::ENGINE_INTERNAL: $this->preferredEngine = $engine; break; default: $this->preferredEngine = self::ENGINE_OPENSSL; } $this->_setEngine(); } /** * Returns the engine currently being utilized * * @see self::_setEngine() * @access public */ function getEngine() { return $this->engine; } /** * Sets the engine as appropriate * * @see self::__construct() * @access private */ function _setEngine() { $this->engine = null; $candidateEngines = array( $this->preferredEngine, self::ENGINE_OPENSSL, self::ENGINE_MCRYPT ); foreach ($candidateEngines as $engine) { if ($this->isValidEngine($engine)) { $this->engine = $engine; break; } } if (!$this->engine) { $this->engine = self::ENGINE_INTERNAL; } if ($this->engine != self::ENGINE_MCRYPT && $this->enmcrypt) { // Closing the current mcrypt resource(s). _mcryptSetup() will, if needed, // (re)open them with the module named in $this->cipher_name_mcrypt @mcrypt_module_close($this->enmcrypt); @mcrypt_module_close($this->demcrypt); $this->enmcrypt = null; $this->demcrypt = null; if ($this->ecb) { @mcrypt_module_close($this->ecb); $this->ecb = null; } } $this->changed = true; } /** * Encrypts a block * * Note: Must be extended by the child \phpseclib\Crypt\* class * * @access private * @param string $in * @return string */ abstract function _encryptBlock($in); /** * Decrypts a block * * Note: Must be extended by the child \phpseclib\Crypt\* class * * @access private * @param string $in * @return string */ abstract function _decryptBlock($in); /** * Setup the key (expansion) * * Only used if $engine == self::ENGINE_INTERNAL * * Note: Must extend by the child \phpseclib\Crypt\* class * * @see self::_setup() * @access private */ abstract function _setupKey(); /** * Setup the self::ENGINE_INTERNAL $engine * * (re)init, if necessary, the internal cipher $engine and flush all $buffers * Used (only) if $engine == self::ENGINE_INTERNAL * * _setup() will be called each time if $changed === true * typically this happens when using one or more of following public methods: * * - setKey() * * - setIV() * * - disableContinuousBuffer() * * - First run of encrypt() / decrypt() with no init-settings * * @see self::setKey() * @see self::setIV() * @see self::disableContinuousBuffer() * @access private * @internal _setup() is always called before en/decryption. * @internal Could, but not must, extend by the child Crypt_* class */ function _setup() { $this->_clearBuffers(); $this->_setupKey(); if ($this->use_inline_crypt) { $this->_setupInlineCrypt(); } } /** * Setup the self::ENGINE_MCRYPT $engine * * (re)init, if necessary, the (ext)mcrypt resources and flush all $buffers * Used (only) if $engine = self::ENGINE_MCRYPT * * _setupMcrypt() will be called each time if $changed === true * typically this happens when using one or more of following public methods: * * - setKey() * * - setIV() * * - disableContinuousBuffer() * * - First run of encrypt() / decrypt() * * @see self::setKey() * @see self::setIV() * @see self::disableContinuousBuffer() * @access private * @internal Could, but not must, extend by the child Crypt_* class */ function _setupMcrypt() { $this->_clearBuffers(); $this->enchanged = $this->dechanged = true; if (!isset($this->enmcrypt)) { static $mcrypt_modes = array( self::MODE_CTR => 'ctr', self::MODE_ECB => MCRYPT_MODE_ECB, self::MODE_CBC => MCRYPT_MODE_CBC, self::MODE_CFB => 'ncfb', self::MODE_CFB8 => MCRYPT_MODE_CFB, self::MODE_OFB => MCRYPT_MODE_NOFB, self::MODE_STREAM => MCRYPT_MODE_STREAM, ); $this->demcrypt = @mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], ''); $this->enmcrypt = @mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], ''); // we need the $ecb mcrypt resource (only) in MODE_CFB with enableContinuousBuffer() // to workaround mcrypt's broken ncfb implementation in buffered mode // see: {@link http://phpseclib.sourceforge.net/cfb-demo.phps} if ($this->mode == self::MODE_CFB) { $this->ecb = @mcrypt_module_open($this->cipher_name_mcrypt, '', MCRYPT_MODE_ECB, ''); } } // else should mcrypt_generic_deinit be called? if ($this->mode == self::MODE_CFB) { @mcrypt_generic_init($this->ecb, $this->key, str_repeat("\0", $this->block_size)); } } /** * Pads a string * * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize. * $this->block_size - (strlen($text) % $this->block_size) bytes are added, each of which is equal to * chr($this->block_size - (strlen($text) % $this->block_size) * * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless * and padding will, hence forth, be enabled. * * @see self::_unpad() * @param string $text * @access private * @return string */ function _pad($text) { $length = strlen($text); if (!$this->padding) { if ($length % $this->block_size == 0) { return $text; } else { user_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})"); $this->padding = true; } } $pad = $this->block_size - ($length % $this->block_size); return str_pad($text, $length + $pad, chr($pad)); } /** * Unpads a string. * * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong * and false will be returned. * * @see self::_pad() * @param string $text * @access private * @return string */ function _unpad($text) { if (!$this->padding) { return $text; } $length = ord($text[strlen($text) - 1]); if (!$length || $length > $this->block_size) { return false; } return substr($text, 0, -$length); } /** * Clears internal buffers * * Clearing/resetting the internal buffers is done everytime * after disableContinuousBuffer() or on cipher $engine (re)init * ie after setKey() or setIV() * * @access public * @internal Could, but not must, extend by the child Crypt_* class */ function _clearBuffers() { $this->enbuffer = $this->debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0, 'enmcrypt_init' => true); // mcrypt's handling of invalid's $iv: // $this->encryptIV = $this->decryptIV = strlen($this->iv) == $this->block_size ? $this->iv : str_repeat("\0", $this->block_size); $this->encryptIV = $this->decryptIV = str_pad(substr($this->iv, 0, $this->block_size), $this->block_size, "\0"); if (!$this->skip_key_adjustment) { $this->key = str_pad(substr($this->key, 0, $this->key_length), $this->key_length, "\0"); } } /** * String Shift * * Inspired by array_shift * * @param string $string * @param int $index * @access private * @return string */ function _string_shift(&$string, $index = 1) { $substr = substr($string, 0, $index); $string = substr($string, $index); return $substr; } /** * String Pop * * Inspired by array_pop * * @param string $string * @param int $index * @access private * @return string */ function _string_pop(&$string, $index = 1) { $substr = substr($string, -$index); $string = substr($string, 0, -$index); return $substr; } /** * Increment the current string * * @see self::decrypt() * @see self::encrypt() * @param string $var * @access private */ function _increment_str(&$var) { for ($i = 4; $i <= strlen($var); $i+= 4) { $temp = substr($var, -$i, 4); switch ($temp) { case "\xFF\xFF\xFF\xFF": $var = substr_replace($var, "\x00\x00\x00\x00", -$i, 4); break; case "\x7F\xFF\xFF\xFF": $var = substr_replace($var, "\x80\x00\x00\x00", -$i, 4); return; default: $temp = unpack('Nnum', $temp); $var = substr_replace($var, pack('N', $temp['num'] + 1), -$i, 4); return; } } $remainder = strlen($var) % 4; if ($remainder == 0) { return; } $temp = unpack('Nnum', str_pad(substr($var, 0, $remainder), 4, "\0", STR_PAD_LEFT)); $temp = substr(pack('N', $temp['num'] + 1), -$remainder); $var = substr_replace($var, $temp, 0, $remainder); } /** * Setup the performance-optimized function for de/encrypt() * * Stores the created (or existing) callback function-name * in $this->inline_crypt * * Internally for phpseclib developers: * * _setupInlineCrypt() would be called only if: * * - $engine == self::ENGINE_INTERNAL and * * - $use_inline_crypt === true * * - each time on _setup(), after(!) _setupKey() * * * This ensures that _setupInlineCrypt() has always a * full ready2go initializated internal cipher $engine state * where, for example, the keys allready expanded, * keys/block_size calculated and such. * * It is, each time if called, the responsibility of _setupInlineCrypt(): * * - to set $this->inline_crypt to a valid and fully working callback function * as a (faster) replacement for encrypt() / decrypt() * * - NOT to create unlimited callback functions (for memory reasons!) * no matter how often _setupInlineCrypt() would be called. At some * point of amount they must be generic re-useable. * * - the code of _setupInlineCrypt() it self, * and the generated callback code, * must be, in following order: * - 100% safe * - 100% compatible to encrypt()/decrypt() * - using only php5+ features/lang-constructs/php-extensions if * compatibility (down to php4) or fallback is provided * - readable/maintainable/understandable/commented and... not-cryptic-styled-code :-) * - >= 10% faster than encrypt()/decrypt() [which is, by the way, * the reason for the existence of _setupInlineCrypt() :-)] * - memory-nice * - short (as good as possible) * * Note: - _setupInlineCrypt() is using _createInlineCryptFunction() to create the full callback function code. * - In case of using inline crypting, _setupInlineCrypt() must extend by the child \phpseclib\Crypt\* class. * - The following variable names are reserved: * - $_* (all variable names prefixed with an underscore) * - $self (object reference to it self. Do not use $this, but $self instead) * - $in (the content of $in has to en/decrypt by the generated code) * - The callback function should not use the 'return' statement, but en/decrypt'ing the content of $in only * * * @see self::_setup() * @see self::_createInlineCryptFunction() * @see self::encrypt() * @see self::decrypt() * @access private * @internal If a Crypt_* class providing inline crypting it must extend _setupInlineCrypt() */ function _setupInlineCrypt() { // If, for any reason, an extending \phpseclib\Crypt\Base() \phpseclib\Crypt\* class // not using inline crypting then it must be ensured that: $this->use_inline_crypt = false // ie in the class var declaration of $use_inline_crypt in general for the \phpseclib\Crypt\* class, // in the constructor at object instance-time // or, if it's runtime-specific, at runtime $this->use_inline_crypt = false; } /** * Creates the performance-optimized function for en/decrypt() * * Internally for phpseclib developers: * * _createInlineCryptFunction(): * * - merge the $cipher_code [setup'ed by _setupInlineCrypt()] * with the current [$this->]mode of operation code * * - create the $inline function, which called by encrypt() / decrypt() * as its replacement to speed up the en/decryption operations. * * - return the name of the created $inline callback function * * - used to speed up en/decryption * * * * The main reason why can speed up things [up to 50%] this way are: * * - using variables more effective then regular. * (ie no use of expensive arrays but integers $k_0, $k_1 ... * or even, for example, the pure $key[] values hardcoded) * * - avoiding 1000's of function calls of ie _encryptBlock() * but inlining the crypt operations. * in the mode of operation for() loop. * * - full loop unroll the (sometimes key-dependent) rounds * avoiding this way ++$i counters and runtime-if's etc... * * The basic code architectur of the generated $inline en/decrypt() * lambda function, in pseudo php, is: * * <code> * +----------------------------------------------------------------------------------------------+ * | callback $inline = create_function: | * | lambda_function_0001_crypt_ECB($action, $text) | * | { | * | INSERT PHP CODE OF: | * | $cipher_code['init_crypt']; // general init code. | * | // ie: $sbox'es declarations used for | * | // encrypt and decrypt'ing. | * | | * | switch ($action) { | * | case 'encrypt': | * | INSERT PHP CODE OF: | * | $cipher_code['init_encrypt']; // encrypt sepcific init code. | * | ie: specified $key or $box | * | declarations for encrypt'ing. | * | | * | foreach ($ciphertext) { | * | $in = $block_size of $ciphertext; | * | | * | INSERT PHP CODE OF: | * | $cipher_code['encrypt_block']; // encrypt's (string) $in, which is always: | * | // strlen($in) == $this->block_size | * | // here comes the cipher algorithm in action | * | // for encryption. | * | // $cipher_code['encrypt_block'] has to | * | // encrypt the content of the $in variable | * | | * | $plaintext .= $in; | * | } | * | return $plaintext; | * | | * | case 'decrypt': | * | INSERT PHP CODE OF: | * | $cipher_code['init_decrypt']; // decrypt sepcific init code | * | ie: specified $key or $box | * | declarations for decrypt'ing. | * | foreach ($plaintext) { | * | $in = $block_size of $plaintext; | * | | * | INSERT PHP CODE OF: | * | $cipher_code['decrypt_block']; // decrypt's (string) $in, which is always | * | // strlen($in) == $this->block_size | * | // here comes the cipher algorithm in action | * | // for decryption. | * | // $cipher_code['decrypt_block'] has to | * | // decrypt the content of the $in variable | * | $ciphertext .= $in; | * | } | * | return $ciphertext; | * | } | * | } | * +----------------------------------------------------------------------------------------------+ * </code> * * See also the \phpseclib\Crypt\*::_setupInlineCrypt()'s for * productive inline $cipher_code's how they works. * * Structure of: * <code> * $cipher_code = array( * 'init_crypt' => (string) '', // optional * 'init_encrypt' => (string) '', // optional * 'init_decrypt' => (string) '', // optional * 'encrypt_block' => (string) '', // required * 'decrypt_block' => (string) '' // required * ); * </code> * * @see self::_setupInlineCrypt() * @see self::encrypt() * @see self::decrypt() * @param array $cipher_code * @access private * @return string (the name of the created callback function) */ function _createInlineCryptFunction($cipher_code) { $block_size = $this->block_size; // optional $init_crypt = isset($cipher_code['init_crypt']) ? $cipher_code['init_crypt'] : ''; $init_encrypt = isset($cipher_code['init_encrypt']) ? $cipher_code['init_encrypt'] : ''; $init_decrypt = isset($cipher_code['init_decrypt']) ? $cipher_code['init_decrypt'] : ''; // required $encrypt_block = $cipher_code['encrypt_block']; $decrypt_block = $cipher_code['decrypt_block']; // Generating mode of operation inline code, // merged with the $cipher_code algorithm // for encrypt- and decryption. switch ($this->mode) { case self::MODE_ECB: $encrypt = $init_encrypt . ' $_ciphertext = ""; $_plaintext_len = strlen($_text); for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') { $in = substr($_text, $_i, '.$block_size.'); '.$encrypt_block.' $_ciphertext.= $in; } return $_ciphertext; '; $decrypt = $init_decrypt . ' $_plaintext = ""; $_text = str_pad($_text, strlen($_text) + ('.$block_size.' - strlen($_text) % '.$block_size.') % '.$block_size.', chr(0)); $_ciphertext_len = strlen($_text); for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') { $in = substr($_text, $_i, '.$block_size.'); '.$decrypt_block.' $_plaintext.= $in; } return $self->_unpad($_plaintext); '; break; case self::MODE_CTR: $encrypt = $init_encrypt . ' $_ciphertext = ""; $_plaintext_len = strlen($_text); $_xor = $self->encryptIV; $_buffer = &$self->enbuffer; if (strlen($_buffer["ciphertext"])) { for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') { $_block = substr($_text, $_i, '.$block_size.'); if (strlen($_block) > strlen($_buffer["ciphertext"])) { $in = $_xor; '.$encrypt_block.' $self->_increment_str($_xor); $_buffer["ciphertext"].= $in; } $_key = $self->_string_shift($_buffer["ciphertext"], '.$block_size.'); $_ciphertext.= $_block ^ $_key; } } else { for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') { $_block = substr($_text, $_i, '.$block_size.'); $in = $_xor; '.$encrypt_block.' $self->_increment_str($_xor); $_key = $in; $_ciphertext.= $_block ^ $_key; } } if ($self->continuousBuffer) { $self->encryptIV = $_xor; if ($_start = $_plaintext_len % '.$block_size.') { $_buffer["ciphertext"] = substr($_key, $_start) . $_buffer["ciphertext"]; } } return $_ciphertext; '; $decrypt = $init_encrypt . ' $_plaintext = ""; $_ciphertext_len = strlen($_text); $_xor = $self->decryptIV; $_buffer = &$self->debuffer; if (strlen($_buffer["ciphertext"])) { for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') { $_block = substr($_text, $_i, '.$block_size.'); if (strlen($_block) > strlen($_buffer["ciphertext"])) { $in = $_xor; '.$encrypt_block.' $self->_increment_str($_xor); $_buffer["ciphertext"].= $in; } $_key = $self->_string_shift($_buffer["ciphertext"], '.$block_size.'); $_plaintext.= $_block ^ $_key; } } else { for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') { $_block = substr($_text, $_i, '.$block_size.'); $in = $_xor; '.$encrypt_block.' $self->_increment_str($_xor); $_key = $in; $_plaintext.= $_block ^ $_key; } } if ($self->continuousBuffer) { $self->decryptIV = $_xor; if ($_start = $_ciphertext_len % '.$block_size.') { $_buffer["ciphertext"] = substr($_key, $_start) . $_buffer["ciphertext"]; } } return $_plaintext; '; break; case self::MODE_CFB: $encrypt = $init_encrypt . ' $_ciphertext = ""; $_buffer = &$self->enbuffer; if ($self->continuousBuffer) { $_iv = &$self->encryptIV; $_pos = &$_buffer["pos"]; } else { $_iv = $self->encryptIV; $_pos = 0; } $_len = strlen($_text); $_i = 0; if ($_pos) { $_orig_pos = $_pos; $_max = '.$block_size.' - $_pos; if ($_len >= $_max) { $_i = $_max; $_len-= $_max; $_pos = 0; } else { $_i = $_len; $_pos+= $_len; $_len = 0; } $_ciphertext = substr($_iv, $_orig_pos) ^ $_text; $_iv = substr_replace($_iv, $_ciphertext, $_orig_pos, $_i); } while ($_len >= '.$block_size.') { $in = $_iv; '.$encrypt_block.'; $_iv = $in ^ substr($_text, $_i, '.$block_size.'); $_ciphertext.= $_iv; $_len-= '.$block_size.'; $_i+= '.$block_size.'; } if ($_len) { $in = $_iv; '.$encrypt_block.' $_iv = $in; $_block = $_iv ^ substr($_text, $_i); $_iv = substr_replace($_iv, $_block, 0, $_len); $_ciphertext.= $_block; $_pos = $_len; } return $_ciphertext; '; $decrypt = $init_encrypt . ' $_plaintext = ""; $_buffer = &$self->debuffer; if ($self->continuousBuffer) { $_iv = &$self->decryptIV; $_pos = &$_buffer["pos"]; } else { $_iv = $self->decryptIV; $_pos = 0; } $_len = strlen($_text); $_i = 0; if ($_pos) { $_orig_pos = $_pos; $_max = '.$block_size.' - $_pos; if ($_len >= $_max) { $_i = $_max; $_len-= $_max; $_pos = 0; } else { $_i = $_len; $_pos+= $_len; $_len = 0; } $_plaintext = substr($_iv, $_orig_pos) ^ $_text; $_iv = substr_replace($_iv, substr($_text, 0, $_i), $_orig_pos, $_i); } while ($_len >= '.$block_size.') { $in = $_iv; '.$encrypt_block.' $_iv = $in; $cb = substr($_text, $_i, '.$block_size.'); $_plaintext.= $_iv ^ $cb; $_iv = $cb; $_len-= '.$block_size.'; $_i+= '.$block_size.'; } if ($_len) { $in = $_iv; '.$encrypt_block.' $_iv = $in; $_plaintext.= $_iv ^ substr($_text, $_i); $_iv = substr_replace($_iv, substr($_text, $_i), 0, $_len); $_pos = $_len; } return $_plaintext; '; break; case self::MODE_CFB8: $encrypt = $init_encrypt . ' $_ciphertext = ""; $_len = strlen($_text); $_iv = $self->encryptIV; for ($_i = 0; $_i < $_len; ++$_i) { $in = $_iv; '.$encrypt_block.' $_ciphertext .= ($_c = $_text[$_i] ^ $in); $_iv = substr($_iv, 1) . $_c; } if ($self->continuousBuffer) { if ($_len >= '.$block_size.') { $self->encryptIV = substr($_ciphertext, -'.$block_size.'); } else { $self->encryptIV = substr($self->encryptIV, $_len - '.$block_size.') . substr($_ciphertext, -$_len); } } return $_ciphertext; '; $decrypt = $init_encrypt . ' $_plaintext = ""; $_len = strlen($_text); $_iv = $self->decryptIV; for ($_i = 0; $_i < $_len; ++$_i) { $in = $_iv; '.$encrypt_block.' $_plaintext .= $_text[$_i] ^ $in; $_iv = substr($_iv, 1) . $_text[$_i]; } if ($self->continuousBuffer) { if ($_len >= '.$block_size.') { $self->decryptIV = substr($_text, -'.$block_size.'); } else { $self->decryptIV = substr($self->decryptIV, $_len - '.$block_size.') . substr($_text, -$_len); } } return $_plaintext; '; break; case self::MODE_OFB: $encrypt = $init_encrypt . ' $_ciphertext = ""; $_plaintext_len = strlen($_text); $_xor = $self->encryptIV; $_buffer = &$self->enbuffer; if (strlen($_buffer["xor"])) { for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') { $_block = substr($_text, $_i, '.$block_size.'); if (strlen($_block) > strlen($_buffer["xor"])) { $in = $_xor; '.$encrypt_block.' $_xor = $in; $_buffer["xor"].= $_xor; } $_key = $self->_string_shift($_buffer["xor"], '.$block_size.'); $_ciphertext.= $_block ^ $_key; } } else { for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') { $in = $_xor; '.$encrypt_block.' $_xor = $in; $_ciphertext.= substr($_text, $_i, '.$block_size.') ^ $_xor; } $_key = $_xor; } if ($self->continuousBuffer) { $self->encryptIV = $_xor; if ($_start = $_plaintext_len % '.$block_size.') { $_buffer["xor"] = substr($_key, $_start) . $_buffer["xor"]; } } return $_ciphertext; '; $decrypt = $init_encrypt . ' $_plaintext = ""; $_ciphertext_len = strlen($_text); $_xor = $self->decryptIV; $_buffer = &$self->debuffer; if (strlen($_buffer["xor"])) { for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') { $_block = substr($_text, $_i, '.$block_size.'); if (strlen($_block) > strlen($_buffer["xor"])) { $in = $_xor; '.$encrypt_block.' $_xor = $in; $_buffer["xor"].= $_xor; } $_key = $self->_string_shift($_buffer["xor"], '.$block_size.'); $_plaintext.= $_block ^ $_key; } } else { for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') { $in = $_xor; '.$encrypt_block.' $_xor = $in; $_plaintext.= substr($_text, $_i, '.$block_size.') ^ $_xor; } $_key = $_xor; } if ($self->continuousBuffer) { $self->decryptIV = $_xor; if ($_start = $_ciphertext_len % '.$block_size.') { $_buffer["xor"] = substr($_key, $_start) . $_buffer["xor"]; } } return $_plaintext; '; break; case self::MODE_STREAM: $encrypt = $init_encrypt . ' $_ciphertext = ""; '.$encrypt_block.' return $_ciphertext; '; $decrypt = $init_decrypt . ' $_plaintext = ""; '.$decrypt_block.' return $_plaintext; '; break; // case self::MODE_CBC: default: $encrypt = $init_encrypt . ' $_ciphertext = ""; $_plaintext_len = strlen($_text); $in = $self->encryptIV; for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') { $in = substr($_text, $_i, '.$block_size.') ^ $in; '.$encrypt_block.' $_ciphertext.= $in; } if ($self->continuousBuffer) { $self->encryptIV = $in; } return $_ciphertext; '; $decrypt = $init_decrypt . ' $_plaintext = ""; $_text = str_pad($_text, strlen($_text) + ('.$block_size.' - strlen($_text) % '.$block_size.') % '.$block_size.', chr(0)); $_ciphertext_len = strlen($_text); $_iv = $self->decryptIV; for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') { $in = $_block = substr($_text, $_i, '.$block_size.'); '.$decrypt_block.' $_plaintext.= $in ^ $_iv; $_iv = $_block; } if ($self->continuousBuffer) { $self->decryptIV = $_iv; } return $self->_unpad($_plaintext); '; break; } // Create the $inline function and return its name as string. Ready to run! if (version_compare(PHP_VERSION, '5.3.0') >= 0) { eval('$func = function ($_action, &$self, $_text) { ' . $init_crypt . 'if ($_action == "encrypt") { ' . $encrypt . ' } else { ' . $decrypt . ' } };'); return $func; } return create_function('$_action, &$self, $_text', $init_crypt . 'if ($_action == "encrypt") { ' . $encrypt . ' } else { ' . $decrypt . ' }'); } /** * Holds the lambda_functions table (classwide) * * Each name of the lambda function, created from * _setupInlineCrypt() && _createInlineCryptFunction() * is stored, classwide (!), here for reusing. * * The string-based index of $function is a classwide * unique value representing, at least, the $mode of * operation (or more... depends of the optimizing level) * for which $mode the lambda function was created. * * @access private * @return array &$functions */ function &_getLambdaFunctions() { static $functions = array(); return $functions; } /** * Generates a digest from $bytes * * @see self::_setupInlineCrypt() * @access private * @param $bytes * @return string */ function _hashInlineCryptFunction($bytes) { if (!isset(self::$WHIRLPOOL_AVAILABLE)) { self::$WHIRLPOOL_AVAILABLE = extension_loaded('hash') && in_array('whirlpool', hash_algos()); } $result = ''; $hash = $bytes; switch (true) { case self::$WHIRLPOOL_AVAILABLE: foreach (str_split($bytes, 64) as $t) { $hash = hash('whirlpool', $hash, true); $result .= $t ^ $hash; } return $result . hash('whirlpool', $hash, true); default: $len = strlen($bytes); for ($i = 0; $i < $len; $i+=20) { $t = substr($bytes, $i, 20); $hash = pack('H*', sha1($hash)); $result .= $t ^ $hash; } return $result . pack('H*', sha1($hash)); } } /** * Convert float to int * * On ARM CPUs converting floats to ints doesn't always work * * @access private * @param string $x * @return int */ function safe_intval($x) { switch (true) { case is_int($x): // PHP 5.3, per http://php.net/releases/5_3_0.php, introduced "more consistent float rounding" case (php_uname('m') & "\xDF\xDF\xDF") != 'ARM': return $x; } return (fmod($x, 0x80000000) & 0x7FFFFFFF) | ((fmod(floor($x / 0x80000000), 2) & 1) << 31); } /** * eval()'able string for in-line float to int * * @access private * @return string */ function safe_intval_inline() { switch (true) { case defined('PHP_INT_SIZE') && PHP_INT_SIZE == 8: case (php_uname('m') & "\xDF\xDF\xDF") != 'ARM': return '%s'; break; default: $safeint = '(is_int($temp = %s) ? $temp : (fmod($temp, 0x80000000) & 0x7FFFFFFF) | '; return $safeint . '((fmod(floor($temp / 0x80000000), 2) & 1) << 31))'; } } } <?php /** * Pure-PHP implementation of Rijndael. * * Uses mcrypt, if available/possible, and an internal implementation, otherwise. * * PHP version 5 * * If {@link self::setBlockLength() setBlockLength()} isn't called, it'll be assumed to be 128 bits. If * {@link self::setKeyLength() setKeyLength()} isn't called, it'll be calculated from * {@link self::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's * 136-bits it'll be null-padded to 192-bits and 192 bits will be the key length until * {@link self::setKey() setKey()} is called, again, at which point, it'll be recalculated. * * Not all Rijndael implementations may support 160-bits or 224-bits as the block length / key length. mcrypt, for example, * does not. AES, itself, only supports block lengths of 128 and key lengths of 128, 192, and 256. * {@link http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=10 Rijndael-ammended.pdf#page=10} defines the * algorithm for block lengths of 192 and 256 but not for block lengths / key lengths of 160 and 224. Indeed, 160 and 224 * are first defined as valid key / block lengths in * {@link http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=44 Rijndael-ammended.pdf#page=44}: * Extensions: Other block and Cipher Key lengths. * Note: Use of 160/224-bit Keys must be explicitly set by setKeyLength(160) respectively setKeyLength(224). * * {@internal The variable names are the same as those in * {@link http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf#page=10 fips-197.pdf#page=10}.}} * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $rijndael = new \phpseclib\Crypt\Rijndael(); * * $rijndael->setKey('abcdefghijklmnop'); * * $size = 10 * 1024; * $plaintext = ''; * for ($i = 0; $i < $size; $i++) { * $plaintext.= 'a'; * } * * echo $rijndael->decrypt($rijndael->encrypt($plaintext)); * ?> * </code> * * @category Crypt * @package Rijndael * @author Jim Wigginton <terrafrost@php.net> * @copyright 2008 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; /** * Pure-PHP implementation of Rijndael. * * @package Rijndael * @author Jim Wigginton <terrafrost@php.net> * @access public */ class Rijndael extends Base { /** * The mcrypt specific name of the cipher * * Mcrypt is useable for 128/192/256-bit $block_size/$key_length. For 160/224 not. * \phpseclib\Crypt\Rijndael determines automatically whether mcrypt is useable * or not for the current $block_size/$key_length. * In case of, $cipher_name_mcrypt will be set dynamically at run time accordingly. * * @see \phpseclib\Crypt\Base::cipher_name_mcrypt * @see \phpseclib\Crypt\Base::engine * @see self::isValidEngine() * @var string * @access private */ var $cipher_name_mcrypt = 'rijndael-128'; /** * The default salt used by setPassword() * * @see \phpseclib\Crypt\Base::password_default_salt * @see \phpseclib\Crypt\Base::setPassword() * @var string * @access private */ var $password_default_salt = 'phpseclib'; /** * The Key Schedule * * @see self::_setup() * @var array * @access private */ var $w; /** * The Inverse Key Schedule * * @see self::_setup() * @var array * @access private */ var $dw; /** * The Block Length divided by 32 * * @see self::setBlockLength() * @var int * @access private * @internal The max value is 256 / 32 = 8, the min value is 128 / 32 = 4. Exists in conjunction with $block_size * because the encryption / decryption / key schedule creation requires this number and not $block_size. We could * derive this from $block_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu * of that, we'll just precompute it once. */ var $Nb = 4; /** * The Key Length (in bytes) * * @see self::setKeyLength() * @var int * @access private * @internal The max value is 256 / 8 = 32, the min value is 128 / 8 = 16. Exists in conjunction with $Nk * because the encryption / decryption / key schedule creation requires this number and not $key_length. We could * derive this from $key_length or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu * of that, we'll just precompute it once. */ var $key_length = 16; /** * The Key Length divided by 32 * * @see self::setKeyLength() * @var int * @access private * @internal The max value is 256 / 32 = 8, the min value is 128 / 32 = 4 */ var $Nk = 4; /** * The Number of Rounds * * @var int * @access private * @internal The max value is 14, the min value is 10. */ var $Nr; /** * Shift offsets * * @var array * @access private */ var $c; /** * Holds the last used key- and block_size information * * @var array * @access private */ var $kl; /** * Sets the key length. * * Valid key lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount. * * Note: phpseclib extends Rijndael (and AES) for using 160- and 224-bit keys but they are officially not defined * and the most (if not all) implementations are not able using 160/224-bit keys but round/pad them up to * 192/256 bits as, for example, mcrypt will do. * * That said, if you want be compatible with other Rijndael and AES implementations, * you should not setKeyLength(160) or setKeyLength(224). * * Additional: In case of 160- and 224-bit keys, phpseclib will/can, for that reason, not use * the mcrypt php extension, even if available. * This results then in slower encryption. * * @access public * @param int $length */ function setKeyLength($length) { switch (true) { case $length <= 128: $this->key_length = 16; break; case $length <= 160: $this->key_length = 20; break; case $length <= 192: $this->key_length = 24; break; case $length <= 224: $this->key_length = 28; break; default: $this->key_length = 32; } parent::setKeyLength($length); } /** * Sets the block length * * Valid block lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount. * * @access public * @param int $length */ function setBlockLength($length) { $length >>= 5; if ($length > 8) { $length = 8; } elseif ($length < 4) { $length = 4; } $this->Nb = $length; $this->block_size = $length << 2; $this->changed = true; $this->_setEngine(); } /** * Test for engine validity * * This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine() * * @see \phpseclib\Crypt\Base::__construct() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { switch ($engine) { case self::ENGINE_OPENSSL: if ($this->block_size != 16) { return false; } $this->cipher_name_openssl_ecb = 'aes-' . ($this->key_length << 3) . '-ecb'; $this->cipher_name_openssl = 'aes-' . ($this->key_length << 3) . '-' . $this->_openssl_translate_mode(); break; case self::ENGINE_MCRYPT: $this->cipher_name_mcrypt = 'rijndael-' . ($this->block_size << 3); if ($this->key_length % 8) { // is it a 160/224-bit key? // mcrypt is not usable for them, only for 128/192/256-bit keys return false; } } return parent::isValidEngine($engine); } /** * Encrypts a block * * @access private * @param string $in * @return string */ function _encryptBlock($in) { static $tables; if (empty($tables)) { $tables = &$this->_getTables(); } $t0 = $tables[0]; $t1 = $tables[1]; $t2 = $tables[2]; $t3 = $tables[3]; $sbox = $tables[4]; $state = array(); $words = unpack('N*', $in); $c = $this->c; $w = $this->w; $Nb = $this->Nb; $Nr = $this->Nr; // addRoundKey $wc = $Nb - 1; foreach ($words as $word) { $state[] = $word ^ $w[++$wc]; } // fips-197.pdf#page=19, "Figure 5. Pseudo Code for the Cipher", states that this loop has four components - // subBytes, shiftRows, mixColumns, and addRoundKey. fips-197.pdf#page=30, "Implementation Suggestions Regarding // Various Platforms" suggests that performs enhanced implementations are described in Rijndael-ammended.pdf. // Rijndael-ammended.pdf#page=20, "Implementation aspects / 32-bit processor", discusses such an optimization. // Unfortunately, the description given there is not quite correct. Per aes.spec.v316.pdf#page=19 [1], // equation (7.4.7) is supposed to use addition instead of subtraction, so we'll do that here, as well. // [1] http://fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.v316.pdf $temp = array(); for ($round = 1; $round < $Nr; ++$round) { $i = 0; // $c[0] == 0 $j = $c[1]; $k = $c[2]; $l = $c[3]; while ($i < $Nb) { $temp[$i] = $t0[$state[$i] >> 24 & 0x000000FF] ^ $t1[$state[$j] >> 16 & 0x000000FF] ^ $t2[$state[$k] >> 8 & 0x000000FF] ^ $t3[$state[$l] & 0x000000FF] ^ $w[++$wc]; ++$i; $j = ($j + 1) % $Nb; $k = ($k + 1) % $Nb; $l = ($l + 1) % $Nb; } $state = $temp; } // subWord for ($i = 0; $i < $Nb; ++$i) { $state[$i] = $sbox[$state[$i] & 0x000000FF] | ($sbox[$state[$i] >> 8 & 0x000000FF] << 8) | ($sbox[$state[$i] >> 16 & 0x000000FF] << 16) | ($sbox[$state[$i] >> 24 & 0x000000FF] << 24); } // shiftRows + addRoundKey $i = 0; // $c[0] == 0 $j = $c[1]; $k = $c[2]; $l = $c[3]; while ($i < $Nb) { $temp[$i] = ($state[$i] & 0xFF000000) ^ ($state[$j] & 0x00FF0000) ^ ($state[$k] & 0x0000FF00) ^ ($state[$l] & 0x000000FF) ^ $w[$i]; ++$i; $j = ($j + 1) % $Nb; $k = ($k + 1) % $Nb; $l = ($l + 1) % $Nb; } switch ($Nb) { case 8: return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6], $temp[7]); case 7: return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6]); case 6: return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5]); case 5: return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4]); default: return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3]); } } /** * Decrypts a block * * @access private * @param string $in * @return string */ function _decryptBlock($in) { static $invtables; if (empty($invtables)) { $invtables = &$this->_getInvTables(); } $dt0 = $invtables[0]; $dt1 = $invtables[1]; $dt2 = $invtables[2]; $dt3 = $invtables[3]; $isbox = $invtables[4]; $state = array(); $words = unpack('N*', $in); $c = $this->c; $dw = $this->dw; $Nb = $this->Nb; $Nr = $this->Nr; // addRoundKey $wc = $Nb - 1; foreach ($words as $word) { $state[] = $word ^ $dw[++$wc]; } $temp = array(); for ($round = $Nr - 1; $round > 0; --$round) { $i = 0; // $c[0] == 0 $j = $Nb - $c[1]; $k = $Nb - $c[2]; $l = $Nb - $c[3]; while ($i < $Nb) { $temp[$i] = $dt0[$state[$i] >> 24 & 0x000000FF] ^ $dt1[$state[$j] >> 16 & 0x000000FF] ^ $dt2[$state[$k] >> 8 & 0x000000FF] ^ $dt3[$state[$l] & 0x000000FF] ^ $dw[++$wc]; ++$i; $j = ($j + 1) % $Nb; $k = ($k + 1) % $Nb; $l = ($l + 1) % $Nb; } $state = $temp; } // invShiftRows + invSubWord + addRoundKey $i = 0; // $c[0] == 0 $j = $Nb - $c[1]; $k = $Nb - $c[2]; $l = $Nb - $c[3]; while ($i < $Nb) { $word = ($state[$i] & 0xFF000000) | ($state[$j] & 0x00FF0000) | ($state[$k] & 0x0000FF00) | ($state[$l] & 0x000000FF); $temp[$i] = $dw[$i] ^ ($isbox[$word & 0x000000FF] | ($isbox[$word >> 8 & 0x000000FF] << 8) | ($isbox[$word >> 16 & 0x000000FF] << 16) | ($isbox[$word >> 24 & 0x000000FF] << 24)); ++$i; $j = ($j + 1) % $Nb; $k = ($k + 1) % $Nb; $l = ($l + 1) % $Nb; } switch ($Nb) { case 8: return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6], $temp[7]); case 7: return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6]); case 6: return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5]); case 5: return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4]); default: return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3]); } } /** * Setup the key (expansion) * * @see \phpseclib\Crypt\Base::_setupKey() * @access private */ function _setupKey() { // Each number in $rcon is equal to the previous number multiplied by two in Rijndael's finite field. // See http://en.wikipedia.org/wiki/Finite_field_arithmetic#Multiplicative_inverse static $rcon = array(0, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000, 0x6C000000, 0xD8000000, 0xAB000000, 0x4D000000, 0x9A000000, 0x2F000000, 0x5E000000, 0xBC000000, 0x63000000, 0xC6000000, 0x97000000, 0x35000000, 0x6A000000, 0xD4000000, 0xB3000000, 0x7D000000, 0xFA000000, 0xEF000000, 0xC5000000, 0x91000000 ); if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->key_length === $this->kl['key_length'] && $this->block_size === $this->kl['block_size']) { // already expanded return; } $this->kl = array('key' => $this->key, 'key_length' => $this->key_length, 'block_size' => $this->block_size); $this->Nk = $this->key_length >> 2; // see Rijndael-ammended.pdf#page=44 $this->Nr = max($this->Nk, $this->Nb) + 6; // shift offsets for Nb = 5, 7 are defined in Rijndael-ammended.pdf#page=44, // "Table 8: Shift offsets in Shiftrow for the alternative block lengths" // shift offsets for Nb = 4, 6, 8 are defined in Rijndael-ammended.pdf#page=14, // "Table 2: Shift offsets for different block lengths" switch ($this->Nb) { case 4: case 5: case 6: $this->c = array(0, 1, 2, 3); break; case 7: $this->c = array(0, 1, 2, 4); break; case 8: $this->c = array(0, 1, 3, 4); } $w = array_values(unpack('N*words', $this->key)); $length = $this->Nb * ($this->Nr + 1); for ($i = $this->Nk; $i < $length; $i++) { $temp = $w[$i - 1]; if ($i % $this->Nk == 0) { // according to <http://php.net/language.types.integer>, "the size of an integer is platform-dependent". // on a 32-bit machine, it's 32-bits, and on a 64-bit machine, it's 64-bits. on a 32-bit machine, // 0xFFFFFFFF << 8 == 0xFFFFFF00, but on a 64-bit machine, it equals 0xFFFFFFFF00. as such, doing 'and' // with 0xFFFFFFFF (or 0xFFFFFF00) on a 32-bit machine is unnecessary, but on a 64-bit machine, it is. $temp = (($temp << 8) & 0xFFFFFF00) | (($temp >> 24) & 0x000000FF); // rotWord $temp = $this->_subWord($temp) ^ $rcon[$i / $this->Nk]; } elseif ($this->Nk > 6 && $i % $this->Nk == 4) { $temp = $this->_subWord($temp); } $w[$i] = $w[$i - $this->Nk] ^ $temp; } // convert the key schedule from a vector of $Nb * ($Nr + 1) length to a matrix with $Nr + 1 rows and $Nb columns // and generate the inverse key schedule. more specifically, // according to <http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=23> (section 5.3.3), // "The key expansion for the Inverse Cipher is defined as follows: // 1. Apply the Key Expansion. // 2. Apply InvMixColumn to all Round Keys except the first and the last one." // also, see fips-197.pdf#page=27, "5.3.5 Equivalent Inverse Cipher" list($dt0, $dt1, $dt2, $dt3) = $this->_getInvTables(); $temp = $this->w = $this->dw = array(); for ($i = $row = $col = 0; $i < $length; $i++, $col++) { if ($col == $this->Nb) { if ($row == 0) { $this->dw[0] = $this->w[0]; } else { // subWord + invMixColumn + invSubWord = invMixColumn $j = 0; while ($j < $this->Nb) { $dw = $this->_subWord($this->w[$row][$j]); $temp[$j] = $dt0[$dw >> 24 & 0x000000FF] ^ $dt1[$dw >> 16 & 0x000000FF] ^ $dt2[$dw >> 8 & 0x000000FF] ^ $dt3[$dw & 0x000000FF]; $j++; } $this->dw[$row] = $temp; } $col = 0; $row++; } $this->w[$row][$col] = $w[$i]; } $this->dw[$row] = $this->w[$row]; // Converting to 1-dim key arrays (both ascending) $this->dw = array_reverse($this->dw); $w = array_pop($this->w); $dw = array_pop($this->dw); foreach ($this->w as $r => $wr) { foreach ($wr as $c => $wc) { $w[] = $wc; $dw[] = $this->dw[$r][$c]; } } $this->w = $w; $this->dw = $dw; } /** * Performs S-Box substitutions * * @access private * @param int $word */ function _subWord($word) { static $sbox; if (empty($sbox)) { list(, , , , $sbox) = $this->_getTables(); } return $sbox[$word & 0x000000FF] | ($sbox[$word >> 8 & 0x000000FF] << 8) | ($sbox[$word >> 16 & 0x000000FF] << 16) | ($sbox[$word >> 24 & 0x000000FF] << 24); } /** * Provides the mixColumns and sboxes tables * * @see self::_encryptBlock() * @see self::_setupInlineCrypt() * @see self::_subWord() * @access private * @return array &$tables */ function &_getTables() { static $tables; if (empty($tables)) { // according to <http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=19> (section 5.2.1), // precomputed tables can be used in the mixColumns phase. in that example, they're assigned t0...t3, so // those are the names we'll use. $t3 = array_map('intval', array( // with array_map('intval', ...) we ensure we have only int's and not // some slower floats converted by php automatically on high values 0x6363A5C6, 0x7C7C84F8, 0x777799EE, 0x7B7B8DF6, 0xF2F20DFF, 0x6B6BBDD6, 0x6F6FB1DE, 0xC5C55491, 0x30305060, 0x01010302, 0x6767A9CE, 0x2B2B7D56, 0xFEFE19E7, 0xD7D762B5, 0xABABE64D, 0x76769AEC, 0xCACA458F, 0x82829D1F, 0xC9C94089, 0x7D7D87FA, 0xFAFA15EF, 0x5959EBB2, 0x4747C98E, 0xF0F00BFB, 0xADADEC41, 0xD4D467B3, 0xA2A2FD5F, 0xAFAFEA45, 0x9C9CBF23, 0xA4A4F753, 0x727296E4, 0xC0C05B9B, 0xB7B7C275, 0xFDFD1CE1, 0x9393AE3D, 0x26266A4C, 0x36365A6C, 0x3F3F417E, 0xF7F702F5, 0xCCCC4F83, 0x34345C68, 0xA5A5F451, 0xE5E534D1, 0xF1F108F9, 0x717193E2, 0xD8D873AB, 0x31315362, 0x15153F2A, 0x04040C08, 0xC7C75295, 0x23236546, 0xC3C35E9D, 0x18182830, 0x9696A137, 0x05050F0A, 0x9A9AB52F, 0x0707090E, 0x12123624, 0x80809B1B, 0xE2E23DDF, 0xEBEB26CD, 0x2727694E, 0xB2B2CD7F, 0x75759FEA, 0x09091B12, 0x83839E1D, 0x2C2C7458, 0x1A1A2E34, 0x1B1B2D36, 0x6E6EB2DC, 0x5A5AEEB4, 0xA0A0FB5B, 0x5252F6A4, 0x3B3B4D76, 0xD6D661B7, 0xB3B3CE7D, 0x29297B52, 0xE3E33EDD, 0x2F2F715E, 0x84849713, 0x5353F5A6, 0xD1D168B9, 0x00000000, 0xEDED2CC1, 0x20206040, 0xFCFC1FE3, 0xB1B1C879, 0x5B5BEDB6, 0x6A6ABED4, 0xCBCB468D, 0xBEBED967, 0x39394B72, 0x4A4ADE94, 0x4C4CD498, 0x5858E8B0, 0xCFCF4A85, 0xD0D06BBB, 0xEFEF2AC5, 0xAAAAE54F, 0xFBFB16ED, 0x4343C586, 0x4D4DD79A, 0x33335566, 0x85859411, 0x4545CF8A, 0xF9F910E9, 0x02020604, 0x7F7F81FE, 0x5050F0A0, 0x3C3C4478, 0x9F9FBA25, 0xA8A8E34B, 0x5151F3A2, 0xA3A3FE5D, 0x4040C080, 0x8F8F8A05, 0x9292AD3F, 0x9D9DBC21, 0x38384870, 0xF5F504F1, 0xBCBCDF63, 0xB6B6C177, 0xDADA75AF, 0x21216342, 0x10103020, 0xFFFF1AE5, 0xF3F30EFD, 0xD2D26DBF, 0xCDCD4C81, 0x0C0C1418, 0x13133526, 0xECEC2FC3, 0x5F5FE1BE, 0x9797A235, 0x4444CC88, 0x1717392E, 0xC4C45793, 0xA7A7F255, 0x7E7E82FC, 0x3D3D477A, 0x6464ACC8, 0x5D5DE7BA, 0x19192B32, 0x737395E6, 0x6060A0C0, 0x81819819, 0x4F4FD19E, 0xDCDC7FA3, 0x22226644, 0x2A2A7E54, 0x9090AB3B, 0x8888830B, 0x4646CA8C, 0xEEEE29C7, 0xB8B8D36B, 0x14143C28, 0xDEDE79A7, 0x5E5EE2BC, 0x0B0B1D16, 0xDBDB76AD, 0xE0E03BDB, 0x32325664, 0x3A3A4E74, 0x0A0A1E14, 0x4949DB92, 0x06060A0C, 0x24246C48, 0x5C5CE4B8, 0xC2C25D9F, 0xD3D36EBD, 0xACACEF43, 0x6262A6C4, 0x9191A839, 0x9595A431, 0xE4E437D3, 0x79798BF2, 0xE7E732D5, 0xC8C8438B, 0x3737596E, 0x6D6DB7DA, 0x8D8D8C01, 0xD5D564B1, 0x4E4ED29C, 0xA9A9E049, 0x6C6CB4D8, 0x5656FAAC, 0xF4F407F3, 0xEAEA25CF, 0x6565AFCA, 0x7A7A8EF4, 0xAEAEE947, 0x08081810, 0xBABAD56F, 0x787888F0, 0x25256F4A, 0x2E2E725C, 0x1C1C2438, 0xA6A6F157, 0xB4B4C773, 0xC6C65197, 0xE8E823CB, 0xDDDD7CA1, 0x74749CE8, 0x1F1F213E, 0x4B4BDD96, 0xBDBDDC61, 0x8B8B860D, 0x8A8A850F, 0x707090E0, 0x3E3E427C, 0xB5B5C471, 0x6666AACC, 0x4848D890, 0x03030506, 0xF6F601F7, 0x0E0E121C, 0x6161A3C2, 0x35355F6A, 0x5757F9AE, 0xB9B9D069, 0x86869117, 0xC1C15899, 0x1D1D273A, 0x9E9EB927, 0xE1E138D9, 0xF8F813EB, 0x9898B32B, 0x11113322, 0x6969BBD2, 0xD9D970A9, 0x8E8E8907, 0x9494A733, 0x9B9BB62D, 0x1E1E223C, 0x87879215, 0xE9E920C9, 0xCECE4987, 0x5555FFAA, 0x28287850, 0xDFDF7AA5, 0x8C8C8F03, 0xA1A1F859, 0x89898009, 0x0D0D171A, 0xBFBFDA65, 0xE6E631D7, 0x4242C684, 0x6868B8D0, 0x4141C382, 0x9999B029, 0x2D2D775A, 0x0F0F111E, 0xB0B0CB7B, 0x5454FCA8, 0xBBBBD66D, 0x16163A2C )); foreach ($t3 as $t3i) { $t0[] = (($t3i << 24) & 0xFF000000) | (($t3i >> 8) & 0x00FFFFFF); $t1[] = (($t3i << 16) & 0xFFFF0000) | (($t3i >> 16) & 0x0000FFFF); $t2[] = (($t3i << 8) & 0xFFFFFF00) | (($t3i >> 24) & 0x000000FF); } $tables = array( // The Precomputed mixColumns tables t0 - t3 $t0, $t1, $t2, $t3, // The SubByte S-Box array( 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16 ) ); } return $tables; } /** * Provides the inverse mixColumns and inverse sboxes tables * * @see self::_decryptBlock() * @see self::_setupInlineCrypt() * @see self::_setupKey() * @access private * @return array &$tables */ function &_getInvTables() { static $tables; if (empty($tables)) { $dt3 = array_map('intval', array( 0xF4A75051, 0x4165537E, 0x17A4C31A, 0x275E963A, 0xAB6BCB3B, 0x9D45F11F, 0xFA58ABAC, 0xE303934B, 0x30FA5520, 0x766DF6AD, 0xCC769188, 0x024C25F5, 0xE5D7FC4F, 0x2ACBD7C5, 0x35448026, 0x62A38FB5, 0xB15A49DE, 0xBA1B6725, 0xEA0E9845, 0xFEC0E15D, 0x2F7502C3, 0x4CF01281, 0x4697A38D, 0xD3F9C66B, 0x8F5FE703, 0x929C9515, 0x6D7AEBBF, 0x5259DA95, 0xBE832DD4, 0x7421D358, 0xE0692949, 0xC9C8448E, 0xC2896A75, 0x8E7978F4, 0x583E6B99, 0xB971DD27, 0xE14FB6BE, 0x88AD17F0, 0x20AC66C9, 0xCE3AB47D, 0xDF4A1863, 0x1A3182E5, 0x51336097, 0x537F4562, 0x6477E0B1, 0x6BAE84BB, 0x81A01CFE, 0x082B94F9, 0x48685870, 0x45FD198F, 0xDE6C8794, 0x7BF8B752, 0x73D323AB, 0x4B02E272, 0x1F8F57E3, 0x55AB2A66, 0xEB2807B2, 0xB5C2032F, 0xC57B9A86, 0x3708A5D3, 0x2887F230, 0xBFA5B223, 0x036ABA02, 0x16825CED, 0xCF1C2B8A, 0x79B492A7, 0x07F2F0F3, 0x69E2A14E, 0xDAF4CD65, 0x05BED506, 0x34621FD1, 0xA6FE8AC4, 0x2E539D34, 0xF355A0A2, 0x8AE13205, 0xF6EB75A4, 0x83EC390B, 0x60EFAA40, 0x719F065E, 0x6E1051BD, 0x218AF93E, 0xDD063D96, 0x3E05AEDD, 0xE6BD464D, 0x548DB591, 0xC45D0571, 0x06D46F04, 0x5015FF60, 0x98FB2419, 0xBDE997D6, 0x4043CC89, 0xD99E7767, 0xE842BDB0, 0x898B8807, 0x195B38E7, 0xC8EEDB79, 0x7C0A47A1, 0x420FE97C, 0x841EC9F8, 0x00000000, 0x80868309, 0x2BED4832, 0x1170AC1E, 0x5A724E6C, 0x0EFFFBFD, 0x8538560F, 0xAED51E3D, 0x2D392736, 0x0FD9640A, 0x5CA62168, 0x5B54D19B, 0x362E3A24, 0x0A67B10C, 0x57E70F93, 0xEE96D2B4, 0x9B919E1B, 0xC0C54F80, 0xDC20A261, 0x774B695A, 0x121A161C, 0x93BA0AE2, 0xA02AE5C0, 0x22E0433C, 0x1B171D12, 0x090D0B0E, 0x8BC7ADF2, 0xB6A8B92D, 0x1EA9C814, 0xF1198557, 0x75074CAF, 0x99DDBBEE, 0x7F60FDA3, 0x01269FF7, 0x72F5BC5C, 0x663BC544, 0xFB7E345B, 0x4329768B, 0x23C6DCCB, 0xEDFC68B6, 0xE4F163B8, 0x31DCCAD7, 0x63851042, 0x97224013, 0xC6112084, 0x4A247D85, 0xBB3DF8D2, 0xF93211AE, 0x29A16DC7, 0x9E2F4B1D, 0xB230F3DC, 0x8652EC0D, 0xC1E3D077, 0xB3166C2B, 0x70B999A9, 0x9448FA11, 0xE9642247, 0xFC8CC4A8, 0xF03F1AA0, 0x7D2CD856, 0x3390EF22, 0x494EC787, 0x38D1C1D9, 0xCAA2FE8C, 0xD40B3698, 0xF581CFA6, 0x7ADE28A5, 0xB78E26DA, 0xADBFA43F, 0x3A9DE42C, 0x78920D50, 0x5FCC9B6A, 0x7E466254, 0x8D13C2F6, 0xD8B8E890, 0x39F75E2E, 0xC3AFF582, 0x5D80BE9F, 0xD0937C69, 0xD52DA96F, 0x2512B3CF, 0xAC993BC8, 0x187DA710, 0x9C636EE8, 0x3BBB7BDB, 0x267809CD, 0x5918F46E, 0x9AB701EC, 0x4F9AA883, 0x956E65E6, 0xFFE67EAA, 0xBCCF0821, 0x15E8E6EF, 0xE79BD9BA, 0x6F36CE4A, 0x9F09D4EA, 0xB07CD629, 0xA4B2AF31, 0x3F23312A, 0xA59430C6, 0xA266C035, 0x4EBC3774, 0x82CAA6FC, 0x90D0B0E0, 0xA7D81533, 0x04984AF1, 0xECDAF741, 0xCD500E7F, 0x91F62F17, 0x4DD68D76, 0xEFB04D43, 0xAA4D54CC, 0x9604DFE4, 0xD1B5E39E, 0x6A881B4C, 0x2C1FB8C1, 0x65517F46, 0x5EEA049D, 0x8C355D01, 0x877473FA, 0x0B412EFB, 0x671D5AB3, 0xDBD25292, 0x105633E9, 0xD647136D, 0xD7618C9A, 0xA10C7A37, 0xF8148E59, 0x133C89EB, 0xA927EECE, 0x61C935B7, 0x1CE5EDE1, 0x47B13C7A, 0xD2DF599C, 0xF2733F55, 0x14CE7918, 0xC737BF73, 0xF7CDEA53, 0xFDAA5B5F, 0x3D6F14DF, 0x44DB8678, 0xAFF381CA, 0x68C43EB9, 0x24342C38, 0xA3405FC2, 0x1DC37216, 0xE2250CBC, 0x3C498B28, 0x0D9541FF, 0xA8017139, 0x0CB3DE08, 0xB4E49CD8, 0x56C19064, 0xCB84617B, 0x32B670D5, 0x6C5C7448, 0xB85742D0 )); foreach ($dt3 as $dt3i) { $dt0[] = (($dt3i << 24) & 0xFF000000) | (($dt3i >> 8) & 0x00FFFFFF); $dt1[] = (($dt3i << 16) & 0xFFFF0000) | (($dt3i >> 16) & 0x0000FFFF); $dt2[] = (($dt3i << 8) & 0xFFFFFF00) | (($dt3i >> 24) & 0x000000FF); }; $tables = array( // The Precomputed inverse mixColumns tables dt0 - dt3 $dt0, $dt1, $dt2, $dt3, // The inverse SubByte S-Box array( 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E, 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84, 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73, 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D ) ); } return $tables; } /** * Setup the performance-optimized function for de/encrypt() * * @see \phpseclib\Crypt\Base::_setupInlineCrypt() * @access private */ function _setupInlineCrypt() { // Note: _setupInlineCrypt() will be called only if $this->changed === true // So here we are'nt under the same heavy timing-stress as we are in _de/encryptBlock() or de/encrypt(). // However...the here generated function- $code, stored as php callback in $this->inline_crypt, must work as fast as even possible. $lambda_functions =& self::_getLambdaFunctions(); // We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function. // (Currently, for Crypt_Rijndael/AES, one generated $lambda_function cost on php5.5@32bit ~80kb unfreeable mem and ~130kb on php5.5@64bit) // After that, we'll still create very fast optimized code but not the hi-ultimative code, for each $mode one. $gen_hi_opt_code = (bool)(count($lambda_functions) < 10); // Generation of a uniqe hash for our generated code $code_hash = "Crypt_Rijndael, {$this->mode}, {$this->Nr}, {$this->Nb}"; if ($gen_hi_opt_code) { $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key); } if (!isset($lambda_functions[$code_hash])) { switch (true) { case $gen_hi_opt_code: // The hi-optimized $lambda_functions will use the key-words hardcoded for better performance. $w = $this->w; $dw = $this->dw; $init_encrypt = ''; $init_decrypt = ''; break; default: for ($i = 0, $cw = count($this->w); $i < $cw; ++$i) { $w[] = '$w[' . $i . ']'; $dw[] = '$dw[' . $i . ']'; } $init_encrypt = '$w = $self->w;'; $init_decrypt = '$dw = $self->dw;'; } $Nr = $this->Nr; $Nb = $this->Nb; $c = $this->c; // Generating encrypt code: $init_encrypt.= ' static $tables; if (empty($tables)) { $tables = &$self->_getTables(); } $t0 = $tables[0]; $t1 = $tables[1]; $t2 = $tables[2]; $t3 = $tables[3]; $sbox = $tables[4]; '; $s = 'e'; $e = 's'; $wc = $Nb - 1; // Preround: addRoundKey $encrypt_block = '$in = unpack("N*", $in);'."\n"; for ($i = 0; $i < $Nb; ++$i) { $encrypt_block .= '$s'.$i.' = $in['.($i + 1).'] ^ '.$w[++$wc].";\n"; } // Mainrounds: shiftRows + subWord + mixColumns + addRoundKey for ($round = 1; $round < $Nr; ++$round) { list($s, $e) = array($e, $s); for ($i = 0; $i < $Nb; ++$i) { $encrypt_block.= '$'.$e.$i.' = $t0[($'.$s.$i .' >> 24) & 0xff] ^ $t1[($'.$s.(($i + $c[1]) % $Nb).' >> 16) & 0xff] ^ $t2[($'.$s.(($i + $c[2]) % $Nb).' >> 8) & 0xff] ^ $t3[ $'.$s.(($i + $c[3]) % $Nb).' & 0xff] ^ '.$w[++$wc].";\n"; } } // Finalround: subWord + shiftRows + addRoundKey for ($i = 0; $i < $Nb; ++$i) { $encrypt_block.= '$'.$e.$i.' = $sbox[ $'.$e.$i.' & 0xff] | ($sbox[($'.$e.$i.' >> 8) & 0xff] << 8) | ($sbox[($'.$e.$i.' >> 16) & 0xff] << 16) | ($sbox[($'.$e.$i.' >> 24) & 0xff] << 24);'."\n"; } $encrypt_block .= '$in = pack("N*"'."\n"; for ($i = 0; $i < $Nb; ++$i) { $encrypt_block.= ', ($'.$e.$i .' & '.((int)0xFF000000).') ^ ($'.$e.(($i + $c[1]) % $Nb).' & 0x00FF0000 ) ^ ($'.$e.(($i + $c[2]) % $Nb).' & 0x0000FF00 ) ^ ($'.$e.(($i + $c[3]) % $Nb).' & 0x000000FF ) ^ '.$w[$i]."\n"; } $encrypt_block .= ');'; // Generating decrypt code: $init_decrypt.= ' static $invtables; if (empty($invtables)) { $invtables = &$self->_getInvTables(); } $dt0 = $invtables[0]; $dt1 = $invtables[1]; $dt2 = $invtables[2]; $dt3 = $invtables[3]; $isbox = $invtables[4]; '; $s = 'e'; $e = 's'; $wc = $Nb - 1; // Preround: addRoundKey $decrypt_block = '$in = unpack("N*", $in);'."\n"; for ($i = 0; $i < $Nb; ++$i) { $decrypt_block .= '$s'.$i.' = $in['.($i + 1).'] ^ '.$dw[++$wc].';'."\n"; } // Mainrounds: shiftRows + subWord + mixColumns + addRoundKey for ($round = 1; $round < $Nr; ++$round) { list($s, $e) = array($e, $s); for ($i = 0; $i < $Nb; ++$i) { $decrypt_block.= '$'.$e.$i.' = $dt0[($'.$s.$i .' >> 24) & 0xff] ^ $dt1[($'.$s.(($Nb + $i - $c[1]) % $Nb).' >> 16) & 0xff] ^ $dt2[($'.$s.(($Nb + $i - $c[2]) % $Nb).' >> 8) & 0xff] ^ $dt3[ $'.$s.(($Nb + $i - $c[3]) % $Nb).' & 0xff] ^ '.$dw[++$wc].";\n"; } } // Finalround: subWord + shiftRows + addRoundKey for ($i = 0; $i < $Nb; ++$i) { $decrypt_block.= '$'.$e.$i.' = $isbox[ $'.$e.$i.' & 0xff] | ($isbox[($'.$e.$i.' >> 8) & 0xff] << 8) | ($isbox[($'.$e.$i.' >> 16) & 0xff] << 16) | ($isbox[($'.$e.$i.' >> 24) & 0xff] << 24);'."\n"; } $decrypt_block .= '$in = pack("N*"'."\n"; for ($i = 0; $i < $Nb; ++$i) { $decrypt_block.= ', ($'.$e.$i. ' & '.((int)0xFF000000).') ^ ($'.$e.(($Nb + $i - $c[1]) % $Nb).' & 0x00FF0000 ) ^ ($'.$e.(($Nb + $i - $c[2]) % $Nb).' & 0x0000FF00 ) ^ ($'.$e.(($Nb + $i - $c[3]) % $Nb).' & 0x000000FF ) ^ '.$dw[$i]."\n"; } $decrypt_block .= ');'; $lambda_functions[$code_hash] = $this->_createInlineCryptFunction( array( 'init_crypt' => '', 'init_encrypt' => $init_encrypt, 'init_decrypt' => $init_decrypt, 'encrypt_block' => $encrypt_block, 'decrypt_block' => $decrypt_block ) ); } $this->inline_crypt = $lambda_functions[$code_hash]; } } <?php /** * Pure-PHP PKCS#1 (v2.1) compliant implementation of RSA. * * PHP version 5 * * Here's an example of how to encrypt and decrypt text with this library: * <code> * <?php * include 'vendor/autoload.php'; * * $rsa = new \phpseclib\Crypt\RSA(); * extract($rsa->createKey()); * * $plaintext = 'terrafrost'; * * $rsa->loadKey($privatekey); * $ciphertext = $rsa->encrypt($plaintext); * * $rsa->loadKey($publickey); * echo $rsa->decrypt($ciphertext); * ?> * </code> * * Here's an example of how to create signatures and verify signatures with this library: * <code> * <?php * include 'vendor/autoload.php'; * * $rsa = new \phpseclib\Crypt\RSA(); * extract($rsa->createKey()); * * $plaintext = 'terrafrost'; * * $rsa->loadKey($privatekey); * $signature = $rsa->sign($plaintext); * * $rsa->loadKey($publickey); * echo $rsa->verify($plaintext, $signature) ? 'verified' : 'unverified'; * ?> * </code> * * @category Crypt * @package RSA * @author Jim Wigginton <terrafrost@php.net> * @copyright 2009 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; use phpseclib\Math\BigInteger; /** * Pure-PHP PKCS#1 compliant implementation of RSA. * * @package RSA * @author Jim Wigginton <terrafrost@php.net> * @access public */ class RSA { /**#@+ * @access public * @see self::encrypt() * @see self::decrypt() */ /** * Use {@link http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding Optimal Asymmetric Encryption Padding} * (OAEP) for encryption / decryption. * * Uses sha1 by default. * * @see self::setHash() * @see self::setMGFHash() */ const ENCRYPTION_OAEP = 1; /** * Use PKCS#1 padding. * * Although self::ENCRYPTION_OAEP offers more security, including PKCS#1 padding is necessary for purposes of backwards * compatibility with protocols (like SSH-1) written before OAEP's introduction. */ const ENCRYPTION_PKCS1 = 2; /** * Do not use any padding * * Although this method is not recommended it can none-the-less sometimes be useful if you're trying to decrypt some legacy * stuff, if you're trying to diagnose why an encrypted message isn't decrypting, etc. */ const ENCRYPTION_NONE = 3; /**#@-*/ /**#@+ * @access public * @see self::sign() * @see self::verify() * @see self::setHash() */ /** * Use the Probabilistic Signature Scheme for signing * * Uses sha1 by default. * * @see self::setSaltLength() * @see self::setMGFHash() */ const SIGNATURE_PSS = 1; /** * Use the PKCS#1 scheme by default. * * Although self::SIGNATURE_PSS offers more security, including PKCS#1 signing is necessary for purposes of backwards * compatibility with protocols (like SSH-2) written before PSS's introduction. */ const SIGNATURE_PKCS1 = 2; /**#@-*/ /**#@+ * @access private * @see \phpseclib\Crypt\RSA::createKey() */ /** * ASN1 Integer */ const ASN1_INTEGER = 2; /** * ASN1 Bit String */ const ASN1_BITSTRING = 3; /** * ASN1 Octet String */ const ASN1_OCTETSTRING = 4; /** * ASN1 Object Identifier */ const ASN1_OBJECT = 6; /** * ASN1 Sequence (with the constucted bit set) */ const ASN1_SEQUENCE = 48; /**#@-*/ /**#@+ * @access private * @see \phpseclib\Crypt\RSA::__construct() */ /** * To use the pure-PHP implementation */ const MODE_INTERNAL = 1; /** * To use the OpenSSL library * * (if enabled; otherwise, the internal implementation will be used) */ const MODE_OPENSSL = 2; /**#@-*/ /**#@+ * @access public * @see \phpseclib\Crypt\RSA::createKey() * @see \phpseclib\Crypt\RSA::setPrivateKeyFormat() */ /** * PKCS#1 formatted private key * * Used by OpenSSH */ const PRIVATE_FORMAT_PKCS1 = 0; /** * PuTTY formatted private key */ const PRIVATE_FORMAT_PUTTY = 1; /** * XML formatted private key */ const PRIVATE_FORMAT_XML = 2; /** * PKCS#8 formatted private key */ const PRIVATE_FORMAT_PKCS8 = 8; /** * OpenSSH formatted private key */ const PRIVATE_FORMAT_OPENSSH = 9; /**#@-*/ /**#@+ * @access public * @see \phpseclib\Crypt\RSA::createKey() * @see \phpseclib\Crypt\RSA::setPublicKeyFormat() */ /** * Raw public key * * An array containing two \phpseclib\Math\BigInteger objects. * * The exponent can be indexed with any of the following: * * 0, e, exponent, publicExponent * * The modulus can be indexed with any of the following: * * 1, n, modulo, modulus */ const PUBLIC_FORMAT_RAW = 3; /** * PKCS#1 formatted public key (raw) * * Used by File/X509.php * * Has the following header: * * -----BEGIN RSA PUBLIC KEY----- * * Analogous to ssh-keygen's pem format (as specified by -m) */ const PUBLIC_FORMAT_PKCS1 = 4; const PUBLIC_FORMAT_PKCS1_RAW = 4; /** * XML formatted public key */ const PUBLIC_FORMAT_XML = 5; /** * OpenSSH formatted public key * * Place in $HOME/.ssh/authorized_keys */ const PUBLIC_FORMAT_OPENSSH = 6; /** * PKCS#1 formatted public key (encapsulated) * * Used by PHP's openssl_public_encrypt() and openssl's rsautl (when -pubin is set) * * Has the following header: * * -----BEGIN PUBLIC KEY----- * * Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8 * is specific to private keys it's basically creating a DER-encoded wrapper * for keys. This just extends that same concept to public keys (much like ssh-keygen) */ const PUBLIC_FORMAT_PKCS8 = 7; /**#@-*/ /** * Precomputed Zero * * @var \phpseclib\Math\BigInteger * @access private */ var $zero; /** * Precomputed One * * @var \phpseclib\Math\BigInteger * @access private */ var $one; /** * Private Key Format * * @var int * @access private */ var $privateKeyFormat = self::PRIVATE_FORMAT_PKCS1; /** * Public Key Format * * @var int * @access public */ var $publicKeyFormat = self::PUBLIC_FORMAT_PKCS8; /** * Modulus (ie. n) * * @var \phpseclib\Math\BigInteger * @access private */ var $modulus; /** * Modulus length * * @var \phpseclib\Math\BigInteger * @access private */ var $k; /** * Exponent (ie. e or d) * * @var \phpseclib\Math\BigInteger * @access private */ var $exponent; /** * Primes for Chinese Remainder Theorem (ie. p and q) * * @var array * @access private */ var $primes; /** * Exponents for Chinese Remainder Theorem (ie. dP and dQ) * * @var array * @access private */ var $exponents; /** * Coefficients for Chinese Remainder Theorem (ie. qInv) * * @var array * @access private */ var $coefficients; /** * Hash name * * @var string * @access private */ var $hashName; /** * Hash function * * @var \phpseclib\Crypt\Hash * @access private */ var $hash; /** * Length of hash function output * * @var int * @access private */ var $hLen; /** * Length of salt * * @var int * @access private */ var $sLen; /** * Hash function for the Mask Generation Function * * @var \phpseclib\Crypt\Hash * @access private */ var $mgfHash; /** * Length of MGF hash function output * * @var int * @access private */ var $mgfHLen; /** * Encryption mode * * @var int * @access private */ var $encryptionMode = self::ENCRYPTION_OAEP; /** * Signature mode * * @var int * @access private */ var $signatureMode = self::SIGNATURE_PSS; /** * Public Exponent * * @var mixed * @access private */ var $publicExponent = false; /** * Password * * @var string * @access private */ var $password = false; /** * Components * * For use with parsing XML formatted keys. PHP's XML Parser functions use utilized - instead of PHP's DOM functions - * because PHP's XML Parser functions work on PHP4 whereas PHP's DOM functions - although surperior - don't. * * @see self::_start_element_handler() * @var array * @access private */ var $components = array(); /** * Current String * * For use with parsing XML formatted keys. * * @see self::_character_handler() * @see self::_stop_element_handler() * @var mixed * @access private */ var $current; /** * OpenSSL configuration file name. * * Set to null to use system configuration file. * @see self::createKey() * @var mixed * @Access public */ var $configFile; /** * Public key comment field. * * @var string * @access private */ var $comment = 'phpseclib-generated-key'; /** * The constructor * * If you want to make use of the openssl extension, you'll need to set the mode manually, yourself. The reason * \phpseclib\Crypt\RSA doesn't do it is because OpenSSL doesn't fail gracefully. openssl_pkey_new(), in particular, requires * openssl.cnf be present somewhere and, unfortunately, the only real way to find out is too late. * * @return \phpseclib\Crypt\RSA * @access public */ function __construct() { $this->configFile = dirname(__FILE__) . '/../openssl.cnf'; if (!defined('CRYPT_RSA_MODE')) { switch (true) { // Math/BigInteger's openssl requirements are a little less stringent than Crypt/RSA's. in particular, // Math/BigInteger doesn't require an openssl.cfg file whereas Crypt/RSA does. so if Math/BigInteger // can't use OpenSSL it can be pretty trivially assumed, then, that Crypt/RSA can't either. case defined('MATH_BIGINTEGER_OPENSSL_DISABLE'): define('CRYPT_RSA_MODE', self::MODE_INTERNAL); break; case extension_loaded('openssl') && file_exists($this->configFile): // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work $versions = array(); // avoid generating errors (even with suppression) when phpinfo() is disabled (common in production systems) if (strpos(ini_get('disable_functions'), 'phpinfo') === false) { ob_start(); @phpinfo(); $content = ob_get_contents(); ob_end_clean(); preg_match_all('#OpenSSL (Header|Library) Version(.*)#im', $content, $matches); if (!empty($matches[1])) { for ($i = 0; $i < count($matches[1]); $i++) { $fullVersion = trim(str_replace('=>', '', strip_tags($matches[2][$i]))); // Remove letter part in OpenSSL version if (!preg_match('/(\d+\.\d+\.\d+)/i', $fullVersion, $m)) { $versions[$matches[1][$i]] = $fullVersion; } else { $versions[$matches[1][$i]] = $m[0]; } } } } // it doesn't appear that OpenSSL versions were reported upon until PHP 5.3+ switch (true) { case !isset($versions['Header']): case !isset($versions['Library']): case $versions['Header'] == $versions['Library']: case version_compare($versions['Header'], '1.0.0') >= 0 && version_compare($versions['Library'], '1.0.0') >= 0: define('CRYPT_RSA_MODE', self::MODE_OPENSSL); break; default: define('CRYPT_RSA_MODE', self::MODE_INTERNAL); define('MATH_BIGINTEGER_OPENSSL_DISABLE', true); } break; default: define('CRYPT_RSA_MODE', self::MODE_INTERNAL); } } $this->zero = new BigInteger(); $this->one = new BigInteger(1); $this->hash = new Hash('sha1'); $this->hLen = $this->hash->getLength(); $this->hashName = 'sha1'; $this->mgfHash = new Hash('sha1'); $this->mgfHLen = $this->mgfHash->getLength(); } /** * Create public / private key pair * * Returns an array with the following three elements: * - 'privatekey': The private key. * - 'publickey': The public key. * - 'partialkey': A partially computed key (if the execution time exceeded $timeout). * Will need to be passed back to \phpseclib\Crypt\RSA::createKey() as the third parameter for further processing. * * @access public * @param int $bits * @param int $timeout * @param array $p */ function createKey($bits = 1024, $timeout = false, $partial = array()) { if (!defined('CRYPT_RSA_EXPONENT')) { // http://en.wikipedia.org/wiki/65537_%28number%29 define('CRYPT_RSA_EXPONENT', '65537'); } // per <http://cseweb.ucsd.edu/~hovav/dist/survey.pdf#page=5>, this number ought not result in primes smaller // than 256 bits. as a consequence if the key you're trying to create is 1024 bits and you've set CRYPT_RSA_SMALLEST_PRIME // to 384 bits then you're going to get a 384 bit prime and a 640 bit prime (384 + 1024 % 384). at least if // CRYPT_RSA_MODE is set to self::MODE_INTERNAL. if CRYPT_RSA_MODE is set to self::MODE_OPENSSL then // CRYPT_RSA_SMALLEST_PRIME is ignored (ie. multi-prime RSA support is more intended as a way to speed up RSA key // generation when there's a chance neither gmp nor OpenSSL are installed) if (!defined('CRYPT_RSA_SMALLEST_PRIME')) { define('CRYPT_RSA_SMALLEST_PRIME', 4096); } // OpenSSL uses 65537 as the exponent and requires RSA keys be 384 bits minimum if (CRYPT_RSA_MODE == self::MODE_OPENSSL && $bits >= 384 && CRYPT_RSA_EXPONENT == 65537) { $config = array(); if (isset($this->configFile)) { $config['config'] = $this->configFile; } $rsa = openssl_pkey_new(array('private_key_bits' => $bits) + $config); openssl_pkey_export($rsa, $privatekey, null, $config); $publickey = openssl_pkey_get_details($rsa); $publickey = $publickey['key']; $privatekey = call_user_func_array(array($this, '_convertPrivateKey'), array_values($this->_parseKey($privatekey, self::PRIVATE_FORMAT_PKCS1))); $publickey = call_user_func_array(array($this, '_convertPublicKey'), array_values($this->_parseKey($publickey, self::PUBLIC_FORMAT_PKCS1))); // clear the buffer of error strings stemming from a minimalistic openssl.cnf while (openssl_error_string() !== false) { } return array( 'privatekey' => $privatekey, 'publickey' => $publickey, 'partialkey' => false ); } static $e; if (!isset($e)) { $e = new BigInteger(CRYPT_RSA_EXPONENT); } extract($this->_generateMinMax($bits)); $absoluteMin = $min; $temp = $bits >> 1; // divide by two to see how many bits P and Q would be if ($temp > CRYPT_RSA_SMALLEST_PRIME) { $num_primes = floor($bits / CRYPT_RSA_SMALLEST_PRIME); $temp = CRYPT_RSA_SMALLEST_PRIME; } else { $num_primes = 2; } extract($this->_generateMinMax($temp + $bits % $temp)); $finalMax = $max; extract($this->_generateMinMax($temp)); $generator = new BigInteger(); $n = $this->one->copy(); if (!empty($partial)) { extract(unserialize($partial)); } else { $exponents = $coefficients = $primes = array(); $lcm = array( 'top' => $this->one->copy(), 'bottom' => false ); } $start = time(); $i0 = count($primes) + 1; do { for ($i = $i0; $i <= $num_primes; $i++) { if ($timeout !== false) { $timeout-= time() - $start; $start = time(); if ($timeout <= 0) { return array( 'privatekey' => '', 'publickey' => '', 'partialkey' => serialize(array( 'primes' => $primes, 'coefficients' => $coefficients, 'lcm' => $lcm, 'exponents' => $exponents )) ); } } if ($i == $num_primes) { list($min, $temp) = $absoluteMin->divide($n); if (!$temp->equals($this->zero)) { $min = $min->add($this->one); // ie. ceil() } $primes[$i] = $generator->randomPrime($min, $finalMax, $timeout); } else { $primes[$i] = $generator->randomPrime($min, $max, $timeout); } if ($primes[$i] === false) { // if we've reached the timeout if (count($primes) > 1) { $partialkey = ''; } else { array_pop($primes); $partialkey = serialize(array( 'primes' => $primes, 'coefficients' => $coefficients, 'lcm' => $lcm, 'exponents' => $exponents )); } return array( 'privatekey' => '', 'publickey' => '', 'partialkey' => $partialkey ); } // the first coefficient is calculated differently from the rest // ie. instead of being $primes[1]->modInverse($primes[2]), it's $primes[2]->modInverse($primes[1]) if ($i > 2) { $coefficients[$i] = $n->modInverse($primes[$i]); } $n = $n->multiply($primes[$i]); $temp = $primes[$i]->subtract($this->one); // textbook RSA implementations use Euler's totient function instead of the least common multiple. // see http://en.wikipedia.org/wiki/Euler%27s_totient_function $lcm['top'] = $lcm['top']->multiply($temp); $lcm['bottom'] = $lcm['bottom'] === false ? $temp : $lcm['bottom']->gcd($temp); $exponents[$i] = $e->modInverse($temp); } list($temp) = $lcm['top']->divide($lcm['bottom']); $gcd = $temp->gcd($e); $i0 = 1; } while (!$gcd->equals($this->one)); $d = $e->modInverse($temp); $coefficients[2] = $primes[2]->modInverse($primes[1]); // from <http://tools.ietf.org/html/rfc3447#appendix-A.1.2>: // RSAPrivateKey ::= SEQUENCE { // version Version, // modulus INTEGER, -- n // publicExponent INTEGER, -- e // privateExponent INTEGER, -- d // prime1 INTEGER, -- p // prime2 INTEGER, -- q // exponent1 INTEGER, -- d mod (p-1) // exponent2 INTEGER, -- d mod (q-1) // coefficient INTEGER, -- (inverse of q) mod p // otherPrimeInfos OtherPrimeInfos OPTIONAL // } return array( 'privatekey' => $this->_convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients), 'publickey' => $this->_convertPublicKey($n, $e), 'partialkey' => false ); } /** * Convert a private key to the appropriate format. * * @access private * @see self::setPrivateKeyFormat() * @param string $RSAPrivateKey * @return string */ function _convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients) { $signed = $this->privateKeyFormat != self::PRIVATE_FORMAT_XML; $num_primes = count($primes); $raw = array( 'version' => $num_primes == 2 ? chr(0) : chr(1), // two-prime vs. multi 'modulus' => $n->toBytes($signed), 'publicExponent' => $e->toBytes($signed), 'privateExponent' => $d->toBytes($signed), 'prime1' => $primes[1]->toBytes($signed), 'prime2' => $primes[2]->toBytes($signed), 'exponent1' => $exponents[1]->toBytes($signed), 'exponent2' => $exponents[2]->toBytes($signed), 'coefficient' => $coefficients[2]->toBytes($signed) ); // if the format in question does not support multi-prime rsa and multi-prime rsa was used, // call _convertPublicKey() instead. switch ($this->privateKeyFormat) { case self::PRIVATE_FORMAT_XML: if ($num_primes != 2) { return false; } return "<RSAKeyValue>\r\n" . ' <Modulus>' . base64_encode($raw['modulus']) . "</Modulus>\r\n" . ' <Exponent>' . base64_encode($raw['publicExponent']) . "</Exponent>\r\n" . ' <P>' . base64_encode($raw['prime1']) . "</P>\r\n" . ' <Q>' . base64_encode($raw['prime2']) . "</Q>\r\n" . ' <DP>' . base64_encode($raw['exponent1']) . "</DP>\r\n" . ' <DQ>' . base64_encode($raw['exponent2']) . "</DQ>\r\n" . ' <InverseQ>' . base64_encode($raw['coefficient']) . "</InverseQ>\r\n" . ' <D>' . base64_encode($raw['privateExponent']) . "</D>\r\n" . '</RSAKeyValue>'; break; case self::PRIVATE_FORMAT_PUTTY: if ($num_primes != 2) { return false; } $key = "PuTTY-User-Key-File-2: ssh-rsa\r\nEncryption: "; $encryption = (!empty($this->password) || is_string($this->password)) ? 'aes256-cbc' : 'none'; $key.= $encryption; $key.= "\r\nComment: " . $this->comment . "\r\n"; $public = pack( 'Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($raw['publicExponent']), $raw['publicExponent'], strlen($raw['modulus']), $raw['modulus'] ); $source = pack( 'Na*Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($encryption), $encryption, strlen($this->comment), $this->comment, strlen($public), $public ); $public = base64_encode($public); $key.= "Public-Lines: " . ((strlen($public) + 63) >> 6) . "\r\n"; $key.= chunk_split($public, 64); $private = pack( 'Na*Na*Na*Na*', strlen($raw['privateExponent']), $raw['privateExponent'], strlen($raw['prime1']), $raw['prime1'], strlen($raw['prime2']), $raw['prime2'], strlen($raw['coefficient']), $raw['coefficient'] ); if (empty($this->password) && !is_string($this->password)) { $source.= pack('Na*', strlen($private), $private); $hashkey = 'putty-private-key-file-mac-key'; } else { $private.= Random::string(16 - (strlen($private) & 15)); $source.= pack('Na*', strlen($private), $private); $sequence = 0; $symkey = ''; while (strlen($symkey) < 32) { $temp = pack('Na*', $sequence++, $this->password); $symkey.= pack('H*', sha1($temp)); } $symkey = substr($symkey, 0, 32); $crypto = new AES(); $crypto->setKey($symkey); $crypto->disablePadding(); $private = $crypto->encrypt($private); $hashkey = 'putty-private-key-file-mac-key' . $this->password; } $private = base64_encode($private); $key.= 'Private-Lines: ' . ((strlen($private) + 63) >> 6) . "\r\n"; $key.= chunk_split($private, 64); $hash = new Hash('sha1'); $hash->setKey(pack('H*', sha1($hashkey))); $key.= 'Private-MAC: ' . bin2hex($hash->hash($source)) . "\r\n"; return $key; case self::PRIVATE_FORMAT_OPENSSH: if ($num_primes != 2) { return false; } $publicKey = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($raw['publicExponent']), $raw['publicExponent'], strlen($raw['modulus']), $raw['modulus']); $privateKey = pack( 'Na*Na*Na*Na*Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($raw['modulus']), $raw['modulus'], strlen($raw['publicExponent']), $raw['publicExponent'], strlen($raw['privateExponent']), $raw['privateExponent'], strlen($raw['coefficient']), $raw['coefficient'], strlen($raw['prime1']), $raw['prime1'], strlen($raw['prime2']), $raw['prime2'] ); $checkint = Random::string(4); $paddedKey = pack( 'a*Na*', $checkint . $checkint . $privateKey, strlen($this->comment), $this->comment ); $paddingLength = (7 * strlen($paddedKey)) % 8; for ($i = 1; $i <= $paddingLength; $i++) { $paddedKey.= chr($i); } $key = pack( 'Na*Na*Na*NNa*Na*', strlen('none'), 'none', strlen('none'), 'none', 0, '', 1, strlen($publicKey), $publicKey, strlen($paddedKey), $paddedKey ); $key = "openssh-key-v1\0$key"; return "-----BEGIN OPENSSH PRIVATE KEY-----\r\n" . chunk_split(base64_encode($key), 70) . "-----END OPENSSH PRIVATE KEY-----"; default: // eg. self::PRIVATE_FORMAT_PKCS1 $components = array(); foreach ($raw as $name => $value) { $components[$name] = pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($value)), $value); } $RSAPrivateKey = implode('', $components); if ($num_primes > 2) { $OtherPrimeInfos = ''; for ($i = 3; $i <= $num_primes; $i++) { // OtherPrimeInfos ::= SEQUENCE SIZE(1..MAX) OF OtherPrimeInfo // // OtherPrimeInfo ::= SEQUENCE { // prime INTEGER, -- ri // exponent INTEGER, -- di // coefficient INTEGER -- ti // } $OtherPrimeInfo = pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($primes[$i]->toBytes(true))), $primes[$i]->toBytes(true)); $OtherPrimeInfo.= pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($exponents[$i]->toBytes(true))), $exponents[$i]->toBytes(true)); $OtherPrimeInfo.= pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($coefficients[$i]->toBytes(true))), $coefficients[$i]->toBytes(true)); $OtherPrimeInfos.= pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($OtherPrimeInfo)), $OtherPrimeInfo); } $RSAPrivateKey.= pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($OtherPrimeInfos)), $OtherPrimeInfos); } $RSAPrivateKey = pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey); if ($this->privateKeyFormat == self::PRIVATE_FORMAT_PKCS8) { $rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA $RSAPrivateKey = pack( 'Ca*a*Ca*a*', self::ASN1_INTEGER, "\01\00", $rsaOID, 4, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey ); $RSAPrivateKey = pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey); if (!empty($this->password) || is_string($this->password)) { $salt = Random::string(8); $iterationCount = 2048; $crypto = new DES(); $crypto->setPassword($this->password, 'pbkdf1', 'md5', $salt, $iterationCount); $RSAPrivateKey = $crypto->encrypt($RSAPrivateKey); $parameters = pack( 'Ca*a*Ca*N', self::ASN1_OCTETSTRING, $this->_encodeLength(strlen($salt)), $salt, self::ASN1_INTEGER, $this->_encodeLength(4), $iterationCount ); $pbeWithMD5AndDES_CBC = "\x2a\x86\x48\x86\xf7\x0d\x01\x05\x03"; $encryptionAlgorithm = pack( 'Ca*a*Ca*a*', self::ASN1_OBJECT, $this->_encodeLength(strlen($pbeWithMD5AndDES_CBC)), $pbeWithMD5AndDES_CBC, self::ASN1_SEQUENCE, $this->_encodeLength(strlen($parameters)), $parameters ); $RSAPrivateKey = pack( 'Ca*a*Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($encryptionAlgorithm)), $encryptionAlgorithm, self::ASN1_OCTETSTRING, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey ); $RSAPrivateKey = pack('Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey); $RSAPrivateKey = "-----BEGIN ENCRYPTED PRIVATE KEY-----\r\n" . chunk_split(base64_encode($RSAPrivateKey), 64) . '-----END ENCRYPTED PRIVATE KEY-----'; } else { $RSAPrivateKey = "-----BEGIN PRIVATE KEY-----\r\n" . chunk_split(base64_encode($RSAPrivateKey), 64) . '-----END PRIVATE KEY-----'; } return $RSAPrivateKey; } if (!empty($this->password) || is_string($this->password)) { $iv = Random::string(8); $symkey = pack('H*', md5($this->password . $iv)); // symkey is short for symmetric key $symkey.= substr(pack('H*', md5($symkey . $this->password . $iv)), 0, 8); $des = new TripleDES(); $des->setKey($symkey); $des->setIV($iv); $iv = strtoupper(bin2hex($iv)); $RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" . "Proc-Type: 4,ENCRYPTED\r\n" . "DEK-Info: DES-EDE3-CBC,$iv\r\n" . "\r\n" . chunk_split(base64_encode($des->encrypt($RSAPrivateKey)), 64) . '-----END RSA PRIVATE KEY-----'; } else { $RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" . chunk_split(base64_encode($RSAPrivateKey), 64) . '-----END RSA PRIVATE KEY-----'; } return $RSAPrivateKey; } } /** * Convert a public key to the appropriate format * * @access private * @see self::setPublicKeyFormat() * @param string $RSAPrivateKey * @return string */ function _convertPublicKey($n, $e) { $signed = $this->publicKeyFormat != self::PUBLIC_FORMAT_XML; $modulus = $n->toBytes($signed); $publicExponent = $e->toBytes($signed); switch ($this->publicKeyFormat) { case self::PUBLIC_FORMAT_RAW: return array('e' => $e->copy(), 'n' => $n->copy()); case self::PUBLIC_FORMAT_XML: return "<RSAKeyValue>\r\n" . ' <Modulus>' . base64_encode($modulus) . "</Modulus>\r\n" . ' <Exponent>' . base64_encode($publicExponent) . "</Exponent>\r\n" . '</RSAKeyValue>'; break; case self::PUBLIC_FORMAT_OPENSSH: // from <http://tools.ietf.org/html/rfc4253#page-15>: // string "ssh-rsa" // mpint e // mpint n $RSAPublicKey = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publicExponent), $publicExponent, strlen($modulus), $modulus); $RSAPublicKey = 'ssh-rsa ' . base64_encode($RSAPublicKey) . ' ' . $this->comment; return $RSAPublicKey; default: // eg. self::PUBLIC_FORMAT_PKCS1_RAW or self::PUBLIC_FORMAT_PKCS1 // from <http://tools.ietf.org/html/rfc3447#appendix-A.1.1>: // RSAPublicKey ::= SEQUENCE { // modulus INTEGER, -- n // publicExponent INTEGER -- e // } $components = array( 'modulus' => pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($modulus)), $modulus), 'publicExponent' => pack('Ca*a*', self::ASN1_INTEGER, $this->_encodeLength(strlen($publicExponent)), $publicExponent) ); $RSAPublicKey = pack( 'Ca*a*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($components['modulus']) + strlen($components['publicExponent'])), $components['modulus'], $components['publicExponent'] ); if ($this->publicKeyFormat == self::PUBLIC_FORMAT_PKCS1_RAW) { $RSAPublicKey = "-----BEGIN RSA PUBLIC KEY-----\r\n" . chunk_split(base64_encode($RSAPublicKey), 64) . '-----END RSA PUBLIC KEY-----'; } else { // sequence(oid(1.2.840.113549.1.1.1), null)) = rsaEncryption. $rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA $RSAPublicKey = chr(0) . $RSAPublicKey; $RSAPublicKey = chr(3) . $this->_encodeLength(strlen($RSAPublicKey)) . $RSAPublicKey; $RSAPublicKey = pack( 'Ca*a*', self::ASN1_SEQUENCE, $this->_encodeLength(strlen($rsaOID . $RSAPublicKey)), $rsaOID . $RSAPublicKey ); $RSAPublicKey = "-----BEGIN PUBLIC KEY-----\r\n" . chunk_split(base64_encode($RSAPublicKey), 64) . '-----END PUBLIC KEY-----'; } return $RSAPublicKey; } } /** * Break a public or private key down into its constituant components * * @access private * @see self::_convertPublicKey() * @see self::_convertPrivateKey() * @param string|array $key * @param int $type * @return array|bool */ function _parseKey($key, $type) { if ($type != self::PUBLIC_FORMAT_RAW && !is_string($key)) { return false; } switch ($type) { case self::PUBLIC_FORMAT_RAW: if (!is_array($key)) { return false; } $components = array(); switch (true) { case isset($key['e']): $components['publicExponent'] = $key['e']->copy(); break; case isset($key['exponent']): $components['publicExponent'] = $key['exponent']->copy(); break; case isset($key['publicExponent']): $components['publicExponent'] = $key['publicExponent']->copy(); break; case isset($key[0]): $components['publicExponent'] = $key[0]->copy(); } switch (true) { case isset($key['n']): $components['modulus'] = $key['n']->copy(); break; case isset($key['modulo']): $components['modulus'] = $key['modulo']->copy(); break; case isset($key['modulus']): $components['modulus'] = $key['modulus']->copy(); break; case isset($key[1]): $components['modulus'] = $key[1]->copy(); } return isset($components['modulus']) && isset($components['publicExponent']) ? $components : false; case self::PRIVATE_FORMAT_PKCS1: case self::PRIVATE_FORMAT_PKCS8: case self::PUBLIC_FORMAT_PKCS1: /* Although PKCS#1 proposes a format that public and private keys can use, encrypting them is "outside the scope" of PKCS#1. PKCS#1 then refers you to PKCS#12 and PKCS#15 if you're wanting to protect private keys, however, that's not what OpenSSL* does. OpenSSL protects private keys by adding two new "fields" to the key - DEK-Info and Proc-Type. These fields are discussed here: http://tools.ietf.org/html/rfc1421#section-4.6.1.1 http://tools.ietf.org/html/rfc1421#section-4.6.1.3 DES-EDE3-CBC as an algorithm, however, is not discussed anywhere, near as I can tell. DES-CBC and DES-EDE are discussed in RFC1423, however, DES-EDE3-CBC isn't, nor is its key derivation function. As is, the definitive authority on this encoding scheme isn't the IETF but rather OpenSSL's own implementation. ie. the implementation *is* the standard and any bugs that may exist in that implementation are part of the standard, as well. * OpenSSL is the de facto standard. It's utilized by OpenSSH and other projects */ if (preg_match('#DEK-Info: (.+),(.+)#', $key, $matches)) { $iv = pack('H*', trim($matches[2])); $symkey = pack('H*', md5($this->password . substr($iv, 0, 8))); // symkey is short for symmetric key $symkey.= pack('H*', md5($symkey . $this->password . substr($iv, 0, 8))); // remove the Proc-Type / DEK-Info sections as they're no longer needed $key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $key); $ciphertext = $this->_extractBER($key); if ($ciphertext === false) { $ciphertext = $key; } switch ($matches[1]) { case 'AES-256-CBC': $crypto = new AES(); break; case 'AES-128-CBC': $symkey = substr($symkey, 0, 16); $crypto = new AES(); break; case 'DES-EDE3-CFB': $crypto = new TripleDES(Base::MODE_CFB); break; case 'DES-EDE3-CBC': $symkey = substr($symkey, 0, 24); $crypto = new TripleDES(); break; case 'DES-CBC': $crypto = new DES(); break; default: return false; } $crypto->setKey($symkey); $crypto->setIV($iv); $decoded = $crypto->decrypt($ciphertext); } else { $decoded = $this->_extractBER($key); } if ($decoded !== false) { $key = $decoded; } $components = array(); if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) { return false; } if ($this->_decodeLength($key) != strlen($key)) { return false; } $tag = ord($this->_string_shift($key)); /* intended for keys for which OpenSSL's asn1parse returns the following: 0:d=0 hl=4 l= 631 cons: SEQUENCE 4:d=1 hl=2 l= 1 prim: INTEGER :00 7:d=1 hl=2 l= 13 cons: SEQUENCE 9:d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption 20:d=2 hl=2 l= 0 prim: NULL 22:d=1 hl=4 l= 609 prim: OCTET STRING ie. PKCS8 keys*/ if ($tag == self::ASN1_INTEGER && substr($key, 0, 3) == "\x01\x00\x30") { $this->_string_shift($key, 3); $tag = self::ASN1_SEQUENCE; } if ($tag == self::ASN1_SEQUENCE) { $temp = $this->_string_shift($key, $this->_decodeLength($key)); if (ord($this->_string_shift($temp)) != self::ASN1_OBJECT) { return false; } $length = $this->_decodeLength($temp); switch ($this->_string_shift($temp, $length)) { case "\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01": // rsaEncryption break; case "\x2a\x86\x48\x86\xf7\x0d\x01\x05\x03": // pbeWithMD5AndDES-CBC /* PBEParameter ::= SEQUENCE { salt OCTET STRING (SIZE(8)), iterationCount INTEGER } */ if (ord($this->_string_shift($temp)) != self::ASN1_SEQUENCE) { return false; } if ($this->_decodeLength($temp) != strlen($temp)) { return false; } $this->_string_shift($temp); // assume it's an octet string $salt = $this->_string_shift($temp, $this->_decodeLength($temp)); if (ord($this->_string_shift($temp)) != self::ASN1_INTEGER) { return false; } $this->_decodeLength($temp); list(, $iterationCount) = unpack('N', str_pad($temp, 4, chr(0), STR_PAD_LEFT)); $this->_string_shift($key); // assume it's an octet string $length = $this->_decodeLength($key); if (strlen($key) != $length) { return false; } $crypto = new DES(); $crypto->setPassword($this->password, 'pbkdf1', 'md5', $salt, $iterationCount); $key = $crypto->decrypt($key); if ($key === false) { return false; } return $this->_parseKey($key, self::PRIVATE_FORMAT_PKCS1); default: return false; } /* intended for keys for which OpenSSL's asn1parse returns the following: 0:d=0 hl=4 l= 290 cons: SEQUENCE 4:d=1 hl=2 l= 13 cons: SEQUENCE 6:d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption 17:d=2 hl=2 l= 0 prim: NULL 19:d=1 hl=4 l= 271 prim: BIT STRING */ $tag = ord($this->_string_shift($key)); // skip over the BIT STRING / OCTET STRING tag $this->_decodeLength($key); // skip over the BIT STRING / OCTET STRING length // "The initial octet shall encode, as an unsigned binary integer wtih bit 1 as the least significant bit, the number of // unused bits in the final subsequent octet. The number shall be in the range zero to seven." // -- http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf (section 8.6.2.2) if ($tag == self::ASN1_BITSTRING) { $this->_string_shift($key); } if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) { return false; } if ($this->_decodeLength($key) != strlen($key)) { return false; } $tag = ord($this->_string_shift($key)); } if ($tag != self::ASN1_INTEGER) { return false; } $length = $this->_decodeLength($key); $temp = $this->_string_shift($key, $length); if (strlen($temp) != 1 || ord($temp) > 2) { $components['modulus'] = new BigInteger($temp, 256); $this->_string_shift($key); // skip over self::ASN1_INTEGER $length = $this->_decodeLength($key); $components[$type == self::PUBLIC_FORMAT_PKCS1 ? 'publicExponent' : 'privateExponent'] = new BigInteger($this->_string_shift($key, $length), 256); return $components; } if (ord($this->_string_shift($key)) != self::ASN1_INTEGER) { return false; } $length = $this->_decodeLength($key); $components['modulus'] = new BigInteger($this->_string_shift($key, $length), 256); $this->_string_shift($key); $length = $this->_decodeLength($key); $components['publicExponent'] = new BigInteger($this->_string_shift($key, $length), 256); $this->_string_shift($key); $length = $this->_decodeLength($key); $components['privateExponent'] = new BigInteger($this->_string_shift($key, $length), 256); $this->_string_shift($key); $length = $this->_decodeLength($key); $components['primes'] = array(1 => new BigInteger($this->_string_shift($key, $length), 256)); $this->_string_shift($key); $length = $this->_decodeLength($key); $components['primes'][] = new BigInteger($this->_string_shift($key, $length), 256); $this->_string_shift($key); $length = $this->_decodeLength($key); $components['exponents'] = array(1 => new BigInteger($this->_string_shift($key, $length), 256)); $this->_string_shift($key); $length = $this->_decodeLength($key); $components['exponents'][] = new BigInteger($this->_string_shift($key, $length), 256); $this->_string_shift($key); $length = $this->_decodeLength($key); $components['coefficients'] = array(2 => new BigInteger($this->_string_shift($key, $length), 256)); if (!empty($key)) { if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) { return false; } $this->_decodeLength($key); while (!empty($key)) { if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) { return false; } $this->_decodeLength($key); $key = substr($key, 1); $length = $this->_decodeLength($key); $components['primes'][] = new BigInteger($this->_string_shift($key, $length), 256); $this->_string_shift($key); $length = $this->_decodeLength($key); $components['exponents'][] = new BigInteger($this->_string_shift($key, $length), 256); $this->_string_shift($key); $length = $this->_decodeLength($key); $components['coefficients'][] = new BigInteger($this->_string_shift($key, $length), 256); } } return $components; case self::PUBLIC_FORMAT_OPENSSH: $parts = explode(' ', $key, 3); $key = isset($parts[1]) ? base64_decode($parts[1]) : false; if ($key === false) { return false; } $comment = isset($parts[2]) ? $parts[2] : false; $cleanup = substr($key, 0, 11) == "\0\0\0\7ssh-rsa"; if (strlen($key) <= 4) { return false; } extract(unpack('Nlength', $this->_string_shift($key, 4))); $publicExponent = new BigInteger($this->_string_shift($key, $length), -256); if (strlen($key) <= 4) { return false; } extract(unpack('Nlength', $this->_string_shift($key, 4))); $modulus = new BigInteger($this->_string_shift($key, $length), -256); if ($cleanup && strlen($key)) { if (strlen($key) <= 4) { return false; } extract(unpack('Nlength', $this->_string_shift($key, 4))); $realModulus = new BigInteger($this->_string_shift($key, $length), -256); return strlen($key) ? false : array( 'modulus' => $realModulus, 'publicExponent' => $modulus, 'comment' => $comment ); } else { return strlen($key) ? false : array( 'modulus' => $modulus, 'publicExponent' => $publicExponent, 'comment' => $comment ); } // http://www.w3.org/TR/xmldsig-core/#sec-RSAKeyValue // http://en.wikipedia.org/wiki/XML_Signature case self::PRIVATE_FORMAT_XML: case self::PUBLIC_FORMAT_XML: $this->components = array(); $xml = xml_parser_create('UTF-8'); xml_set_object($xml, $this); xml_set_element_handler($xml, '_start_element_handler', '_stop_element_handler'); xml_set_character_data_handler($xml, '_data_handler'); // add <xml></xml> to account for "dangling" tags like <BitStrength>...</BitStrength> that are sometimes added if (!xml_parse($xml, '<xml>' . $key . '</xml>')) { xml_parser_free($xml); unset($xml); return false; } xml_parser_free($xml); unset($xml); return isset($this->components['modulus']) && isset($this->components['publicExponent']) ? $this->components : false; // from PuTTY's SSHPUBK.C case self::PRIVATE_FORMAT_PUTTY: $components = array(); $key = preg_split('#\r\n|\r|\n#', $key); $type = trim(preg_replace('#PuTTY-User-Key-File-2: (.+)#', '$1', $key[0])); if ($type != 'ssh-rsa') { return false; } $encryption = trim(preg_replace('#Encryption: (.+)#', '$1', $key[1])); $comment = trim(preg_replace('#Comment: (.+)#', '$1', $key[2])); $publicLength = trim(preg_replace('#Public-Lines: (\d+)#', '$1', $key[3])); $public = base64_decode(implode('', array_map('trim', array_slice($key, 4, $publicLength)))); $public = substr($public, 11); extract(unpack('Nlength', $this->_string_shift($public, 4))); $components['publicExponent'] = new BigInteger($this->_string_shift($public, $length), -256); extract(unpack('Nlength', $this->_string_shift($public, 4))); $components['modulus'] = new BigInteger($this->_string_shift($public, $length), -256); $privateLength = trim(preg_replace('#Private-Lines: (\d+)#', '$1', $key[$publicLength + 4])); $private = base64_decode(implode('', array_map('trim', array_slice($key, $publicLength + 5, $privateLength)))); switch ($encryption) { case 'aes256-cbc': $symkey = ''; $sequence = 0; while (strlen($symkey) < 32) { $temp = pack('Na*', $sequence++, $this->password); $symkey.= pack('H*', sha1($temp)); } $symkey = substr($symkey, 0, 32); $crypto = new AES(); } if ($encryption != 'none') { $crypto->setKey($symkey); $crypto->disablePadding(); $private = $crypto->decrypt($private); if ($private === false) { return false; } } extract(unpack('Nlength', $this->_string_shift($private, 4))); if (strlen($private) < $length) { return false; } $components['privateExponent'] = new BigInteger($this->_string_shift($private, $length), -256); extract(unpack('Nlength', $this->_string_shift($private, 4))); if (strlen($private) < $length) { return false; } $components['primes'] = array(1 => new BigInteger($this->_string_shift($private, $length), -256)); extract(unpack('Nlength', $this->_string_shift($private, 4))); if (strlen($private) < $length) { return false; } $components['primes'][] = new BigInteger($this->_string_shift($private, $length), -256); $temp = $components['primes'][1]->subtract($this->one); $components['exponents'] = array(1 => $components['publicExponent']->modInverse($temp)); $temp = $components['primes'][2]->subtract($this->one); $components['exponents'][] = $components['publicExponent']->modInverse($temp); extract(unpack('Nlength', $this->_string_shift($private, 4))); if (strlen($private) < $length) { return false; } $components['coefficients'] = array(2 => new BigInteger($this->_string_shift($private, $length), -256)); return $components; case self::PRIVATE_FORMAT_OPENSSH: $components = array(); $decoded = $this->_extractBER($key); $magic = $this->_string_shift($decoded, 15); if ($magic !== "openssh-key-v1\0") { return false; } $options = $this->_string_shift($decoded, 24); // \0\0\0\4none = ciphername // \0\0\0\4none = kdfname // \0\0\0\0 = kdfoptions // \0\0\0\1 = numkeys if ($options != "\0\0\0\4none\0\0\0\4none\0\0\0\0\0\0\0\1") { return false; } extract(unpack('Nlength', $this->_string_shift($decoded, 4))); if (strlen($decoded) < $length) { return false; } $publicKey = $this->_string_shift($decoded, $length); extract(unpack('Nlength', $this->_string_shift($decoded, 4))); if (strlen($decoded) < $length) { return false; } $paddedKey = $this->_string_shift($decoded, $length); if ($this->_string_shift($publicKey, 11) !== "\0\0\0\7ssh-rsa") { return false; } $checkint1 = $this->_string_shift($paddedKey, 4); $checkint2 = $this->_string_shift($paddedKey, 4); if (strlen($checkint1) != 4 || $checkint1 !== $checkint2) { return false; } if ($this->_string_shift($paddedKey, 11) !== "\0\0\0\7ssh-rsa") { return false; } $values = array( &$components['modulus'], &$components['publicExponent'], &$components['privateExponent'], &$components['coefficients'][2], &$components['primes'][1], &$components['primes'][2] ); foreach ($values as &$value) { extract(unpack('Nlength', $this->_string_shift($paddedKey, 4))); if (strlen($paddedKey) < $length) { return false; } $value = new BigInteger($this->_string_shift($paddedKey, $length), -256); } extract(unpack('Nlength', $this->_string_shift($paddedKey, 4))); if (strlen($paddedKey) < $length) { return false; } $components['comment'] = $this->_string_shift($decoded, $length); $temp = $components['primes'][1]->subtract($this->one); $components['exponents'] = array(1 => $components['publicExponent']->modInverse($temp)); $temp = $components['primes'][2]->subtract($this->one); $components['exponents'][] = $components['publicExponent']->modInverse($temp); return $components; } } /** * Returns the key size * * More specifically, this returns the size of the modulo in bits. * * @access public * @return int */ function getSize() { return !isset($this->modulus) ? 0 : strlen($this->modulus->toBits()); } /** * Start Element Handler * * Called by xml_set_element_handler() * * @access private * @param resource $parser * @param string $name * @param array $attribs */ function _start_element_handler($parser, $name, $attribs) { //$name = strtoupper($name); switch ($name) { case 'MODULUS': $this->current = &$this->components['modulus']; break; case 'EXPONENT': $this->current = &$this->components['publicExponent']; break; case 'P': $this->current = &$this->components['primes'][1]; break; case 'Q': $this->current = &$this->components['primes'][2]; break; case 'DP': $this->current = &$this->components['exponents'][1]; break; case 'DQ': $this->current = &$this->components['exponents'][2]; break; case 'INVERSEQ': $this->current = &$this->components['coefficients'][2]; break; case 'D': $this->current = &$this->components['privateExponent']; } $this->current = ''; } /** * Stop Element Handler * * Called by xml_set_element_handler() * * @access private * @param resource $parser * @param string $name */ function _stop_element_handler($parser, $name) { if (isset($this->current)) { $this->current = new BigInteger(base64_decode($this->current), 256); unset($this->current); } } /** * Data Handler * * Called by xml_set_character_data_handler() * * @access private * @param resource $parser * @param string $data */ function _data_handler($parser, $data) { if (!isset($this->current) || is_object($this->current)) { return; } $this->current.= trim($data); } /** * Loads a public or private key * * Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed) * * @access public * @param string|RSA|array $key * @param bool|int $type optional * @return bool */ function loadKey($key, $type = false) { if ($key instanceof RSA) { $this->privateKeyFormat = $key->privateKeyFormat; $this->publicKeyFormat = $key->publicKeyFormat; $this->k = $key->k; $this->hLen = $key->hLen; $this->sLen = $key->sLen; $this->mgfHLen = $key->mgfHLen; $this->encryptionMode = $key->encryptionMode; $this->signatureMode = $key->signatureMode; $this->password = $key->password; $this->configFile = $key->configFile; $this->comment = $key->comment; if (is_object($key->hash)) { $this->hash = new Hash($key->hash->getHash()); } if (is_object($key->mgfHash)) { $this->mgfHash = new Hash($key->mgfHash->getHash()); } if (is_object($key->modulus)) { $this->modulus = $key->modulus->copy(); } if (is_object($key->exponent)) { $this->exponent = $key->exponent->copy(); } if (is_object($key->publicExponent)) { $this->publicExponent = $key->publicExponent->copy(); } $this->primes = array(); $this->exponents = array(); $this->coefficients = array(); foreach ($this->primes as $prime) { $this->primes[] = $prime->copy(); } foreach ($this->exponents as $exponent) { $this->exponents[] = $exponent->copy(); } foreach ($this->coefficients as $coefficient) { $this->coefficients[] = $coefficient->copy(); } return true; } if ($type === false) { $types = array( self::PUBLIC_FORMAT_RAW, self::PRIVATE_FORMAT_PKCS1, self::PRIVATE_FORMAT_XML, self::PRIVATE_FORMAT_PUTTY, self::PUBLIC_FORMAT_OPENSSH, self::PRIVATE_FORMAT_OPENSSH ); foreach ($types as $type) { $components = $this->_parseKey($key, $type); if ($components !== false) { break; } } } else { $components = $this->_parseKey($key, $type); } if ($components === false) { $this->comment = null; $this->modulus = null; $this->k = null; $this->exponent = null; $this->primes = null; $this->exponents = null; $this->coefficients = null; $this->publicExponent = null; return false; } if (isset($components['comment']) && $components['comment'] !== false) { $this->comment = $components['comment']; } $this->modulus = $components['modulus']; $this->k = strlen($this->modulus->toBytes()); $this->exponent = isset($components['privateExponent']) ? $components['privateExponent'] : $components['publicExponent']; if (isset($components['primes'])) { $this->primes = $components['primes']; $this->exponents = $components['exponents']; $this->coefficients = $components['coefficients']; $this->publicExponent = $components['publicExponent']; } else { $this->primes = array(); $this->exponents = array(); $this->coefficients = array(); $this->publicExponent = false; } switch ($type) { case self::PUBLIC_FORMAT_OPENSSH: case self::PUBLIC_FORMAT_RAW: $this->setPublicKey(); break; case self::PRIVATE_FORMAT_PKCS1: switch (true) { case strpos($key, '-BEGIN PUBLIC KEY-') !== false: case strpos($key, '-BEGIN RSA PUBLIC KEY-') !== false: $this->setPublicKey(); } } return true; } /** * Sets the password * * Private keys can be encrypted with a password. To unset the password, pass in the empty string or false. * Or rather, pass in $password such that empty($password) && !is_string($password) is true. * * @see self::createKey() * @see self::loadKey() * @access public * @param string $password */ function setPassword($password = false) { $this->password = $password; } /** * Defines the public key * * Some private key formats define the public exponent and some don't. Those that don't define it are problematic when * used in certain contexts. For example, in SSH-2, RSA authentication works by sending the public key along with a * message signed by the private key to the server. The SSH-2 server looks the public key up in an index of public keys * and if it's present then proceeds to verify the signature. Problem is, if your private key doesn't include the public * exponent this won't work unless you manually add the public exponent. phpseclib tries to guess if the key being used * is the public key but in the event that it guesses incorrectly you might still want to explicitly set the key as being * public. * * Do note that when a new key is loaded the index will be cleared. * * Returns true on success, false on failure * * @see self::getPublicKey() * @access public * @param string $key optional * @param int $type optional * @return bool */ function setPublicKey($key = false, $type = false) { // if a public key has already been loaded return false if (!empty($this->publicExponent)) { return false; } if ($key === false && !empty($this->modulus)) { $this->publicExponent = $this->exponent; return true; } if ($type === false) { $types = array( self::PUBLIC_FORMAT_RAW, self::PUBLIC_FORMAT_PKCS1, self::PUBLIC_FORMAT_XML, self::PUBLIC_FORMAT_OPENSSH ); foreach ($types as $type) { $components = $this->_parseKey($key, $type); if ($components !== false) { break; } } } else { $components = $this->_parseKey($key, $type); } if ($components === false) { return false; } if (empty($this->modulus) || !$this->modulus->equals($components['modulus'])) { $this->modulus = $components['modulus']; $this->exponent = $this->publicExponent = $components['publicExponent']; return true; } $this->publicExponent = $components['publicExponent']; return true; } /** * Defines the private key * * If phpseclib guessed a private key was a public key and loaded it as such it might be desirable to force * phpseclib to treat the key as a private key. This function will do that. * * Do note that when a new key is loaded the index will be cleared. * * Returns true on success, false on failure * * @see self::getPublicKey() * @access public * @param string $key optional * @param int $type optional * @return bool */ function setPrivateKey($key = false, $type = false) { if ($key === false && !empty($this->publicExponent)) { $this->publicExponent = false; return true; } $rsa = new RSA(); if (!$rsa->loadKey($key, $type)) { return false; } $rsa->publicExponent = false; // don't overwrite the old key if the new key is invalid $this->loadKey($rsa); return true; } /** * Returns the public key * * The public key is only returned under two circumstances - if the private key had the public key embedded within it * or if the public key was set via setPublicKey(). If the currently loaded key is supposed to be the public key this * function won't return it since this library, for the most part, doesn't distinguish between public and private keys. * * @see self::getPublicKey() * @access public * @param string $key * @param int $type optional */ function getPublicKey($type = self::PUBLIC_FORMAT_PKCS8) { if (empty($this->modulus) || empty($this->publicExponent)) { return false; } $oldFormat = $this->publicKeyFormat; $this->publicKeyFormat = $type; $temp = $this->_convertPublicKey($this->modulus, $this->publicExponent); $this->publicKeyFormat = $oldFormat; return $temp; } /** * Returns the public key's fingerprint * * The public key's fingerprint is returned, which is equivalent to running `ssh-keygen -lf rsa.pub`. If there is * no public key currently loaded, false is returned. * Example output (md5): "c1:b1:30:29:d7:b8:de:6c:97:77:10:d7:46:41:63:87" (as specified by RFC 4716) * * @access public * @param string $algorithm The hashing algorithm to be used. Valid options are 'md5' and 'sha256'. False is returned * for invalid values. * @return mixed */ function getPublicKeyFingerprint($algorithm = 'md5') { if (empty($this->modulus) || empty($this->publicExponent)) { return false; } $modulus = $this->modulus->toBytes(true); $publicExponent = $this->publicExponent->toBytes(true); $RSAPublicKey = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publicExponent), $publicExponent, strlen($modulus), $modulus); switch ($algorithm) { case 'sha256': $hash = new Hash('sha256'); $base = base64_encode($hash->hash($RSAPublicKey)); return substr($base, 0, strlen($base) - 1); case 'md5': return substr(chunk_split(md5($RSAPublicKey), 2, ':'), 0, -1); default: return false; } } /** * Returns the private key * * The private key is only returned if the currently loaded key contains the constituent prime numbers. * * @see self::getPublicKey() * @access public * @param string $key * @param int $type optional * @return mixed */ function getPrivateKey($type = self::PUBLIC_FORMAT_PKCS1) { if (empty($this->primes)) { return false; } $oldFormat = $this->privateKeyFormat; $this->privateKeyFormat = $type; $temp = $this->_convertPrivateKey($this->modulus, $this->publicExponent, $this->exponent, $this->primes, $this->exponents, $this->coefficients); $this->privateKeyFormat = $oldFormat; return $temp; } /** * Returns a minimalistic private key * * Returns the private key without the prime number constituants. Structurally identical to a public key that * hasn't been set as the public key * * @see self::getPrivateKey() * @access private * @param string $key * @param int $type optional */ function _getPrivatePublicKey($mode = self::PUBLIC_FORMAT_PKCS8) { if (empty($this->modulus) || empty($this->exponent)) { return false; } $oldFormat = $this->publicKeyFormat; $this->publicKeyFormat = $mode; $temp = $this->_convertPublicKey($this->modulus, $this->exponent); $this->publicKeyFormat = $oldFormat; return $temp; } /** * __toString() magic method * * @access public * @return string */ function __toString() { $key = $this->getPrivateKey($this->privateKeyFormat); if ($key !== false) { return $key; } $key = $this->_getPrivatePublicKey($this->publicKeyFormat); return $key !== false ? $key : ''; } /** * __clone() magic method * * @access public * @return Crypt_RSA */ function __clone() { $key = new RSA(); $key->loadKey($this); return $key; } /** * Generates the smallest and largest numbers requiring $bits bits * * @access private * @param int $bits * @return array */ function _generateMinMax($bits) { $bytes = $bits >> 3; $min = str_repeat(chr(0), $bytes); $max = str_repeat(chr(0xFF), $bytes); $msb = $bits & 7; if ($msb) { $min = chr(1 << ($msb - 1)) . $min; $max = chr((1 << $msb) - 1) . $max; } else { $min[0] = chr(0x80); } return array( 'min' => new BigInteger($min, 256), 'max' => new BigInteger($max, 256) ); } /** * DER-decode the length * * DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See * {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information. * * @access private * @param string $string * @return int */ function _decodeLength(&$string) { $length = ord($this->_string_shift($string)); if ($length & 0x80) { // definite length, long form $length&= 0x7F; $temp = $this->_string_shift($string, $length); list(, $length) = unpack('N', substr(str_pad($temp, 4, chr(0), STR_PAD_LEFT), -4)); } return $length; } /** * DER-encode the length * * DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See * {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information. * * @access private * @param int $length * @return string */ function _encodeLength($length) { if ($length <= 0x7F) { return chr($length); } $temp = ltrim(pack('N', $length), chr(0)); return pack('Ca*', 0x80 | strlen($temp), $temp); } /** * String Shift * * Inspired by array_shift * * @param string $string * @param int $index * @return string * @access private */ function _string_shift(&$string, $index = 1) { $substr = substr($string, 0, $index); $string = substr($string, $index); return $substr; } /** * Determines the private key format * * @see self::createKey() * @access public * @param int $format */ function setPrivateKeyFormat($format) { $this->privateKeyFormat = $format; } /** * Determines the public key format * * @see self::createKey() * @access public * @param int $format */ function setPublicKeyFormat($format) { $this->publicKeyFormat = $format; } /** * Determines which hashing function should be used * * Used with signature production / verification and (if the encryption mode is self::ENCRYPTION_OAEP) encryption and * decryption. If $hash isn't supported, sha1 is used. * * @access public * @param string $hash */ function setHash($hash) { // \phpseclib\Crypt\Hash supports algorithms that PKCS#1 doesn't support. md5-96 and sha1-96, for example. switch ($hash) { case 'md2': case 'md5': case 'sha1': case 'sha256': case 'sha384': case 'sha512': $this->hash = new Hash($hash); $this->hashName = $hash; break; default: $this->hash = new Hash('sha1'); $this->hashName = 'sha1'; } $this->hLen = $this->hash->getLength(); } /** * Determines which hashing function should be used for the mask generation function * * The mask generation function is used by self::ENCRYPTION_OAEP and self::SIGNATURE_PSS and although it's * best if Hash and MGFHash are set to the same thing this is not a requirement. * * @access public * @param string $hash */ function setMGFHash($hash) { // \phpseclib\Crypt\Hash supports algorithms that PKCS#1 doesn't support. md5-96 and sha1-96, for example. switch ($hash) { case 'md2': case 'md5': case 'sha1': case 'sha256': case 'sha384': case 'sha512': $this->mgfHash = new Hash($hash); break; default: $this->mgfHash = new Hash('sha1'); } $this->mgfHLen = $this->mgfHash->getLength(); } /** * Determines the salt length * * To quote from {@link http://tools.ietf.org/html/rfc3447#page-38 RFC3447#page-38}: * * Typical salt lengths in octets are hLen (the length of the output * of the hash function Hash) and 0. * * @access public * @param int $format */ function setSaltLength($sLen) { $this->sLen = $sLen; } /** * Integer-to-Octet-String primitive * * See {@link http://tools.ietf.org/html/rfc3447#section-4.1 RFC3447#section-4.1}. * * @access private * @param \phpseclib\Math\BigInteger $x * @param int $xLen * @return string */ function _i2osp($x, $xLen) { $x = $x->toBytes(); if (strlen($x) > $xLen) { user_error('Integer too large'); return false; } return str_pad($x, $xLen, chr(0), STR_PAD_LEFT); } /** * Octet-String-to-Integer primitive * * See {@link http://tools.ietf.org/html/rfc3447#section-4.2 RFC3447#section-4.2}. * * @access private * @param string $x * @return \phpseclib\Math\BigInteger */ function _os2ip($x) { return new BigInteger($x, 256); } /** * Exponentiate with or without Chinese Remainder Theorem * * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.2}. * * @access private * @param \phpseclib\Math\BigInteger $x * @return \phpseclib\Math\BigInteger */ function _exponentiate($x) { switch (true) { case empty($this->primes): case $this->primes[1]->equals($this->zero): case empty($this->coefficients): case $this->coefficients[2]->equals($this->zero): case empty($this->exponents): case $this->exponents[1]->equals($this->zero): return $x->modPow($this->exponent, $this->modulus); } $num_primes = count($this->primes); if (defined('CRYPT_RSA_DISABLE_BLINDING')) { $m_i = array( 1 => $x->modPow($this->exponents[1], $this->primes[1]), 2 => $x->modPow($this->exponents[2], $this->primes[2]) ); $h = $m_i[1]->subtract($m_i[2]); $h = $h->multiply($this->coefficients[2]); list(, $h) = $h->divide($this->primes[1]); $m = $m_i[2]->add($h->multiply($this->primes[2])); $r = $this->primes[1]; for ($i = 3; $i <= $num_primes; $i++) { $m_i = $x->modPow($this->exponents[$i], $this->primes[$i]); $r = $r->multiply($this->primes[$i - 1]); $h = $m_i->subtract($m); $h = $h->multiply($this->coefficients[$i]); list(, $h) = $h->divide($this->primes[$i]); $m = $m->add($r->multiply($h)); } } else { $smallest = $this->primes[1]; for ($i = 2; $i <= $num_primes; $i++) { if ($smallest->compare($this->primes[$i]) > 0) { $smallest = $this->primes[$i]; } } $one = new BigInteger(1); $r = $one->random($one, $smallest->subtract($one)); $m_i = array( 1 => $this->_blind($x, $r, 1), 2 => $this->_blind($x, $r, 2) ); $h = $m_i[1]->subtract($m_i[2]); $h = $h->multiply($this->coefficients[2]); list(, $h) = $h->divide($this->primes[1]); $m = $m_i[2]->add($h->multiply($this->primes[2])); $r = $this->primes[1]; for ($i = 3; $i <= $num_primes; $i++) { $m_i = $this->_blind($x, $r, $i); $r = $r->multiply($this->primes[$i - 1]); $h = $m_i->subtract($m); $h = $h->multiply($this->coefficients[$i]); list(, $h) = $h->divide($this->primes[$i]); $m = $m->add($r->multiply($h)); } } return $m; } /** * Performs RSA Blinding * * Protects against timing attacks by employing RSA Blinding. * Returns $x->modPow($this->exponents[$i], $this->primes[$i]) * * @access private * @param \phpseclib\Math\BigInteger $x * @param \phpseclib\Math\BigInteger $r * @param int $i * @return \phpseclib\Math\BigInteger */ function _blind($x, $r, $i) { $x = $x->multiply($r->modPow($this->publicExponent, $this->primes[$i])); $x = $x->modPow($this->exponents[$i], $this->primes[$i]); $r = $r->modInverse($this->primes[$i]); $x = $x->multiply($r); list(, $x) = $x->divide($this->primes[$i]); return $x; } /** * Performs blinded RSA equality testing * * Protects against a particular type of timing attack described. * * See {@link http://codahale.com/a-lesson-in-timing-attacks/ A Lesson In Timing Attacks (or, Don't use MessageDigest.isEquals)} * * Thanks for the heads up singpolyma! * * @access private * @param string $x * @param string $y * @return bool */ function _equals($x, $y) { if (function_exists('hash_equals')) { return hash_equals($x, $y); } if (strlen($x) != strlen($y)) { return false; } $result = "\0"; $x^= $y; for ($i = 0; $i < strlen($x); $i++) { $result|= $x[$i]; } return $result === "\0"; } /** * RSAEP * * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.1}. * * @access private * @param \phpseclib\Math\BigInteger $m * @return \phpseclib\Math\BigInteger */ function _rsaep($m) { if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) { user_error('Message representative out of range'); return false; } return $this->_exponentiate($m); } /** * RSADP * * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.2 RFC3447#section-5.1.2}. * * @access private * @param \phpseclib\Math\BigInteger $c * @return \phpseclib\Math\BigInteger */ function _rsadp($c) { if ($c->compare($this->zero) < 0 || $c->compare($this->modulus) > 0) { user_error('Ciphertext representative out of range'); return false; } return $this->_exponentiate($c); } /** * RSASP1 * * See {@link http://tools.ietf.org/html/rfc3447#section-5.2.1 RFC3447#section-5.2.1}. * * @access private * @param \phpseclib\Math\BigInteger $m * @return \phpseclib\Math\BigInteger */ function _rsasp1($m) { if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) { user_error('Message representative out of range'); return false; } return $this->_exponentiate($m); } /** * RSAVP1 * * See {@link http://tools.ietf.org/html/rfc3447#section-5.2.2 RFC3447#section-5.2.2}. * * @access private * @param \phpseclib\Math\BigInteger $s * @return \phpseclib\Math\BigInteger */ function _rsavp1($s) { if ($s->compare($this->zero) < 0 || $s->compare($this->modulus) > 0) { user_error('Signature representative out of range'); return false; } return $this->_exponentiate($s); } /** * MGF1 * * See {@link http://tools.ietf.org/html/rfc3447#appendix-B.2.1 RFC3447#appendix-B.2.1}. * * @access private * @param string $mgfSeed * @param int $mgfLen * @return string */ function _mgf1($mgfSeed, $maskLen) { // if $maskLen would yield strings larger than 4GB, PKCS#1 suggests a "Mask too long" error be output. $t = ''; $count = ceil($maskLen / $this->mgfHLen); for ($i = 0; $i < $count; $i++) { $c = pack('N', $i); $t.= $this->mgfHash->hash($mgfSeed . $c); } return substr($t, 0, $maskLen); } /** * RSAES-OAEP-ENCRYPT * * See {@link http://tools.ietf.org/html/rfc3447#section-7.1.1 RFC3447#section-7.1.1} and * {http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding OAES}. * * @access private * @param string $m * @param string $l * @return string */ function _rsaes_oaep_encrypt($m, $l = '') { $mLen = strlen($m); // Length checking // if $l is larger than two million terrabytes and you're using sha1, PKCS#1 suggests a "Label too long" error // be output. if ($mLen > $this->k - 2 * $this->hLen - 2) { user_error('Message too long'); return false; } // EME-OAEP encoding $lHash = $this->hash->hash($l); $ps = str_repeat(chr(0), $this->k - $mLen - 2 * $this->hLen - 2); $db = $lHash . $ps . chr(1) . $m; $seed = Random::string($this->hLen); $dbMask = $this->_mgf1($seed, $this->k - $this->hLen - 1); $maskedDB = $db ^ $dbMask; $seedMask = $this->_mgf1($maskedDB, $this->hLen); $maskedSeed = $seed ^ $seedMask; $em = chr(0) . $maskedSeed . $maskedDB; // RSA encryption $m = $this->_os2ip($em); $c = $this->_rsaep($m); $c = $this->_i2osp($c, $this->k); // Output the ciphertext C return $c; } /** * RSAES-OAEP-DECRYPT * * See {@link http://tools.ietf.org/html/rfc3447#section-7.1.2 RFC3447#section-7.1.2}. The fact that the error * messages aren't distinguishable from one another hinders debugging, but, to quote from RFC3447#section-7.1.2: * * Note. Care must be taken to ensure that an opponent cannot * distinguish the different error conditions in Step 3.g, whether by * error message or timing, or, more generally, learn partial * information about the encoded message EM. Otherwise an opponent may * be able to obtain useful information about the decryption of the * ciphertext C, leading to a chosen-ciphertext attack such as the one * observed by Manger [36]. * * As for $l... to quote from {@link http://tools.ietf.org/html/rfc3447#page-17 RFC3447#page-17}: * * Both the encryption and the decryption operations of RSAES-OAEP take * the value of a label L as input. In this version of PKCS #1, L is * the empty string; other uses of the label are outside the scope of * this document. * * @access private * @param string $c * @param string $l * @return string */ function _rsaes_oaep_decrypt($c, $l = '') { // Length checking // if $l is larger than two million terrabytes and you're using sha1, PKCS#1 suggests a "Label too long" error // be output. if (strlen($c) != $this->k || $this->k < 2 * $this->hLen + 2) { user_error('Decryption error'); return false; } // RSA decryption $c = $this->_os2ip($c); $m = $this->_rsadp($c); if ($m === false) { user_error('Decryption error'); return false; } $em = $this->_i2osp($m, $this->k); // EME-OAEP decoding $lHash = $this->hash->hash($l); $y = ord($em[0]); $maskedSeed = substr($em, 1, $this->hLen); $maskedDB = substr($em, $this->hLen + 1); $seedMask = $this->_mgf1($maskedDB, $this->hLen); $seed = $maskedSeed ^ $seedMask; $dbMask = $this->_mgf1($seed, $this->k - $this->hLen - 1); $db = $maskedDB ^ $dbMask; $lHash2 = substr($db, 0, $this->hLen); $m = substr($db, $this->hLen); $hashesMatch = $this->_equals($lHash, $lHash2); $leadingZeros = 1; $patternMatch = 0; $offset = 0; for ($i = 0; $i < strlen($m); $i++) { $patternMatch|= $leadingZeros & ($m[$i] === "\1"); $leadingZeros&= $m[$i] === "\0"; $offset+= $patternMatch ? 0 : 1; } // we do & instead of && to avoid https://en.wikipedia.org/wiki/Short-circuit_evaluation // to protect against timing attacks if (!$hashesMatch & !$patternMatch) { user_error('Decryption error'); return false; } // Output the message M return substr($m, $offset + 1); } /** * Raw Encryption / Decryption * * Doesn't use padding and is not recommended. * * @access private * @param string $m * @return string */ function _raw_encrypt($m) { $temp = $this->_os2ip($m); $temp = $this->_rsaep($temp); return $this->_i2osp($temp, $this->k); } /** * RSAES-PKCS1-V1_5-ENCRYPT * * See {@link http://tools.ietf.org/html/rfc3447#section-7.2.1 RFC3447#section-7.2.1}. * * @access private * @param string $m * @return string */ function _rsaes_pkcs1_v1_5_encrypt($m) { $mLen = strlen($m); // Length checking if ($mLen > $this->k - 11) { user_error('Message too long'); return false; } // EME-PKCS1-v1_5 encoding $psLen = $this->k - $mLen - 3; $ps = ''; while (strlen($ps) != $psLen) { $temp = Random::string($psLen - strlen($ps)); $temp = str_replace("\x00", '', $temp); $ps.= $temp; } $type = 2; // see the comments of _rsaes_pkcs1_v1_5_decrypt() to understand why this is being done if (defined('CRYPT_RSA_PKCS15_COMPAT') && (!isset($this->publicExponent) || $this->exponent !== $this->publicExponent)) { $type = 1; // "The padding string PS shall consist of k-3-||D|| octets. ... for block type 01, they shall have value FF" $ps = str_repeat("\xFF", $psLen); } $em = chr(0) . chr($type) . $ps . chr(0) . $m; // RSA encryption $m = $this->_os2ip($em); $c = $this->_rsaep($m); $c = $this->_i2osp($c, $this->k); // Output the ciphertext C return $c; } /** * RSAES-PKCS1-V1_5-DECRYPT * * See {@link http://tools.ietf.org/html/rfc3447#section-7.2.2 RFC3447#section-7.2.2}. * * For compatibility purposes, this function departs slightly from the description given in RFC3447. * The reason being that RFC2313#section-8.1 (PKCS#1 v1.5) states that ciphertext's encrypted by the * private key should have the second byte set to either 0 or 1 and that ciphertext's encrypted by the * public key should have the second byte set to 2. In RFC3447 (PKCS#1 v2.1), the second byte is supposed * to be 2 regardless of which key is used. For compatibility purposes, we'll just check to make sure the * second byte is 2 or less. If it is, we'll accept the decrypted string as valid. * * As a consequence of this, a private key encrypted ciphertext produced with \phpseclib\Crypt\RSA may not decrypt * with a strictly PKCS#1 v1.5 compliant RSA implementation. Public key encrypted ciphertext's should but * not private key encrypted ciphertext's. * * @access private * @param string $c * @return string */ function _rsaes_pkcs1_v1_5_decrypt($c) { // Length checking if (strlen($c) != $this->k) { // or if k < 11 user_error('Decryption error'); return false; } // RSA decryption $c = $this->_os2ip($c); $m = $this->_rsadp($c); if ($m === false) { user_error('Decryption error'); return false; } $em = $this->_i2osp($m, $this->k); // EME-PKCS1-v1_5 decoding if (ord($em[0]) != 0 || ord($em[1]) > 2) { user_error('Decryption error'); return false; } $ps = substr($em, 2, strpos($em, chr(0), 2) - 2); $m = substr($em, strlen($ps) + 3); if (strlen($ps) < 8) { user_error('Decryption error'); return false; } // Output M return $m; } /** * EMSA-PSS-ENCODE * * See {@link http://tools.ietf.org/html/rfc3447#section-9.1.1 RFC3447#section-9.1.1}. * * @access private * @param string $m * @param int $emBits */ function _emsa_pss_encode($m, $emBits) { // if $m is larger than two million terrabytes and you're using sha1, PKCS#1 suggests a "Label too long" error // be output. $emLen = ($emBits + 1) >> 3; // ie. ceil($emBits / 8) $sLen = $this->sLen !== null ? $this->sLen : $this->hLen; $mHash = $this->hash->hash($m); if ($emLen < $this->hLen + $sLen + 2) { user_error('Encoding error'); return false; } $salt = Random::string($sLen); $m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt; $h = $this->hash->hash($m2); $ps = str_repeat(chr(0), $emLen - $sLen - $this->hLen - 2); $db = $ps . chr(1) . $salt; $dbMask = $this->_mgf1($h, $emLen - $this->hLen - 1); $maskedDB = $db ^ $dbMask; $maskedDB[0] = ~chr(0xFF << ($emBits & 7)) & $maskedDB[0]; $em = $maskedDB . $h . chr(0xBC); return $em; } /** * EMSA-PSS-VERIFY * * See {@link http://tools.ietf.org/html/rfc3447#section-9.1.2 RFC3447#section-9.1.2}. * * @access private * @param string $m * @param string $em * @param int $emBits * @return string */ function _emsa_pss_verify($m, $em, $emBits) { // if $m is larger than two million terrabytes and you're using sha1, PKCS#1 suggests a "Label too long" error // be output. $emLen = ($emBits + 1) >> 3; // ie. ceil($emBits / 8); $sLen = $this->sLen !== null ? $this->sLen : $this->hLen; $mHash = $this->hash->hash($m); if ($emLen < $this->hLen + $sLen + 2) { return false; } if ($em[strlen($em) - 1] != chr(0xBC)) { return false; } $maskedDB = substr($em, 0, -$this->hLen - 1); $h = substr($em, -$this->hLen - 1, $this->hLen); $temp = chr(0xFF << ($emBits & 7)); if ((~$maskedDB[0] & $temp) != $temp) { return false; } $dbMask = $this->_mgf1($h, $emLen - $this->hLen - 1); $db = $maskedDB ^ $dbMask; $db[0] = ~chr(0xFF << ($emBits & 7)) & $db[0]; $temp = $emLen - $this->hLen - $sLen - 2; if (substr($db, 0, $temp) != str_repeat(chr(0), $temp) || ord($db[$temp]) != 1) { return false; } $salt = substr($db, $temp + 1); // should be $sLen long $m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt; $h2 = $this->hash->hash($m2); return $this->_equals($h, $h2); } /** * RSASSA-PSS-SIGN * * See {@link http://tools.ietf.org/html/rfc3447#section-8.1.1 RFC3447#section-8.1.1}. * * @access private * @param string $m * @return string */ function _rsassa_pss_sign($m) { // EMSA-PSS encoding $em = $this->_emsa_pss_encode($m, 8 * $this->k - 1); // RSA signature $m = $this->_os2ip($em); $s = $this->_rsasp1($m); $s = $this->_i2osp($s, $this->k); // Output the signature S return $s; } /** * RSASSA-PSS-VERIFY * * See {@link http://tools.ietf.org/html/rfc3447#section-8.1.2 RFC3447#section-8.1.2}. * * @access private * @param string $m * @param string $s * @return string */ function _rsassa_pss_verify($m, $s) { // Length checking if (strlen($s) != $this->k) { user_error('Invalid signature'); return false; } // RSA verification $modBits = 8 * $this->k; $s2 = $this->_os2ip($s); $m2 = $this->_rsavp1($s2); if ($m2 === false) { user_error('Invalid signature'); return false; } $em = $this->_i2osp($m2, $modBits >> 3); if ($em === false) { user_error('Invalid signature'); return false; } // EMSA-PSS verification return $this->_emsa_pss_verify($m, $em, $modBits - 1); } /** * EMSA-PKCS1-V1_5-ENCODE * * See {@link http://tools.ietf.org/html/rfc3447#section-9.2 RFC3447#section-9.2}. * * @access private * @param string $m * @param int $emLen * @return string */ function _emsa_pkcs1_v1_5_encode($m, $emLen) { $h = $this->hash->hash($m); if ($h === false) { return false; } // see http://tools.ietf.org/html/rfc3447#page-43 switch ($this->hashName) { case 'md2': $t = pack('H*', '3020300c06082a864886f70d020205000410'); break; case 'md5': $t = pack('H*', '3020300c06082a864886f70d020505000410'); break; case 'sha1': $t = pack('H*', '3021300906052b0e03021a05000414'); break; case 'sha256': $t = pack('H*', '3031300d060960864801650304020105000420'); break; case 'sha384': $t = pack('H*', '3041300d060960864801650304020205000430'); break; case 'sha512': $t = pack('H*', '3051300d060960864801650304020305000440'); } $t.= $h; $tLen = strlen($t); if ($emLen < $tLen + 11) { user_error('Intended encoded message length too short'); return false; } $ps = str_repeat(chr(0xFF), $emLen - $tLen - 3); $em = "\0\1$ps\0$t"; return $em; } /** * RSASSA-PKCS1-V1_5-SIGN * * See {@link http://tools.ietf.org/html/rfc3447#section-8.2.1 RFC3447#section-8.2.1}. * * @access private * @param string $m * @return string */ function _rsassa_pkcs1_v1_5_sign($m) { // EMSA-PKCS1-v1_5 encoding $em = $this->_emsa_pkcs1_v1_5_encode($m, $this->k); if ($em === false) { user_error('RSA modulus too short'); return false; } // RSA signature $m = $this->_os2ip($em); $s = $this->_rsasp1($m); $s = $this->_i2osp($s, $this->k); // Output the signature S return $s; } /** * RSASSA-PKCS1-V1_5-VERIFY * * See {@link http://tools.ietf.org/html/rfc3447#section-8.2.2 RFC3447#section-8.2.2}. * * @access private * @param string $m * @return string */ function _rsassa_pkcs1_v1_5_verify($m, $s) { // Length checking if (strlen($s) != $this->k) { user_error('Invalid signature'); return false; } // RSA verification $s = $this->_os2ip($s); $m2 = $this->_rsavp1($s); if ($m2 === false) { user_error('Invalid signature'); return false; } $em = $this->_i2osp($m2, $this->k); if ($em === false) { user_error('Invalid signature'); return false; } // EMSA-PKCS1-v1_5 encoding $em2 = $this->_emsa_pkcs1_v1_5_encode($m, $this->k); if ($em2 === false) { user_error('RSA modulus too short'); return false; } // Compare return $this->_equals($em, $em2); } /** * Set Encryption Mode * * Valid values include self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1. * * @access public * @param int $mode */ function setEncryptionMode($mode) { $this->encryptionMode = $mode; } /** * Set Signature Mode * * Valid values include self::SIGNATURE_PSS and self::SIGNATURE_PKCS1 * * @access public * @param int $mode */ function setSignatureMode($mode) { $this->signatureMode = $mode; } /** * Set public key comment. * * @access public * @param string $comment */ function setComment($comment) { $this->comment = $comment; } /** * Get public key comment. * * @access public * @return string */ function getComment() { return $this->comment; } /** * Encryption * * Both self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. * If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will * be concatenated together. * * @see self::decrypt() * @access public * @param string $plaintext * @return string */ function encrypt($plaintext) { switch ($this->encryptionMode) { case self::ENCRYPTION_NONE: $plaintext = str_split($plaintext, $this->k); $ciphertext = ''; foreach ($plaintext as $m) { $ciphertext.= $this->_raw_encrypt($m); } return $ciphertext; case self::ENCRYPTION_PKCS1: $length = $this->k - 11; if ($length <= 0) { return false; } $plaintext = str_split($plaintext, $length); $ciphertext = ''; foreach ($plaintext as $m) { $ciphertext.= $this->_rsaes_pkcs1_v1_5_encrypt($m); } return $ciphertext; //case self::ENCRYPTION_OAEP: default: $length = $this->k - 2 * $this->hLen - 2; if ($length <= 0) { return false; } $plaintext = str_split($plaintext, $length); $ciphertext = ''; foreach ($plaintext as $m) { $ciphertext.= $this->_rsaes_oaep_encrypt($m); } return $ciphertext; } } /** * Decryption * * @see self::encrypt() * @access public * @param string $plaintext * @return string */ function decrypt($ciphertext) { if ($this->k <= 0) { return false; } $ciphertext = str_split($ciphertext, $this->k); $ciphertext[count($ciphertext) - 1] = str_pad($ciphertext[count($ciphertext) - 1], $this->k, chr(0), STR_PAD_LEFT); $plaintext = ''; switch ($this->encryptionMode) { case self::ENCRYPTION_NONE: $decrypt = '_raw_encrypt'; break; case self::ENCRYPTION_PKCS1: $decrypt = '_rsaes_pkcs1_v1_5_decrypt'; break; //case self::ENCRYPTION_OAEP: default: $decrypt = '_rsaes_oaep_decrypt'; } foreach ($ciphertext as $c) { $temp = $this->$decrypt($c); if ($temp === false) { return false; } $plaintext.= $temp; } return $plaintext; } /** * Create a signature * * @see self::verify() * @access public * @param string $message * @return string */ function sign($message) { if (empty($this->modulus) || empty($this->exponent)) { return false; } switch ($this->signatureMode) { case self::SIGNATURE_PKCS1: return $this->_rsassa_pkcs1_v1_5_sign($message); //case self::SIGNATURE_PSS: default: return $this->_rsassa_pss_sign($message); } } /** * Verifies a signature * * @see self::sign() * @access public * @param string $message * @param string $signature * @return bool */ function verify($message, $signature) { if (empty($this->modulus) || empty($this->exponent)) { return false; } switch ($this->signatureMode) { case self::SIGNATURE_PKCS1: return $this->_rsassa_pkcs1_v1_5_verify($message, $signature); //case self::SIGNATURE_PSS: default: return $this->_rsassa_pss_verify($message, $signature); } } /** * Extract raw BER from Base64 encoding * * @access private * @param string $str * @return string */ function _extractBER($str) { /* X.509 certs are assumed to be base64 encoded but sometimes they'll have additional things in them * above and beyond the ceritificate. * ie. some may have the following preceding the -----BEGIN CERTIFICATE----- line: * * Bag Attributes * localKeyID: 01 00 00 00 * subject=/O=organization/OU=org unit/CN=common name * issuer=/O=organization/CN=common name */ $temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1); // remove the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- stuff $temp = preg_replace('#-+[^-]+-+#', '', $temp); // remove new lines $temp = str_replace(array("\r", "\n", ' '), '', $temp); $temp = preg_match('#^[a-zA-Z\d/+]*={0,2}$#', $temp) ? base64_decode($temp) : false; return $temp != false ? $temp : $str; } } <?php /** * Pure-PHP implementation of AES. * * Uses mcrypt, if available/possible, and an internal implementation, otherwise. * * PHP version 5 * * NOTE: Since AES.php is (for compatibility and phpseclib-historical reasons) virtually * just a wrapper to Rijndael.php you may consider using Rijndael.php instead of * to save one include_once(). * * If {@link self::setKeyLength() setKeyLength()} isn't called, it'll be calculated from * {@link self::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits * it'll be null-padded to 192-bits and 192 bits will be the key length until {@link self::setKey() setKey()} * is called, again, at which point, it'll be recalculated. * * Since \phpseclib\Crypt\AES extends \phpseclib\Crypt\Rijndael, some functions are available to be called that, in the context of AES, don't * make a whole lot of sense. {@link self::setBlockLength() setBlockLength()}, for instance. Calling that function, * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one). * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $aes = new \phpseclib\Crypt\AES(); * * $aes->setKey('abcdefghijklmnop'); * * $size = 10 * 1024; * $plaintext = ''; * for ($i = 0; $i < $size; $i++) { * $plaintext.= 'a'; * } * * echo $aes->decrypt($aes->encrypt($plaintext)); * ?> * </code> * * @category Crypt * @package AES * @author Jim Wigginton <terrafrost@php.net> * @copyright 2008 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; /** * Pure-PHP implementation of AES. * * @package AES * @author Jim Wigginton <terrafrost@php.net> * @access public */ class AES extends Rijndael { /** * Dummy function * * Since \phpseclib\Crypt\AES extends \phpseclib\Crypt\Rijndael, this function is, technically, available, but it doesn't do anything. * * @see \phpseclib\Crypt\Rijndael::setBlockLength() * @access public * @param int $length */ function setBlockLength($length) { return; } /** * Sets the key length * * Valid key lengths are 128, 192, and 256. If the length is less than 128, it will be rounded up to * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount. * * @see \phpseclib\Crypt\Rijndael:setKeyLength() * @access public * @param int $length */ function setKeyLength($length) { switch ($length) { case 160: $length = 192; break; case 224: $length = 256; } parent::setKeyLength($length); } /** * Sets the key. * * Rijndael supports five different key lengths, AES only supports three. * * @see \phpseclib\Crypt\Rijndael:setKey() * @see setKeyLength() * @access public * @param string $key */ function setKey($key) { parent::setKey($key); if (!$this->explicit_key_length) { $length = strlen($key); switch (true) { case $length <= 16: $this->key_length = 16; break; case $length <= 24: $this->key_length = 24; break; default: $this->key_length = 32; } $this->_setEngine(); } } } <?php /** * Pure-PHP implementation of RC2. * * Uses mcrypt, if available, and an internal implementation, otherwise. * * PHP version 5 * * Useful resources are as follows: * * - {@link http://tools.ietf.org/html/rfc2268} * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $rc2 = new \phpseclib\Crypt\RC2(); * * $rc2->setKey('abcdefgh'); * * $plaintext = str_repeat('a', 1024); * * echo $rc2->decrypt($rc2->encrypt($plaintext)); * ?> * </code> * * @category Crypt * @package RC2 * @author Patrick Monnerat <pm@datasphere.ch> * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; /** * Pure-PHP implementation of RC2. * * @package RC2 * @access public */ class RC2 extends Base { /** * Block Length of the cipher * * @see \phpseclib\Crypt\Base::block_size * @var int * @access private */ var $block_size = 8; /** * The Key * * @see \phpseclib\Crypt\Base::key * @see self::setKey() * @var string * @access private */ var $key; /** * The Original (unpadded) Key * * @see \phpseclib\Crypt\Base::key * @see self::setKey() * @see self::encrypt() * @see self::decrypt() * @var string * @access private */ var $orig_key; /** * Don't truncate / null pad key * * @see \phpseclib\Crypt\Base::_clearBuffers() * @var bool * @access private */ var $skip_key_adjustment = true; /** * Key Length (in bytes) * * @see \phpseclib\Crypt\RC2::setKeyLength() * @var int * @access private */ var $key_length = 16; // = 128 bits /** * The mcrypt specific name of the cipher * * @see \phpseclib\Crypt\Base::cipher_name_mcrypt * @var string * @access private */ var $cipher_name_mcrypt = 'rc2'; /** * Optimizing value while CFB-encrypting * * @see \phpseclib\Crypt\Base::cfb_init_len * @var int * @access private */ var $cfb_init_len = 500; /** * The key length in bits. * * @see self::setKeyLength() * @see self::setKey() * @var int * @access private * @internal Should be in range [1..1024]. * @internal Changing this value after setting the key has no effect. */ var $default_key_length = 1024; /** * The key length in bits. * * @see self::isValidEnine() * @see self::setKey() * @var int * @access private * @internal Should be in range [1..1024]. */ var $current_key_length; /** * The Key Schedule * * @see self::_setupKey() * @var array * @access private */ var $keys; /** * Key expansion randomization table. * Twice the same 256-value sequence to save a modulus in key expansion. * * @see self::setKey() * @var array * @access private */ var $pitable = array( 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED, 0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D, 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E, 0x62, 0x4C, 0x64, 0x88, 0x44, 0x8B, 0xFB, 0xA2, 0x17, 0x9A, 0x59, 0xF5, 0x87, 0xB3, 0x4F, 0x13, 0x61, 0x45, 0x6D, 0x8D, 0x09, 0x81, 0x7D, 0x32, 0xBD, 0x8F, 0x40, 0xEB, 0x86, 0xB7, 0x7B, 0x0B, 0xF0, 0x95, 0x21, 0x22, 0x5C, 0x6B, 0x4E, 0x82, 0x54, 0xD6, 0x65, 0x93, 0xCE, 0x60, 0xB2, 0x1C, 0x73, 0x56, 0xC0, 0x14, 0xA7, 0x8C, 0xF1, 0xDC, 0x12, 0x75, 0xCA, 0x1F, 0x3B, 0xBE, 0xE4, 0xD1, 0x42, 0x3D, 0xD4, 0x30, 0xA3, 0x3C, 0xB6, 0x26, 0x6F, 0xBF, 0x0E, 0xDA, 0x46, 0x69, 0x07, 0x57, 0x27, 0xF2, 0x1D, 0x9B, 0xBC, 0x94, 0x43, 0x03, 0xF8, 0x11, 0xC7, 0xF6, 0x90, 0xEF, 0x3E, 0xE7, 0x06, 0xC3, 0xD5, 0x2F, 0xC8, 0x66, 0x1E, 0xD7, 0x08, 0xE8, 0xEA, 0xDE, 0x80, 0x52, 0xEE, 0xF7, 0x84, 0xAA, 0x72, 0xAC, 0x35, 0x4D, 0x6A, 0x2A, 0x96, 0x1A, 0xD2, 0x71, 0x5A, 0x15, 0x49, 0x74, 0x4B, 0x9F, 0xD0, 0x5E, 0x04, 0x18, 0xA4, 0xEC, 0xC2, 0xE0, 0x41, 0x6E, 0x0F, 0x51, 0xCB, 0xCC, 0x24, 0x91, 0xAF, 0x50, 0xA1, 0xF4, 0x70, 0x39, 0x99, 0x7C, 0x3A, 0x85, 0x23, 0xB8, 0xB4, 0x7A, 0xFC, 0x02, 0x36, 0x5B, 0x25, 0x55, 0x97, 0x31, 0x2D, 0x5D, 0xFA, 0x98, 0xE3, 0x8A, 0x92, 0xAE, 0x05, 0xDF, 0x29, 0x10, 0x67, 0x6C, 0xBA, 0xC9, 0xD3, 0x00, 0xE6, 0xCF, 0xE1, 0x9E, 0xA8, 0x2C, 0x63, 0x16, 0x01, 0x3F, 0x58, 0xE2, 0x89, 0xA9, 0x0D, 0x38, 0x34, 0x1B, 0xAB, 0x33, 0xFF, 0xB0, 0xBB, 0x48, 0x0C, 0x5F, 0xB9, 0xB1, 0xCD, 0x2E, 0xC5, 0xF3, 0xDB, 0x47, 0xE5, 0xA5, 0x9C, 0x77, 0x0A, 0xA6, 0x20, 0x68, 0xFE, 0x7F, 0xC1, 0xAD, 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED, 0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D, 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E, 0x62, 0x4C, 0x64, 0x88, 0x44, 0x8B, 0xFB, 0xA2, 0x17, 0x9A, 0x59, 0xF5, 0x87, 0xB3, 0x4F, 0x13, 0x61, 0x45, 0x6D, 0x8D, 0x09, 0x81, 0x7D, 0x32, 0xBD, 0x8F, 0x40, 0xEB, 0x86, 0xB7, 0x7B, 0x0B, 0xF0, 0x95, 0x21, 0x22, 0x5C, 0x6B, 0x4E, 0x82, 0x54, 0xD6, 0x65, 0x93, 0xCE, 0x60, 0xB2, 0x1C, 0x73, 0x56, 0xC0, 0x14, 0xA7, 0x8C, 0xF1, 0xDC, 0x12, 0x75, 0xCA, 0x1F, 0x3B, 0xBE, 0xE4, 0xD1, 0x42, 0x3D, 0xD4, 0x30, 0xA3, 0x3C, 0xB6, 0x26, 0x6F, 0xBF, 0x0E, 0xDA, 0x46, 0x69, 0x07, 0x57, 0x27, 0xF2, 0x1D, 0x9B, 0xBC, 0x94, 0x43, 0x03, 0xF8, 0x11, 0xC7, 0xF6, 0x90, 0xEF, 0x3E, 0xE7, 0x06, 0xC3, 0xD5, 0x2F, 0xC8, 0x66, 0x1E, 0xD7, 0x08, 0xE8, 0xEA, 0xDE, 0x80, 0x52, 0xEE, 0xF7, 0x84, 0xAA, 0x72, 0xAC, 0x35, 0x4D, 0x6A, 0x2A, 0x96, 0x1A, 0xD2, 0x71, 0x5A, 0x15, 0x49, 0x74, 0x4B, 0x9F, 0xD0, 0x5E, 0x04, 0x18, 0xA4, 0xEC, 0xC2, 0xE0, 0x41, 0x6E, 0x0F, 0x51, 0xCB, 0xCC, 0x24, 0x91, 0xAF, 0x50, 0xA1, 0xF4, 0x70, 0x39, 0x99, 0x7C, 0x3A, 0x85, 0x23, 0xB8, 0xB4, 0x7A, 0xFC, 0x02, 0x36, 0x5B, 0x25, 0x55, 0x97, 0x31, 0x2D, 0x5D, 0xFA, 0x98, 0xE3, 0x8A, 0x92, 0xAE, 0x05, 0xDF, 0x29, 0x10, 0x67, 0x6C, 0xBA, 0xC9, 0xD3, 0x00, 0xE6, 0xCF, 0xE1, 0x9E, 0xA8, 0x2C, 0x63, 0x16, 0x01, 0x3F, 0x58, 0xE2, 0x89, 0xA9, 0x0D, 0x38, 0x34, 0x1B, 0xAB, 0x33, 0xFF, 0xB0, 0xBB, 0x48, 0x0C, 0x5F, 0xB9, 0xB1, 0xCD, 0x2E, 0xC5, 0xF3, 0xDB, 0x47, 0xE5, 0xA5, 0x9C, 0x77, 0x0A, 0xA6, 0x20, 0x68, 0xFE, 0x7F, 0xC1, 0xAD ); /** * Inverse key expansion randomization table. * * @see self::setKey() * @var array * @access private */ var $invpitable = array( 0xD1, 0xDA, 0xB9, 0x6F, 0x9C, 0xC8, 0x78, 0x66, 0x80, 0x2C, 0xF8, 0x37, 0xEA, 0xE0, 0x62, 0xA4, 0xCB, 0x71, 0x50, 0x27, 0x4B, 0x95, 0xD9, 0x20, 0x9D, 0x04, 0x91, 0xE3, 0x47, 0x6A, 0x7E, 0x53, 0xFA, 0x3A, 0x3B, 0xB4, 0xA8, 0xBC, 0x5F, 0x68, 0x08, 0xCA, 0x8F, 0x14, 0xD7, 0xC0, 0xEF, 0x7B, 0x5B, 0xBF, 0x2F, 0xE5, 0xE2, 0x8C, 0xBA, 0x12, 0xE1, 0xAF, 0xB2, 0x54, 0x5D, 0x59, 0x76, 0xDB, 0x32, 0xA2, 0x58, 0x6E, 0x1C, 0x29, 0x64, 0xF3, 0xE9, 0x96, 0x0C, 0x98, 0x19, 0x8D, 0x3E, 0x26, 0xAB, 0xA5, 0x85, 0x16, 0x40, 0xBD, 0x49, 0x67, 0xDC, 0x22, 0x94, 0xBB, 0x3C, 0xC1, 0x9B, 0xEB, 0x45, 0x28, 0x18, 0xD8, 0x1A, 0x42, 0x7D, 0xCC, 0xFB, 0x65, 0x8E, 0x3D, 0xCD, 0x2A, 0xA3, 0x60, 0xAE, 0x93, 0x8A, 0x48, 0x97, 0x51, 0x15, 0xF7, 0x01, 0x0B, 0xB7, 0x36, 0xB1, 0x2E, 0x11, 0xFD, 0x84, 0x2D, 0x3F, 0x13, 0x88, 0xB3, 0x34, 0x24, 0x1B, 0xDE, 0xC5, 0x1D, 0x4D, 0x2B, 0x17, 0x31, 0x74, 0xA9, 0xC6, 0x43, 0x6D, 0x39, 0x90, 0xBE, 0xC3, 0xB0, 0x21, 0x6B, 0xF6, 0x0F, 0xD5, 0x99, 0x0D, 0xAC, 0x1F, 0x5C, 0x9E, 0xF5, 0xF9, 0x4C, 0xD6, 0xDF, 0x89, 0xE4, 0x8B, 0xFF, 0xC7, 0xAA, 0xE7, 0xED, 0x46, 0x25, 0xB6, 0x06, 0x5E, 0x35, 0xB5, 0xEC, 0xCE, 0xE8, 0x6C, 0x30, 0x55, 0x61, 0x4A, 0xFE, 0xA0, 0x79, 0x03, 0xF0, 0x10, 0x72, 0x7C, 0xCF, 0x52, 0xA6, 0xA7, 0xEE, 0x44, 0xD3, 0x9A, 0x57, 0x92, 0xD0, 0x5A, 0x7A, 0x41, 0x7F, 0x0E, 0x00, 0x63, 0xF2, 0x4F, 0x05, 0x83, 0xC9, 0xA1, 0xD4, 0xDD, 0xC4, 0x56, 0xF4, 0xD2, 0x77, 0x81, 0x09, 0x82, 0x33, 0x9F, 0x07, 0x86, 0x75, 0x38, 0x4E, 0x69, 0xF1, 0xAD, 0x23, 0x73, 0x87, 0x70, 0x02, 0xC2, 0x1E, 0xB8, 0x0A, 0xFC, 0xE6 ); /** * Test for engine validity * * This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine() * * @see \phpseclib\Crypt\Base::__construct() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { switch ($engine) { case self::ENGINE_OPENSSL: if ($this->current_key_length != 128 || strlen($this->orig_key) < 16) { return false; } $this->cipher_name_openssl_ecb = 'rc2-ecb'; $this->cipher_name_openssl = 'rc2-' . $this->_openssl_translate_mode(); } return parent::isValidEngine($engine); } /** * Sets the key length. * * Valid key lengths are 8 to 1024. * Calling this function after setting the key has no effect until the next * \phpseclib\Crypt\RC2::setKey() call. * * @access public * @param int $length in bits */ function setKeyLength($length) { if ($length < 8) { $this->default_key_length = 1; } elseif ($length > 1024) { $this->default_key_length = 128; } else { $this->default_key_length = $length; } $this->current_key_length = $this->default_key_length; parent::setKeyLength($length); } /** * Returns the current key length * * @access public * @return int */ function getKeyLength() { return $this->current_key_length; } /** * Sets the key. * * Keys can be of any length. RC2, itself, uses 8 to 1024 bit keys (eg. * strlen($key) <= 128), however, we only use the first 128 bytes if $key * has more then 128 bytes in it, and set $key to a single null byte if * it is empty. * * If the key is not explicitly set, it'll be assumed to be a single * null byte. * * @see \phpseclib\Crypt\Base::setKey() * @access public * @param string $key * @param int $t1 optional Effective key length in bits. */ function setKey($key, $t1 = 0) { $this->orig_key = $key; if ($t1 <= 0) { $t1 = $this->default_key_length; } elseif ($t1 > 1024) { $t1 = 1024; } $this->current_key_length = $t1; // Key byte count should be 1..128. $key = strlen($key) ? substr($key, 0, 128) : "\x00"; $t = strlen($key); // The mcrypt RC2 implementation only supports effective key length // of 1024 bits. It is however possible to handle effective key // lengths in range 1..1024 by expanding the key and applying // inverse pitable mapping to the first byte before submitting it // to mcrypt. // Key expansion. $l = array_values(unpack('C*', $key)); $t8 = ($t1 + 7) >> 3; $tm = 0xFF >> (8 * $t8 - $t1); // Expand key. $pitable = $this->pitable; for ($i = $t; $i < 128; $i++) { $l[$i] = $pitable[$l[$i - 1] + $l[$i - $t]]; } $i = 128 - $t8; $l[$i] = $pitable[$l[$i] & $tm]; while ($i--) { $l[$i] = $pitable[$l[$i + 1] ^ $l[$i + $t8]]; } // Prepare the key for mcrypt. $l[0] = $this->invpitable[$l[0]]; array_unshift($l, 'C*'); parent::setKey(call_user_func_array('pack', $l)); } /** * Encrypts a message. * * Mostly a wrapper for \phpseclib\Crypt\Base::encrypt, with some additional OpenSSL handling code * * @see self::decrypt() * @access public * @param string $plaintext * @return string $ciphertext */ function encrypt($plaintext) { if ($this->engine == self::ENGINE_OPENSSL) { $temp = $this->key; $this->key = $this->orig_key; $result = parent::encrypt($plaintext); $this->key = $temp; return $result; } return parent::encrypt($plaintext); } /** * Decrypts a message. * * Mostly a wrapper for \phpseclib\Crypt\Base::decrypt, with some additional OpenSSL handling code * * @see self::encrypt() * @access public * @param string $ciphertext * @return string $plaintext */ function decrypt($ciphertext) { if ($this->engine == self::ENGINE_OPENSSL) { $temp = $this->key; $this->key = $this->orig_key; $result = parent::decrypt($ciphertext); $this->key = $temp; return $result; } return parent::decrypt($ciphertext); } /** * Encrypts a block * * @see \phpseclib\Crypt\Base::_encryptBlock() * @see \phpseclib\Crypt\Base::encrypt() * @access private * @param string $in * @return string */ function _encryptBlock($in) { list($r0, $r1, $r2, $r3) = array_values(unpack('v*', $in)); $keys = $this->keys; $limit = 20; $actions = array($limit => 44, 44 => 64); $j = 0; for (;;) { // Mixing round. $r0 = (($r0 + $keys[$j++] + ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF) << 1; $r0 |= $r0 >> 16; $r1 = (($r1 + $keys[$j++] + ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF) << 2; $r1 |= $r1 >> 16; $r2 = (($r2 + $keys[$j++] + ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF) << 3; $r2 |= $r2 >> 16; $r3 = (($r3 + $keys[$j++] + ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF) << 5; $r3 |= $r3 >> 16; if ($j === $limit) { if ($limit === 64) { break; } // Mashing round. $r0 += $keys[$r3 & 0x3F]; $r1 += $keys[$r0 & 0x3F]; $r2 += $keys[$r1 & 0x3F]; $r3 += $keys[$r2 & 0x3F]; $limit = $actions[$limit]; } } return pack('vvvv', $r0, $r1, $r2, $r3); } /** * Decrypts a block * * @see \phpseclib\Crypt\Base::_decryptBlock() * @see \phpseclib\Crypt\Base::decrypt() * @access private * @param string $in * @return string */ function _decryptBlock($in) { list($r0, $r1, $r2, $r3) = array_values(unpack('v*', $in)); $keys = $this->keys; $limit = 44; $actions = array($limit => 20, 20 => 0); $j = 64; for (;;) { // R-mixing round. $r3 = ($r3 | ($r3 << 16)) >> 5; $r3 = ($r3 - $keys[--$j] - ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF; $r2 = ($r2 | ($r2 << 16)) >> 3; $r2 = ($r2 - $keys[--$j] - ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF; $r1 = ($r1 | ($r1 << 16)) >> 2; $r1 = ($r1 - $keys[--$j] - ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF; $r0 = ($r0 | ($r0 << 16)) >> 1; $r0 = ($r0 - $keys[--$j] - ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF; if ($j === $limit) { if ($limit === 0) { break; } // R-mashing round. $r3 = ($r3 - $keys[$r2 & 0x3F]) & 0xFFFF; $r2 = ($r2 - $keys[$r1 & 0x3F]) & 0xFFFF; $r1 = ($r1 - $keys[$r0 & 0x3F]) & 0xFFFF; $r0 = ($r0 - $keys[$r3 & 0x3F]) & 0xFFFF; $limit = $actions[$limit]; } } return pack('vvvv', $r0, $r1, $r2, $r3); } /** * Setup the \phpseclib\Crypt\Base::ENGINE_MCRYPT $engine * * @see \phpseclib\Crypt\Base::_setupMcrypt() * @access private */ function _setupMcrypt() { if (!isset($this->key)) { $this->setKey(''); } parent::_setupMcrypt(); } /** * Creates the key schedule * * @see \phpseclib\Crypt\Base::_setupKey() * @access private */ function _setupKey() { if (!isset($this->key)) { $this->setKey(''); } // Key has already been expanded in \phpseclib\Crypt\RC2::setKey(): // Only the first value must be altered. $l = unpack('Ca/Cb/v*', $this->key); array_unshift($l, $this->pitable[$l['a']] | ($l['b'] << 8)); unset($l['a']); unset($l['b']); $this->keys = $l; } /** * Setup the performance-optimized function for de/encrypt() * * @see \phpseclib\Crypt\Base::_setupInlineCrypt() * @access private */ function _setupInlineCrypt() { $lambda_functions =& self::_getLambdaFunctions(); // The first 10 generated $lambda_functions will use the $keys hardcoded as integers // for the mixing rounds, for better inline crypt performance [~20% faster]. // But for memory reason we have to limit those ultra-optimized $lambda_functions to an amount of 10. // (Currently, for Crypt_RC2, one generated $lambda_function cost on php5.5@32bit ~60kb unfreeable mem and ~100kb on php5.5@64bit) $gen_hi_opt_code = (bool)(count($lambda_functions) < 10); // Generation of a unique hash for our generated code $code_hash = "Crypt_RC2, {$this->mode}"; if ($gen_hi_opt_code) { $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key); } // Is there a re-usable $lambda_functions in there? // If not, we have to create it. if (!isset($lambda_functions[$code_hash])) { // Init code for both, encrypt and decrypt. $init_crypt = '$keys = $self->keys;'; switch (true) { case $gen_hi_opt_code: $keys = $this->keys; default: $keys = array(); foreach ($this->keys as $k => $v) { $keys[$k] = '$keys[' . $k . ']'; } } // $in is the current 8 bytes block which has to be en/decrypt $encrypt_block = $decrypt_block = ' $in = unpack("v4", $in); $r0 = $in[1]; $r1 = $in[2]; $r2 = $in[3]; $r3 = $in[4]; '; // Create code for encryption. $limit = 20; $actions = array($limit => 44, 44 => 64); $j = 0; for (;;) { // Mixing round. $encrypt_block .= ' $r0 = (($r0 + ' . $keys[$j++] . ' + ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF) << 1; $r0 |= $r0 >> 16; $r1 = (($r1 + ' . $keys[$j++] . ' + ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF) << 2; $r1 |= $r1 >> 16; $r2 = (($r2 + ' . $keys[$j++] . ' + ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF) << 3; $r2 |= $r2 >> 16; $r3 = (($r3 + ' . $keys[$j++] . ' + ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF) << 5; $r3 |= $r3 >> 16;'; if ($j === $limit) { if ($limit === 64) { break; } // Mashing round. $encrypt_block .= ' $r0 += $keys[$r3 & 0x3F]; $r1 += $keys[$r0 & 0x3F]; $r2 += $keys[$r1 & 0x3F]; $r3 += $keys[$r2 & 0x3F];'; $limit = $actions[$limit]; } } $encrypt_block .= '$in = pack("v4", $r0, $r1, $r2, $r3);'; // Create code for decryption. $limit = 44; $actions = array($limit => 20, 20 => 0); $j = 64; for (;;) { // R-mixing round. $decrypt_block .= ' $r3 = ($r3 | ($r3 << 16)) >> 5; $r3 = ($r3 - ' . $keys[--$j] . ' - ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF; $r2 = ($r2 | ($r2 << 16)) >> 3; $r2 = ($r2 - ' . $keys[--$j] . ' - ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF; $r1 = ($r1 | ($r1 << 16)) >> 2; $r1 = ($r1 - ' . $keys[--$j] . ' - ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF; $r0 = ($r0 | ($r0 << 16)) >> 1; $r0 = ($r0 - ' . $keys[--$j] . ' - ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF;'; if ($j === $limit) { if ($limit === 0) { break; } // R-mashing round. $decrypt_block .= ' $r3 = ($r3 - $keys[$r2 & 0x3F]) & 0xFFFF; $r2 = ($r2 - $keys[$r1 & 0x3F]) & 0xFFFF; $r1 = ($r1 - $keys[$r0 & 0x3F]) & 0xFFFF; $r0 = ($r0 - $keys[$r3 & 0x3F]) & 0xFFFF;'; $limit = $actions[$limit]; } } $decrypt_block .= '$in = pack("v4", $r0, $r1, $r2, $r3);'; // Creates the inline-crypt function $lambda_functions[$code_hash] = $this->_createInlineCryptFunction( array( 'init_crypt' => $init_crypt, 'encrypt_block' => $encrypt_block, 'decrypt_block' => $decrypt_block ) ); } // Set the inline-crypt function as callback in: $this->inline_crypt $this->inline_crypt = $lambda_functions[$code_hash]; } } <?php /** * Pure-PHP implementation of DES. * * Uses mcrypt, if available, and an internal implementation, otherwise. * * PHP version 5 * * Useful resources are as follows: * * - {@link http://en.wikipedia.org/wiki/DES_supplementary_material Wikipedia: DES supplementary material} * - {@link http://www.itl.nist.gov/fipspubs/fip46-2.htm FIPS 46-2 - (DES), Data Encryption Standard} * - {@link http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-DES.html JavaScript DES Example} * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $des = new \phpseclib\Crypt\DES(); * * $des->setKey('abcdefgh'); * * $size = 10 * 1024; * $plaintext = ''; * for ($i = 0; $i < $size; $i++) { * $plaintext.= 'a'; * } * * echo $des->decrypt($des->encrypt($plaintext)); * ?> * </code> * * @category Crypt * @package DES * @author Jim Wigginton <terrafrost@php.net> * @copyright 2007 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; /** * Pure-PHP implementation of DES. * * @package DES * @author Jim Wigginton <terrafrost@php.net> * @access public */ class DES extends Base { /**#@+ * @access private * @see \phpseclib\Crypt\DES::_setupKey() * @see \phpseclib\Crypt\DES::_processBlock() */ /** * Contains $keys[self::ENCRYPT] */ const ENCRYPT = 0; /** * Contains $keys[self::DECRYPT] */ const DECRYPT = 1; /**#@-*/ /** * Block Length of the cipher * * @see \phpseclib\Crypt\Base::block_size * @var int * @access private */ var $block_size = 8; /** * Key Length (in bytes) * * @see \phpseclib\Crypt\Base::setKeyLength() * @var int * @access private */ var $key_length = 8; /** * The mcrypt specific name of the cipher * * @see \phpseclib\Crypt\Base::cipher_name_mcrypt * @var string * @access private */ var $cipher_name_mcrypt = 'des'; /** * The OpenSSL names of the cipher / modes * * @see \phpseclib\Crypt\Base::openssl_mode_names * @var array * @access private */ var $openssl_mode_names = array( self::MODE_ECB => 'des-ecb', self::MODE_CBC => 'des-cbc', self::MODE_CFB => 'des-cfb', self::MODE_OFB => 'des-ofb' // self::MODE_CTR is undefined for DES ); /** * Optimizing value while CFB-encrypting * * @see \phpseclib\Crypt\Base::cfb_init_len * @var int * @access private */ var $cfb_init_len = 500; /** * Switch for DES/3DES encryption * * Used only if $engine == self::ENGINE_INTERNAL * * @see self::_setupKey() * @see self::_processBlock() * @var int * @access private */ var $des_rounds = 1; /** * max possible size of $key * * @see self::setKey() * @var string * @access private */ var $key_length_max = 8; /** * The Key Schedule * * @see self::_setupKey() * @var array * @access private */ var $keys; /** * Shuffle table. * * For each byte value index, the entry holds an 8-byte string * with each byte containing all bits in the same state as the * corresponding bit in the index value. * * @see self::_processBlock() * @see self::_setupKey() * @var array * @access private */ var $shuffle = array( "\x00\x00\x00\x00\x00\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\xFF", "\x00\x00\x00\x00\x00\x00\xFF\x00", "\x00\x00\x00\x00\x00\x00\xFF\xFF", "\x00\x00\x00\x00\x00\xFF\x00\x00", "\x00\x00\x00\x00\x00\xFF\x00\xFF", "\x00\x00\x00\x00\x00\xFF\xFF\x00", "\x00\x00\x00\x00\x00\xFF\xFF\xFF", "\x00\x00\x00\x00\xFF\x00\x00\x00", "\x00\x00\x00\x00\xFF\x00\x00\xFF", "\x00\x00\x00\x00\xFF\x00\xFF\x00", "\x00\x00\x00\x00\xFF\x00\xFF\xFF", "\x00\x00\x00\x00\xFF\xFF\x00\x00", "\x00\x00\x00\x00\xFF\xFF\x00\xFF", "\x00\x00\x00\x00\xFF\xFF\xFF\x00", "\x00\x00\x00\x00\xFF\xFF\xFF\xFF", "\x00\x00\x00\xFF\x00\x00\x00\x00", "\x00\x00\x00\xFF\x00\x00\x00\xFF", "\x00\x00\x00\xFF\x00\x00\xFF\x00", "\x00\x00\x00\xFF\x00\x00\xFF\xFF", "\x00\x00\x00\xFF\x00\xFF\x00\x00", "\x00\x00\x00\xFF\x00\xFF\x00\xFF", "\x00\x00\x00\xFF\x00\xFF\xFF\x00", "\x00\x00\x00\xFF\x00\xFF\xFF\xFF", "\x00\x00\x00\xFF\xFF\x00\x00\x00", "\x00\x00\x00\xFF\xFF\x00\x00\xFF", "\x00\x00\x00\xFF\xFF\x00\xFF\x00", "\x00\x00\x00\xFF\xFF\x00\xFF\xFF", "\x00\x00\x00\xFF\xFF\xFF\x00\x00", "\x00\x00\x00\xFF\xFF\xFF\x00\xFF", "\x00\x00\x00\xFF\xFF\xFF\xFF\x00", "\x00\x00\x00\xFF\xFF\xFF\xFF\xFF", "\x00\x00\xFF\x00\x00\x00\x00\x00", "\x00\x00\xFF\x00\x00\x00\x00\xFF", "\x00\x00\xFF\x00\x00\x00\xFF\x00", "\x00\x00\xFF\x00\x00\x00\xFF\xFF", "\x00\x00\xFF\x00\x00\xFF\x00\x00", "\x00\x00\xFF\x00\x00\xFF\x00\xFF", "\x00\x00\xFF\x00\x00\xFF\xFF\x00", "\x00\x00\xFF\x00\x00\xFF\xFF\xFF", "\x00\x00\xFF\x00\xFF\x00\x00\x00", "\x00\x00\xFF\x00\xFF\x00\x00\xFF", "\x00\x00\xFF\x00\xFF\x00\xFF\x00", "\x00\x00\xFF\x00\xFF\x00\xFF\xFF", "\x00\x00\xFF\x00\xFF\xFF\x00\x00", "\x00\x00\xFF\x00\xFF\xFF\x00\xFF", "\x00\x00\xFF\x00\xFF\xFF\xFF\x00", "\x00\x00\xFF\x00\xFF\xFF\xFF\xFF", "\x00\x00\xFF\xFF\x00\x00\x00\x00", "\x00\x00\xFF\xFF\x00\x00\x00\xFF", "\x00\x00\xFF\xFF\x00\x00\xFF\x00", "\x00\x00\xFF\xFF\x00\x00\xFF\xFF", "\x00\x00\xFF\xFF\x00\xFF\x00\x00", "\x00\x00\xFF\xFF\x00\xFF\x00\xFF", "\x00\x00\xFF\xFF\x00\xFF\xFF\x00", "\x00\x00\xFF\xFF\x00\xFF\xFF\xFF", "\x00\x00\xFF\xFF\xFF\x00\x00\x00", "\x00\x00\xFF\xFF\xFF\x00\x00\xFF", "\x00\x00\xFF\xFF\xFF\x00\xFF\x00", "\x00\x00\xFF\xFF\xFF\x00\xFF\xFF", "\x00\x00\xFF\xFF\xFF\xFF\x00\x00", "\x00\x00\xFF\xFF\xFF\xFF\x00\xFF", "\x00\x00\xFF\xFF\xFF\xFF\xFF\x00", "\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF", "\x00\xFF\x00\x00\x00\x00\x00\x00", "\x00\xFF\x00\x00\x00\x00\x00\xFF", "\x00\xFF\x00\x00\x00\x00\xFF\x00", "\x00\xFF\x00\x00\x00\x00\xFF\xFF", "\x00\xFF\x00\x00\x00\xFF\x00\x00", "\x00\xFF\x00\x00\x00\xFF\x00\xFF", "\x00\xFF\x00\x00\x00\xFF\xFF\x00", "\x00\xFF\x00\x00\x00\xFF\xFF\xFF", "\x00\xFF\x00\x00\xFF\x00\x00\x00", "\x00\xFF\x00\x00\xFF\x00\x00\xFF", "\x00\xFF\x00\x00\xFF\x00\xFF\x00", "\x00\xFF\x00\x00\xFF\x00\xFF\xFF", "\x00\xFF\x00\x00\xFF\xFF\x00\x00", "\x00\xFF\x00\x00\xFF\xFF\x00\xFF", "\x00\xFF\x00\x00\xFF\xFF\xFF\x00", "\x00\xFF\x00\x00\xFF\xFF\xFF\xFF", "\x00\xFF\x00\xFF\x00\x00\x00\x00", "\x00\xFF\x00\xFF\x00\x00\x00\xFF", "\x00\xFF\x00\xFF\x00\x00\xFF\x00", "\x00\xFF\x00\xFF\x00\x00\xFF\xFF", "\x00\xFF\x00\xFF\x00\xFF\x00\x00", "\x00\xFF\x00\xFF\x00\xFF\x00\xFF", "\x00\xFF\x00\xFF\x00\xFF\xFF\x00", "\x00\xFF\x00\xFF\x00\xFF\xFF\xFF", "\x00\xFF\x00\xFF\xFF\x00\x00\x00", "\x00\xFF\x00\xFF\xFF\x00\x00\xFF", "\x00\xFF\x00\xFF\xFF\x00\xFF\x00", "\x00\xFF\x00\xFF\xFF\x00\xFF\xFF", "\x00\xFF\x00\xFF\xFF\xFF\x00\x00", "\x00\xFF\x00\xFF\xFF\xFF\x00\xFF", "\x00\xFF\x00\xFF\xFF\xFF\xFF\x00", "\x00\xFF\x00\xFF\xFF\xFF\xFF\xFF", "\x00\xFF\xFF\x00\x00\x00\x00\x00", "\x00\xFF\xFF\x00\x00\x00\x00\xFF", "\x00\xFF\xFF\x00\x00\x00\xFF\x00", "\x00\xFF\xFF\x00\x00\x00\xFF\xFF", "\x00\xFF\xFF\x00\x00\xFF\x00\x00", "\x00\xFF\xFF\x00\x00\xFF\x00\xFF", "\x00\xFF\xFF\x00\x00\xFF\xFF\x00", "\x00\xFF\xFF\x00\x00\xFF\xFF\xFF", "\x00\xFF\xFF\x00\xFF\x00\x00\x00", "\x00\xFF\xFF\x00\xFF\x00\x00\xFF", "\x00\xFF\xFF\x00\xFF\x00\xFF\x00", "\x00\xFF\xFF\x00\xFF\x00\xFF\xFF", "\x00\xFF\xFF\x00\xFF\xFF\x00\x00", "\x00\xFF\xFF\x00\xFF\xFF\x00\xFF", "\x00\xFF\xFF\x00\xFF\xFF\xFF\x00", "\x00\xFF\xFF\x00\xFF\xFF\xFF\xFF", "\x00\xFF\xFF\xFF\x00\x00\x00\x00", "\x00\xFF\xFF\xFF\x00\x00\x00\xFF", "\x00\xFF\xFF\xFF\x00\x00\xFF\x00", "\x00\xFF\xFF\xFF\x00\x00\xFF\xFF", "\x00\xFF\xFF\xFF\x00\xFF\x00\x00", "\x00\xFF\xFF\xFF\x00\xFF\x00\xFF", "\x00\xFF\xFF\xFF\x00\xFF\xFF\x00", "\x00\xFF\xFF\xFF\x00\xFF\xFF\xFF", "\x00\xFF\xFF\xFF\xFF\x00\x00\x00", "\x00\xFF\xFF\xFF\xFF\x00\x00\xFF", "\x00\xFF\xFF\xFF\xFF\x00\xFF\x00", "\x00\xFF\xFF\xFF\xFF\x00\xFF\xFF", "\x00\xFF\xFF\xFF\xFF\xFF\x00\x00", "\x00\xFF\xFF\xFF\xFF\xFF\x00\xFF", "\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00", "\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF", "\xFF\x00\x00\x00\x00\x00\x00\x00", "\xFF\x00\x00\x00\x00\x00\x00\xFF", "\xFF\x00\x00\x00\x00\x00\xFF\x00", "\xFF\x00\x00\x00\x00\x00\xFF\xFF", "\xFF\x00\x00\x00\x00\xFF\x00\x00", "\xFF\x00\x00\x00\x00\xFF\x00\xFF", "\xFF\x00\x00\x00\x00\xFF\xFF\x00", "\xFF\x00\x00\x00\x00\xFF\xFF\xFF", "\xFF\x00\x00\x00\xFF\x00\x00\x00", "\xFF\x00\x00\x00\xFF\x00\x00\xFF", "\xFF\x00\x00\x00\xFF\x00\xFF\x00", "\xFF\x00\x00\x00\xFF\x00\xFF\xFF", "\xFF\x00\x00\x00\xFF\xFF\x00\x00", "\xFF\x00\x00\x00\xFF\xFF\x00\xFF", "\xFF\x00\x00\x00\xFF\xFF\xFF\x00", "\xFF\x00\x00\x00\xFF\xFF\xFF\xFF", "\xFF\x00\x00\xFF\x00\x00\x00\x00", "\xFF\x00\x00\xFF\x00\x00\x00\xFF", "\xFF\x00\x00\xFF\x00\x00\xFF\x00", "\xFF\x00\x00\xFF\x00\x00\xFF\xFF", "\xFF\x00\x00\xFF\x00\xFF\x00\x00", "\xFF\x00\x00\xFF\x00\xFF\x00\xFF", "\xFF\x00\x00\xFF\x00\xFF\xFF\x00", "\xFF\x00\x00\xFF\x00\xFF\xFF\xFF", "\xFF\x00\x00\xFF\xFF\x00\x00\x00", "\xFF\x00\x00\xFF\xFF\x00\x00\xFF", "\xFF\x00\x00\xFF\xFF\x00\xFF\x00", "\xFF\x00\x00\xFF\xFF\x00\xFF\xFF", "\xFF\x00\x00\xFF\xFF\xFF\x00\x00", "\xFF\x00\x00\xFF\xFF\xFF\x00\xFF", "\xFF\x00\x00\xFF\xFF\xFF\xFF\x00", "\xFF\x00\x00\xFF\xFF\xFF\xFF\xFF", "\xFF\x00\xFF\x00\x00\x00\x00\x00", "\xFF\x00\xFF\x00\x00\x00\x00\xFF", "\xFF\x00\xFF\x00\x00\x00\xFF\x00", "\xFF\x00\xFF\x00\x00\x00\xFF\xFF", "\xFF\x00\xFF\x00\x00\xFF\x00\x00", "\xFF\x00\xFF\x00\x00\xFF\x00\xFF", "\xFF\x00\xFF\x00\x00\xFF\xFF\x00", "\xFF\x00\xFF\x00\x00\xFF\xFF\xFF", "\xFF\x00\xFF\x00\xFF\x00\x00\x00", "\xFF\x00\xFF\x00\xFF\x00\x00\xFF", "\xFF\x00\xFF\x00\xFF\x00\xFF\x00", "\xFF\x00\xFF\x00\xFF\x00\xFF\xFF", "\xFF\x00\xFF\x00\xFF\xFF\x00\x00", "\xFF\x00\xFF\x00\xFF\xFF\x00\xFF", "\xFF\x00\xFF\x00\xFF\xFF\xFF\x00", "\xFF\x00\xFF\x00\xFF\xFF\xFF\xFF", "\xFF\x00\xFF\xFF\x00\x00\x00\x00", "\xFF\x00\xFF\xFF\x00\x00\x00\xFF", "\xFF\x00\xFF\xFF\x00\x00\xFF\x00", "\xFF\x00\xFF\xFF\x00\x00\xFF\xFF", "\xFF\x00\xFF\xFF\x00\xFF\x00\x00", "\xFF\x00\xFF\xFF\x00\xFF\x00\xFF", "\xFF\x00\xFF\xFF\x00\xFF\xFF\x00", "\xFF\x00\xFF\xFF\x00\xFF\xFF\xFF", "\xFF\x00\xFF\xFF\xFF\x00\x00\x00", "\xFF\x00\xFF\xFF\xFF\x00\x00\xFF", "\xFF\x00\xFF\xFF\xFF\x00\xFF\x00", "\xFF\x00\xFF\xFF\xFF\x00\xFF\xFF", "\xFF\x00\xFF\xFF\xFF\xFF\x00\x00", "\xFF\x00\xFF\xFF\xFF\xFF\x00\xFF", "\xFF\x00\xFF\xFF\xFF\xFF\xFF\x00", "\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF", "\xFF\xFF\x00\x00\x00\x00\x00\x00", "\xFF\xFF\x00\x00\x00\x00\x00\xFF", "\xFF\xFF\x00\x00\x00\x00\xFF\x00", "\xFF\xFF\x00\x00\x00\x00\xFF\xFF", "\xFF\xFF\x00\x00\x00\xFF\x00\x00", "\xFF\xFF\x00\x00\x00\xFF\x00\xFF", "\xFF\xFF\x00\x00\x00\xFF\xFF\x00", "\xFF\xFF\x00\x00\x00\xFF\xFF\xFF", "\xFF\xFF\x00\x00\xFF\x00\x00\x00", "\xFF\xFF\x00\x00\xFF\x00\x00\xFF", "\xFF\xFF\x00\x00\xFF\x00\xFF\x00", "\xFF\xFF\x00\x00\xFF\x00\xFF\xFF", "\xFF\xFF\x00\x00\xFF\xFF\x00\x00", "\xFF\xFF\x00\x00\xFF\xFF\x00\xFF", "\xFF\xFF\x00\x00\xFF\xFF\xFF\x00", "\xFF\xFF\x00\x00\xFF\xFF\xFF\xFF", "\xFF\xFF\x00\xFF\x00\x00\x00\x00", "\xFF\xFF\x00\xFF\x00\x00\x00\xFF", "\xFF\xFF\x00\xFF\x00\x00\xFF\x00", "\xFF\xFF\x00\xFF\x00\x00\xFF\xFF", "\xFF\xFF\x00\xFF\x00\xFF\x00\x00", "\xFF\xFF\x00\xFF\x00\xFF\x00\xFF", "\xFF\xFF\x00\xFF\x00\xFF\xFF\x00", "\xFF\xFF\x00\xFF\x00\xFF\xFF\xFF", "\xFF\xFF\x00\xFF\xFF\x00\x00\x00", "\xFF\xFF\x00\xFF\xFF\x00\x00\xFF", "\xFF\xFF\x00\xFF\xFF\x00\xFF\x00", "\xFF\xFF\x00\xFF\xFF\x00\xFF\xFF", "\xFF\xFF\x00\xFF\xFF\xFF\x00\x00", "\xFF\xFF\x00\xFF\xFF\xFF\x00\xFF", "\xFF\xFF\x00\xFF\xFF\xFF\xFF\x00", "\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF", "\xFF\xFF\xFF\x00\x00\x00\x00\x00", "\xFF\xFF\xFF\x00\x00\x00\x00\xFF", "\xFF\xFF\xFF\x00\x00\x00\xFF\x00", "\xFF\xFF\xFF\x00\x00\x00\xFF\xFF", "\xFF\xFF\xFF\x00\x00\xFF\x00\x00", "\xFF\xFF\xFF\x00\x00\xFF\x00\xFF", "\xFF\xFF\xFF\x00\x00\xFF\xFF\x00", "\xFF\xFF\xFF\x00\x00\xFF\xFF\xFF", "\xFF\xFF\xFF\x00\xFF\x00\x00\x00", "\xFF\xFF\xFF\x00\xFF\x00\x00\xFF", "\xFF\xFF\xFF\x00\xFF\x00\xFF\x00", "\xFF\xFF\xFF\x00\xFF\x00\xFF\xFF", "\xFF\xFF\xFF\x00\xFF\xFF\x00\x00", "\xFF\xFF\xFF\x00\xFF\xFF\x00\xFF", "\xFF\xFF\xFF\x00\xFF\xFF\xFF\x00", "\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF", "\xFF\xFF\xFF\xFF\x00\x00\x00\x00", "\xFF\xFF\xFF\xFF\x00\x00\x00\xFF", "\xFF\xFF\xFF\xFF\x00\x00\xFF\x00", "\xFF\xFF\xFF\xFF\x00\x00\xFF\xFF", "\xFF\xFF\xFF\xFF\x00\xFF\x00\x00", "\xFF\xFF\xFF\xFF\x00\xFF\x00\xFF", "\xFF\xFF\xFF\xFF\x00\xFF\xFF\x00", "\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF", "\xFF\xFF\xFF\xFF\xFF\x00\x00\x00", "\xFF\xFF\xFF\xFF\xFF\x00\x00\xFF", "\xFF\xFF\xFF\xFF\xFF\x00\xFF\x00", "\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF", "\xFF\xFF\xFF\xFF\xFF\xFF\x00\x00", "\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF", "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00", "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" ); /** * IP mapping helper table. * * Indexing this table with each source byte performs the initial bit permutation. * * @var array * @access private */ var $ipmap = array( 0x00, 0x10, 0x01, 0x11, 0x20, 0x30, 0x21, 0x31, 0x02, 0x12, 0x03, 0x13, 0x22, 0x32, 0x23, 0x33, 0x40, 0x50, 0x41, 0x51, 0x60, 0x70, 0x61, 0x71, 0x42, 0x52, 0x43, 0x53, 0x62, 0x72, 0x63, 0x73, 0x04, 0x14, 0x05, 0x15, 0x24, 0x34, 0x25, 0x35, 0x06, 0x16, 0x07, 0x17, 0x26, 0x36, 0x27, 0x37, 0x44, 0x54, 0x45, 0x55, 0x64, 0x74, 0x65, 0x75, 0x46, 0x56, 0x47, 0x57, 0x66, 0x76, 0x67, 0x77, 0x80, 0x90, 0x81, 0x91, 0xA0, 0xB0, 0xA1, 0xB1, 0x82, 0x92, 0x83, 0x93, 0xA2, 0xB2, 0xA3, 0xB3, 0xC0, 0xD0, 0xC1, 0xD1, 0xE0, 0xF0, 0xE1, 0xF1, 0xC2, 0xD2, 0xC3, 0xD3, 0xE2, 0xF2, 0xE3, 0xF3, 0x84, 0x94, 0x85, 0x95, 0xA4, 0xB4, 0xA5, 0xB5, 0x86, 0x96, 0x87, 0x97, 0xA6, 0xB6, 0xA7, 0xB7, 0xC4, 0xD4, 0xC5, 0xD5, 0xE4, 0xF4, 0xE5, 0xF5, 0xC6, 0xD6, 0xC7, 0xD7, 0xE6, 0xF6, 0xE7, 0xF7, 0x08, 0x18, 0x09, 0x19, 0x28, 0x38, 0x29, 0x39, 0x0A, 0x1A, 0x0B, 0x1B, 0x2A, 0x3A, 0x2B, 0x3B, 0x48, 0x58, 0x49, 0x59, 0x68, 0x78, 0x69, 0x79, 0x4A, 0x5A, 0x4B, 0x5B, 0x6A, 0x7A, 0x6B, 0x7B, 0x0C, 0x1C, 0x0D, 0x1D, 0x2C, 0x3C, 0x2D, 0x3D, 0x0E, 0x1E, 0x0F, 0x1F, 0x2E, 0x3E, 0x2F, 0x3F, 0x4C, 0x5C, 0x4D, 0x5D, 0x6C, 0x7C, 0x6D, 0x7D, 0x4E, 0x5E, 0x4F, 0x5F, 0x6E, 0x7E, 0x6F, 0x7F, 0x88, 0x98, 0x89, 0x99, 0xA8, 0xB8, 0xA9, 0xB9, 0x8A, 0x9A, 0x8B, 0x9B, 0xAA, 0xBA, 0xAB, 0xBB, 0xC8, 0xD8, 0xC9, 0xD9, 0xE8, 0xF8, 0xE9, 0xF9, 0xCA, 0xDA, 0xCB, 0xDB, 0xEA, 0xFA, 0xEB, 0xFB, 0x8C, 0x9C, 0x8D, 0x9D, 0xAC, 0xBC, 0xAD, 0xBD, 0x8E, 0x9E, 0x8F, 0x9F, 0xAE, 0xBE, 0xAF, 0xBF, 0xCC, 0xDC, 0xCD, 0xDD, 0xEC, 0xFC, 0xED, 0xFD, 0xCE, 0xDE, 0xCF, 0xDF, 0xEE, 0xFE, 0xEF, 0xFF ); /** * Inverse IP mapping helper table. * Indexing this table with a byte value reverses the bit order. * * @var array * @access private */ var $invipmap = array( 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF ); /** * Pre-permuted S-box1 * * Each box ($sbox1-$sbox8) has been vectorized, then each value pre-permuted using the * P table: concatenation can then be replaced by exclusive ORs. * * @var array * @access private */ var $sbox1 = array( 0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202, 0x00000002, 0x00008000, 0x00000200, 0x00808200, 0x00808202, 0x00000200, 0x00800202, 0x00808002, 0x00800000, 0x00000002, 0x00000202, 0x00800200, 0x00800200, 0x00008200, 0x00008200, 0x00808000, 0x00808000, 0x00800202, 0x00008002, 0x00800002, 0x00800002, 0x00008002, 0x00000000, 0x00000202, 0x00008202, 0x00800000, 0x00008000, 0x00808202, 0x00000002, 0x00808000, 0x00808200, 0x00800000, 0x00800000, 0x00000200, 0x00808002, 0x00008000, 0x00008200, 0x00800002, 0x00000200, 0x00000002, 0x00800202, 0x00008202, 0x00808202, 0x00008002, 0x00808000, 0x00800202, 0x00800002, 0x00000202, 0x00008202, 0x00808200, 0x00000202, 0x00800200, 0x00800200, 0x00000000, 0x00008002, 0x00008200, 0x00000000, 0x00808002 ); /** * Pre-permuted S-box2 * * @var array * @access private */ var $sbox2 = array( 0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x00080000, 0x00000010, 0x40080010, 0x40004010, 0x40000010, 0x40084010, 0x40084000, 0x40000000, 0x40004000, 0x00080000, 0x00000010, 0x40080010, 0x00084000, 0x00080010, 0x40004010, 0x00000000, 0x40000000, 0x00004000, 0x00084010, 0x40080000, 0x00080010, 0x40000010, 0x00000000, 0x00084000, 0x00004010, 0x40084000, 0x40080000, 0x00004010, 0x00000000, 0x00084010, 0x40080010, 0x00080000, 0x40004010, 0x40080000, 0x40084000, 0x00004000, 0x40080000, 0x40004000, 0x00000010, 0x40084010, 0x00084010, 0x00000010, 0x00004000, 0x40000000, 0x00004010, 0x40084000, 0x00080000, 0x40000010, 0x00080010, 0x40004010, 0x40000010, 0x00080010, 0x00084000, 0x00000000, 0x40004000, 0x00004010, 0x40000000, 0x40080010, 0x40084010, 0x00084000 ); /** * Pre-permuted S-box3 * * @var array * @access private */ var $sbox3 = array( 0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x04000100, 0x00000000, 0x00010104, 0x04000100, 0x00010004, 0x04000004, 0x04000004, 0x00010000, 0x04010104, 0x00010004, 0x04010000, 0x00000104, 0x04000000, 0x00000004, 0x04010100, 0x00000100, 0x00010100, 0x04010000, 0x04010004, 0x00010104, 0x04000104, 0x00010100, 0x00010000, 0x04000104, 0x00000004, 0x04010104, 0x00000100, 0x04000000, 0x04010100, 0x04000000, 0x00010004, 0x00000104, 0x00010000, 0x04010100, 0x04000100, 0x00000000, 0x00000100, 0x00010004, 0x04010104, 0x04000100, 0x04000004, 0x00000100, 0x00000000, 0x04010004, 0x04000104, 0x00010000, 0x04000000, 0x04010104, 0x00000004, 0x00010104, 0x00010100, 0x04000004, 0x04010000, 0x04000104, 0x00000104, 0x04010000, 0x00010104, 0x00000004, 0x04010004, 0x00010100 ); /** * Pre-permuted S-box4 * * @var array * @access private */ var $sbox4 = array( 0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x00401040, 0x80400040, 0x80400000, 0x80001000, 0x00000000, 0x00401000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00400040, 0x80400000, 0x80000000, 0x00001000, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x80001000, 0x00001040, 0x80400040, 0x80000000, 0x00001040, 0x00400040, 0x00001000, 0x00401040, 0x80401040, 0x80000040, 0x00400040, 0x80400000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00000000, 0x00401000, 0x00001040, 0x00400040, 0x80400040, 0x80000000, 0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x80401040, 0x80000040, 0x80000000, 0x00001000, 0x80400000, 0x80001000, 0x00401040, 0x80400040, 0x80001000, 0x00001040, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x00001000, 0x00401040 ); /** * Pre-permuted S-box5 * * @var array * @access private */ var $sbox5 = array( 0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00040000, 0x00000080, 0x20000000, 0x01040000, 0x20040080, 0x00040000, 0x01000080, 0x20040080, 0x21000080, 0x21040000, 0x00040080, 0x20000000, 0x01000000, 0x20040000, 0x20040000, 0x00000000, 0x20000080, 0x21040080, 0x21040080, 0x01000080, 0x21040000, 0x20000080, 0x00000000, 0x21000000, 0x01040080, 0x01000000, 0x21000000, 0x00040080, 0x00040000, 0x21000080, 0x00000080, 0x01000000, 0x20000000, 0x01040000, 0x21000080, 0x20040080, 0x01000080, 0x20000000, 0x21040000, 0x01040080, 0x20040080, 0x00000080, 0x01000000, 0x21040000, 0x21040080, 0x00040080, 0x21000000, 0x21040080, 0x01040000, 0x00000000, 0x20040000, 0x21000000, 0x00040080, 0x01000080, 0x20000080, 0x00040000, 0x00000000, 0x20040000, 0x01040080, 0x20000080 ); /** * Pre-permuted S-box6 * * @var array * @access private */ var $sbox6 = array( 0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10200000, 0x00000008, 0x10202008, 0x00200000, 0x10002000, 0x00202008, 0x00200000, 0x10000008, 0x00200008, 0x10002000, 0x10000000, 0x00002008, 0x00000000, 0x00200008, 0x10002008, 0x00002000, 0x00202000, 0x10002008, 0x00000008, 0x10200008, 0x10200008, 0x00000000, 0x00202008, 0x10202000, 0x00002008, 0x00202000, 0x10202000, 0x10000000, 0x10002000, 0x00000008, 0x10200008, 0x00202000, 0x10202008, 0x00200000, 0x00002008, 0x10000008, 0x00200000, 0x10002000, 0x10000000, 0x00002008, 0x10000008, 0x10202008, 0x00202000, 0x10200000, 0x00202008, 0x10202000, 0x00000000, 0x10200008, 0x00000008, 0x00002000, 0x10200000, 0x00202008, 0x00002000, 0x00200008, 0x10002008, 0x00000000, 0x10202000, 0x10000000, 0x00200008, 0x10002008 ); /** * Pre-permuted S-box7 * * @var array * @access private */ var $sbox7 = array( 0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00000400, 0x02000401, 0x00100401, 0x02100400, 0x02100401, 0x00100000, 0x00000000, 0x02000001, 0x00000001, 0x02000000, 0x02100001, 0x00000401, 0x02000400, 0x00100401, 0x00100001, 0x02000400, 0x02000001, 0x02100000, 0x02100400, 0x00100001, 0x02100000, 0x00000400, 0x00000401, 0x02100401, 0x00100400, 0x00000001, 0x02000000, 0x00100400, 0x02000000, 0x00100400, 0x00100000, 0x02000401, 0x02000401, 0x02100001, 0x02100001, 0x00000001, 0x00100001, 0x02000000, 0x02000400, 0x00100000, 0x02100400, 0x00000401, 0x00100401, 0x02100400, 0x00000401, 0x02000001, 0x02100401, 0x02100000, 0x00100400, 0x00000000, 0x00000001, 0x02100401, 0x00000000, 0x00100401, 0x02100000, 0x00000400, 0x02000001, 0x02000400, 0x00000400, 0x00100001 ); /** * Pre-permuted S-box8 * * @var array * @access private */ var $sbox8 = array( 0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000000, 0x08000820, 0x00000020, 0x08000000, 0x00020020, 0x08020000, 0x08020820, 0x00020800, 0x08020800, 0x00020820, 0x00000800, 0x00000020, 0x08020000, 0x08000020, 0x08000800, 0x00000820, 0x00020800, 0x00020020, 0x08020020, 0x08020800, 0x00000820, 0x00000000, 0x00000000, 0x08020020, 0x08000020, 0x08000800, 0x00020820, 0x00020000, 0x00020820, 0x00020000, 0x08020800, 0x00000800, 0x00000020, 0x08020020, 0x00000800, 0x00020820, 0x08000800, 0x00000020, 0x08000020, 0x08020000, 0x08020020, 0x08000000, 0x00020000, 0x08000820, 0x00000000, 0x08020820, 0x00020020, 0x08000020, 0x08020000, 0x08000800, 0x08000820, 0x00000000, 0x08020820, 0x00020800, 0x00020800, 0x00000820, 0x00000820, 0x00020020, 0x08000000, 0x08020800 ); /** * Test for engine validity * * This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine() * * @see \phpseclib\Crypt\Base::isValidEngine() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { if ($this->key_length_max == 8) { if ($engine == self::ENGINE_OPENSSL) { $this->cipher_name_openssl_ecb = 'des-ecb'; $this->cipher_name_openssl = 'des-' . $this->_openssl_translate_mode(); } } return parent::isValidEngine($engine); } /** * Sets the key. * * Keys can be of any length. DES, itself, uses 64-bit keys (eg. strlen($key) == 8), however, we * only use the first eight, if $key has more then eight characters in it, and pad $key with the * null byte if it is less then eight characters long. * * DES also requires that every eighth bit be a parity bit, however, we'll ignore that. * * If the key is not explicitly set, it'll be assumed to be all zero's. * * @see \phpseclib\Crypt\Base::setKey() * @access public * @param string $key */ function setKey($key) { // We check/cut here only up to max length of the key. // Key padding to the proper length will be done in _setupKey() if (strlen($key) > $this->key_length_max) { $key = substr($key, 0, $this->key_length_max); } // Sets the key parent::setKey($key); } /** * Encrypts a block * * @see \phpseclib\Crypt\Base::_encryptBlock() * @see \phpseclib\Crypt\Base::encrypt() * @see self::encrypt() * @access private * @param string $in * @return string */ function _encryptBlock($in) { return $this->_processBlock($in, self::ENCRYPT); } /** * Decrypts a block * * @see \phpseclib\Crypt\Base::_decryptBlock() * @see \phpseclib\Crypt\Base::decrypt() * @see self::decrypt() * @access private * @param string $in * @return string */ function _decryptBlock($in) { return $this->_processBlock($in, self::DECRYPT); } /** * Encrypts or decrypts a 64-bit block * * $mode should be either self::ENCRYPT or self::DECRYPT. See * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general * idea of what this function does. * * @see self::_encryptBlock() * @see self::_decryptBlock() * @access private * @param string $block * @param int $mode * @return string */ function _processBlock($block, $mode) { static $sbox1, $sbox2, $sbox3, $sbox4, $sbox5, $sbox6, $sbox7, $sbox8, $shuffleip, $shuffleinvip; if (!$sbox1) { $sbox1 = array_map("intval", $this->sbox1); $sbox2 = array_map("intval", $this->sbox2); $sbox3 = array_map("intval", $this->sbox3); $sbox4 = array_map("intval", $this->sbox4); $sbox5 = array_map("intval", $this->sbox5); $sbox6 = array_map("intval", $this->sbox6); $sbox7 = array_map("intval", $this->sbox7); $sbox8 = array_map("intval", $this->sbox8); /* Merge $shuffle with $[inv]ipmap */ for ($i = 0; $i < 256; ++$i) { $shuffleip[] = $this->shuffle[$this->ipmap[$i]]; $shuffleinvip[] = $this->shuffle[$this->invipmap[$i]]; } } $keys = $this->keys[$mode]; $ki = -1; // Do the initial IP permutation. $t = unpack('Nl/Nr', $block); list($l, $r) = array($t['l'], $t['r']); $block = ($shuffleip[ $r & 0xFF] & "\x80\x80\x80\x80\x80\x80\x80\x80") | ($shuffleip[($r >> 8) & 0xFF] & "\x40\x40\x40\x40\x40\x40\x40\x40") | ($shuffleip[($r >> 16) & 0xFF] & "\x20\x20\x20\x20\x20\x20\x20\x20") | ($shuffleip[($r >> 24) & 0xFF] & "\x10\x10\x10\x10\x10\x10\x10\x10") | ($shuffleip[ $l & 0xFF] & "\x08\x08\x08\x08\x08\x08\x08\x08") | ($shuffleip[($l >> 8) & 0xFF] & "\x04\x04\x04\x04\x04\x04\x04\x04") | ($shuffleip[($l >> 16) & 0xFF] & "\x02\x02\x02\x02\x02\x02\x02\x02") | ($shuffleip[($l >> 24) & 0xFF] & "\x01\x01\x01\x01\x01\x01\x01\x01"); // Extract L0 and R0. $t = unpack('Nl/Nr', $block); list($l, $r) = array($t['l'], $t['r']); for ($des_round = 0; $des_round < $this->des_rounds; ++$des_round) { // Perform the 16 steps. for ($i = 0; $i < 16; $i++) { // start of "the Feistel (F) function" - see the following URL: // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png // Merge key schedule. $b1 = (($r >> 3) & 0x1FFFFFFF) ^ ($r << 29) ^ $keys[++$ki]; $b2 = (($r >> 31) & 0x00000001) ^ ($r << 1) ^ $keys[++$ki]; // S-box indexing. $t = $sbox1[($b1 >> 24) & 0x3F] ^ $sbox2[($b2 >> 24) & 0x3F] ^ $sbox3[($b1 >> 16) & 0x3F] ^ $sbox4[($b2 >> 16) & 0x3F] ^ $sbox5[($b1 >> 8) & 0x3F] ^ $sbox6[($b2 >> 8) & 0x3F] ^ $sbox7[ $b1 & 0x3F] ^ $sbox8[ $b2 & 0x3F] ^ $l; // end of "the Feistel (F) function" $l = $r; $r = $t; } // Last step should not permute L & R. $t = $l; $l = $r; $r = $t; } // Perform the inverse IP permutation. return ($shuffleinvip[($r >> 24) & 0xFF] & "\x80\x80\x80\x80\x80\x80\x80\x80") | ($shuffleinvip[($l >> 24) & 0xFF] & "\x40\x40\x40\x40\x40\x40\x40\x40") | ($shuffleinvip[($r >> 16) & 0xFF] & "\x20\x20\x20\x20\x20\x20\x20\x20") | ($shuffleinvip[($l >> 16) & 0xFF] & "\x10\x10\x10\x10\x10\x10\x10\x10") | ($shuffleinvip[($r >> 8) & 0xFF] & "\x08\x08\x08\x08\x08\x08\x08\x08") | ($shuffleinvip[($l >> 8) & 0xFF] & "\x04\x04\x04\x04\x04\x04\x04\x04") | ($shuffleinvip[ $r & 0xFF] & "\x02\x02\x02\x02\x02\x02\x02\x02") | ($shuffleinvip[ $l & 0xFF] & "\x01\x01\x01\x01\x01\x01\x01\x01"); } /** * Creates the key schedule * * @see \phpseclib\Crypt\Base::_setupKey() * @access private */ function _setupKey() { if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->des_rounds === $this->kl['des_rounds']) { // already expanded return; } $this->kl = array('key' => $this->key, 'des_rounds' => $this->des_rounds); static $shifts = array( // number of key bits shifted per round 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 ); static $pc1map = array( 0x00, 0x00, 0x08, 0x08, 0x04, 0x04, 0x0C, 0x0C, 0x02, 0x02, 0x0A, 0x0A, 0x06, 0x06, 0x0E, 0x0E, 0x10, 0x10, 0x18, 0x18, 0x14, 0x14, 0x1C, 0x1C, 0x12, 0x12, 0x1A, 0x1A, 0x16, 0x16, 0x1E, 0x1E, 0x20, 0x20, 0x28, 0x28, 0x24, 0x24, 0x2C, 0x2C, 0x22, 0x22, 0x2A, 0x2A, 0x26, 0x26, 0x2E, 0x2E, 0x30, 0x30, 0x38, 0x38, 0x34, 0x34, 0x3C, 0x3C, 0x32, 0x32, 0x3A, 0x3A, 0x36, 0x36, 0x3E, 0x3E, 0x40, 0x40, 0x48, 0x48, 0x44, 0x44, 0x4C, 0x4C, 0x42, 0x42, 0x4A, 0x4A, 0x46, 0x46, 0x4E, 0x4E, 0x50, 0x50, 0x58, 0x58, 0x54, 0x54, 0x5C, 0x5C, 0x52, 0x52, 0x5A, 0x5A, 0x56, 0x56, 0x5E, 0x5E, 0x60, 0x60, 0x68, 0x68, 0x64, 0x64, 0x6C, 0x6C, 0x62, 0x62, 0x6A, 0x6A, 0x66, 0x66, 0x6E, 0x6E, 0x70, 0x70, 0x78, 0x78, 0x74, 0x74, 0x7C, 0x7C, 0x72, 0x72, 0x7A, 0x7A, 0x76, 0x76, 0x7E, 0x7E, 0x80, 0x80, 0x88, 0x88, 0x84, 0x84, 0x8C, 0x8C, 0x82, 0x82, 0x8A, 0x8A, 0x86, 0x86, 0x8E, 0x8E, 0x90, 0x90, 0x98, 0x98, 0x94, 0x94, 0x9C, 0x9C, 0x92, 0x92, 0x9A, 0x9A, 0x96, 0x96, 0x9E, 0x9E, 0xA0, 0xA0, 0xA8, 0xA8, 0xA4, 0xA4, 0xAC, 0xAC, 0xA2, 0xA2, 0xAA, 0xAA, 0xA6, 0xA6, 0xAE, 0xAE, 0xB0, 0xB0, 0xB8, 0xB8, 0xB4, 0xB4, 0xBC, 0xBC, 0xB2, 0xB2, 0xBA, 0xBA, 0xB6, 0xB6, 0xBE, 0xBE, 0xC0, 0xC0, 0xC8, 0xC8, 0xC4, 0xC4, 0xCC, 0xCC, 0xC2, 0xC2, 0xCA, 0xCA, 0xC6, 0xC6, 0xCE, 0xCE, 0xD0, 0xD0, 0xD8, 0xD8, 0xD4, 0xD4, 0xDC, 0xDC, 0xD2, 0xD2, 0xDA, 0xDA, 0xD6, 0xD6, 0xDE, 0xDE, 0xE0, 0xE0, 0xE8, 0xE8, 0xE4, 0xE4, 0xEC, 0xEC, 0xE2, 0xE2, 0xEA, 0xEA, 0xE6, 0xE6, 0xEE, 0xEE, 0xF0, 0xF0, 0xF8, 0xF8, 0xF4, 0xF4, 0xFC, 0xFC, 0xF2, 0xF2, 0xFA, 0xFA, 0xF6, 0xF6, 0xFE, 0xFE ); // Mapping tables for the PC-2 transformation. static $pc2mapc1 = array( 0x00000000, 0x00000400, 0x00200000, 0x00200400, 0x00000001, 0x00000401, 0x00200001, 0x00200401, 0x02000000, 0x02000400, 0x02200000, 0x02200400, 0x02000001, 0x02000401, 0x02200001, 0x02200401 ); static $pc2mapc2 = array( 0x00000000, 0x00000800, 0x08000000, 0x08000800, 0x00010000, 0x00010800, 0x08010000, 0x08010800, 0x00000000, 0x00000800, 0x08000000, 0x08000800, 0x00010000, 0x00010800, 0x08010000, 0x08010800, 0x00000100, 0x00000900, 0x08000100, 0x08000900, 0x00010100, 0x00010900, 0x08010100, 0x08010900, 0x00000100, 0x00000900, 0x08000100, 0x08000900, 0x00010100, 0x00010900, 0x08010100, 0x08010900, 0x00000010, 0x00000810, 0x08000010, 0x08000810, 0x00010010, 0x00010810, 0x08010010, 0x08010810, 0x00000010, 0x00000810, 0x08000010, 0x08000810, 0x00010010, 0x00010810, 0x08010010, 0x08010810, 0x00000110, 0x00000910, 0x08000110, 0x08000910, 0x00010110, 0x00010910, 0x08010110, 0x08010910, 0x00000110, 0x00000910, 0x08000110, 0x08000910, 0x00010110, 0x00010910, 0x08010110, 0x08010910, 0x00040000, 0x00040800, 0x08040000, 0x08040800, 0x00050000, 0x00050800, 0x08050000, 0x08050800, 0x00040000, 0x00040800, 0x08040000, 0x08040800, 0x00050000, 0x00050800, 0x08050000, 0x08050800, 0x00040100, 0x00040900, 0x08040100, 0x08040900, 0x00050100, 0x00050900, 0x08050100, 0x08050900, 0x00040100, 0x00040900, 0x08040100, 0x08040900, 0x00050100, 0x00050900, 0x08050100, 0x08050900, 0x00040010, 0x00040810, 0x08040010, 0x08040810, 0x00050010, 0x00050810, 0x08050010, 0x08050810, 0x00040010, 0x00040810, 0x08040010, 0x08040810, 0x00050010, 0x00050810, 0x08050010, 0x08050810, 0x00040110, 0x00040910, 0x08040110, 0x08040910, 0x00050110, 0x00050910, 0x08050110, 0x08050910, 0x00040110, 0x00040910, 0x08040110, 0x08040910, 0x00050110, 0x00050910, 0x08050110, 0x08050910, 0x01000000, 0x01000800, 0x09000000, 0x09000800, 0x01010000, 0x01010800, 0x09010000, 0x09010800, 0x01000000, 0x01000800, 0x09000000, 0x09000800, 0x01010000, 0x01010800, 0x09010000, 0x09010800, 0x01000100, 0x01000900, 0x09000100, 0x09000900, 0x01010100, 0x01010900, 0x09010100, 0x09010900, 0x01000100, 0x01000900, 0x09000100, 0x09000900, 0x01010100, 0x01010900, 0x09010100, 0x09010900, 0x01000010, 0x01000810, 0x09000010, 0x09000810, 0x01010010, 0x01010810, 0x09010010, 0x09010810, 0x01000010, 0x01000810, 0x09000010, 0x09000810, 0x01010010, 0x01010810, 0x09010010, 0x09010810, 0x01000110, 0x01000910, 0x09000110, 0x09000910, 0x01010110, 0x01010910, 0x09010110, 0x09010910, 0x01000110, 0x01000910, 0x09000110, 0x09000910, 0x01010110, 0x01010910, 0x09010110, 0x09010910, 0x01040000, 0x01040800, 0x09040000, 0x09040800, 0x01050000, 0x01050800, 0x09050000, 0x09050800, 0x01040000, 0x01040800, 0x09040000, 0x09040800, 0x01050000, 0x01050800, 0x09050000, 0x09050800, 0x01040100, 0x01040900, 0x09040100, 0x09040900, 0x01050100, 0x01050900, 0x09050100, 0x09050900, 0x01040100, 0x01040900, 0x09040100, 0x09040900, 0x01050100, 0x01050900, 0x09050100, 0x09050900, 0x01040010, 0x01040810, 0x09040010, 0x09040810, 0x01050010, 0x01050810, 0x09050010, 0x09050810, 0x01040010, 0x01040810, 0x09040010, 0x09040810, 0x01050010, 0x01050810, 0x09050010, 0x09050810, 0x01040110, 0x01040910, 0x09040110, 0x09040910, 0x01050110, 0x01050910, 0x09050110, 0x09050910, 0x01040110, 0x01040910, 0x09040110, 0x09040910, 0x01050110, 0x01050910, 0x09050110, 0x09050910 ); static $pc2mapc3 = array( 0x00000000, 0x00000004, 0x00001000, 0x00001004, 0x00000000, 0x00000004, 0x00001000, 0x00001004, 0x10000000, 0x10000004, 0x10001000, 0x10001004, 0x10000000, 0x10000004, 0x10001000, 0x10001004, 0x00000020, 0x00000024, 0x00001020, 0x00001024, 0x00000020, 0x00000024, 0x00001020, 0x00001024, 0x10000020, 0x10000024, 0x10001020, 0x10001024, 0x10000020, 0x10000024, 0x10001020, 0x10001024, 0x00080000, 0x00080004, 0x00081000, 0x00081004, 0x00080000, 0x00080004, 0x00081000, 0x00081004, 0x10080000, 0x10080004, 0x10081000, 0x10081004, 0x10080000, 0x10080004, 0x10081000, 0x10081004, 0x00080020, 0x00080024, 0x00081020, 0x00081024, 0x00080020, 0x00080024, 0x00081020, 0x00081024, 0x10080020, 0x10080024, 0x10081020, 0x10081024, 0x10080020, 0x10080024, 0x10081020, 0x10081024, 0x20000000, 0x20000004, 0x20001000, 0x20001004, 0x20000000, 0x20000004, 0x20001000, 0x20001004, 0x30000000, 0x30000004, 0x30001000, 0x30001004, 0x30000000, 0x30000004, 0x30001000, 0x30001004, 0x20000020, 0x20000024, 0x20001020, 0x20001024, 0x20000020, 0x20000024, 0x20001020, 0x20001024, 0x30000020, 0x30000024, 0x30001020, 0x30001024, 0x30000020, 0x30000024, 0x30001020, 0x30001024, 0x20080000, 0x20080004, 0x20081000, 0x20081004, 0x20080000, 0x20080004, 0x20081000, 0x20081004, 0x30080000, 0x30080004, 0x30081000, 0x30081004, 0x30080000, 0x30080004, 0x30081000, 0x30081004, 0x20080020, 0x20080024, 0x20081020, 0x20081024, 0x20080020, 0x20080024, 0x20081020, 0x20081024, 0x30080020, 0x30080024, 0x30081020, 0x30081024, 0x30080020, 0x30080024, 0x30081020, 0x30081024, 0x00000002, 0x00000006, 0x00001002, 0x00001006, 0x00000002, 0x00000006, 0x00001002, 0x00001006, 0x10000002, 0x10000006, 0x10001002, 0x10001006, 0x10000002, 0x10000006, 0x10001002, 0x10001006, 0x00000022, 0x00000026, 0x00001022, 0x00001026, 0x00000022, 0x00000026, 0x00001022, 0x00001026, 0x10000022, 0x10000026, 0x10001022, 0x10001026, 0x10000022, 0x10000026, 0x10001022, 0x10001026, 0x00080002, 0x00080006, 0x00081002, 0x00081006, 0x00080002, 0x00080006, 0x00081002, 0x00081006, 0x10080002, 0x10080006, 0x10081002, 0x10081006, 0x10080002, 0x10080006, 0x10081002, 0x10081006, 0x00080022, 0x00080026, 0x00081022, 0x00081026, 0x00080022, 0x00080026, 0x00081022, 0x00081026, 0x10080022, 0x10080026, 0x10081022, 0x10081026, 0x10080022, 0x10080026, 0x10081022, 0x10081026, 0x20000002, 0x20000006, 0x20001002, 0x20001006, 0x20000002, 0x20000006, 0x20001002, 0x20001006, 0x30000002, 0x30000006, 0x30001002, 0x30001006, 0x30000002, 0x30000006, 0x30001002, 0x30001006, 0x20000022, 0x20000026, 0x20001022, 0x20001026, 0x20000022, 0x20000026, 0x20001022, 0x20001026, 0x30000022, 0x30000026, 0x30001022, 0x30001026, 0x30000022, 0x30000026, 0x30001022, 0x30001026, 0x20080002, 0x20080006, 0x20081002, 0x20081006, 0x20080002, 0x20080006, 0x20081002, 0x20081006, 0x30080002, 0x30080006, 0x30081002, 0x30081006, 0x30080002, 0x30080006, 0x30081002, 0x30081006, 0x20080022, 0x20080026, 0x20081022, 0x20081026, 0x20080022, 0x20080026, 0x20081022, 0x20081026, 0x30080022, 0x30080026, 0x30081022, 0x30081026, 0x30080022, 0x30080026, 0x30081022, 0x30081026 ); static $pc2mapc4 = array( 0x00000000, 0x00100000, 0x00000008, 0x00100008, 0x00000200, 0x00100200, 0x00000208, 0x00100208, 0x00000000, 0x00100000, 0x00000008, 0x00100008, 0x00000200, 0x00100200, 0x00000208, 0x00100208, 0x04000000, 0x04100000, 0x04000008, 0x04100008, 0x04000200, 0x04100200, 0x04000208, 0x04100208, 0x04000000, 0x04100000, 0x04000008, 0x04100008, 0x04000200, 0x04100200, 0x04000208, 0x04100208, 0x00002000, 0x00102000, 0x00002008, 0x00102008, 0x00002200, 0x00102200, 0x00002208, 0x00102208, 0x00002000, 0x00102000, 0x00002008, 0x00102008, 0x00002200, 0x00102200, 0x00002208, 0x00102208, 0x04002000, 0x04102000, 0x04002008, 0x04102008, 0x04002200, 0x04102200, 0x04002208, 0x04102208, 0x04002000, 0x04102000, 0x04002008, 0x04102008, 0x04002200, 0x04102200, 0x04002208, 0x04102208, 0x00000000, 0x00100000, 0x00000008, 0x00100008, 0x00000200, 0x00100200, 0x00000208, 0x00100208, 0x00000000, 0x00100000, 0x00000008, 0x00100008, 0x00000200, 0x00100200, 0x00000208, 0x00100208, 0x04000000, 0x04100000, 0x04000008, 0x04100008, 0x04000200, 0x04100200, 0x04000208, 0x04100208, 0x04000000, 0x04100000, 0x04000008, 0x04100008, 0x04000200, 0x04100200, 0x04000208, 0x04100208, 0x00002000, 0x00102000, 0x00002008, 0x00102008, 0x00002200, 0x00102200, 0x00002208, 0x00102208, 0x00002000, 0x00102000, 0x00002008, 0x00102008, 0x00002200, 0x00102200, 0x00002208, 0x00102208, 0x04002000, 0x04102000, 0x04002008, 0x04102008, 0x04002200, 0x04102200, 0x04002208, 0x04102208, 0x04002000, 0x04102000, 0x04002008, 0x04102008, 0x04002200, 0x04102200, 0x04002208, 0x04102208, 0x00020000, 0x00120000, 0x00020008, 0x00120008, 0x00020200, 0x00120200, 0x00020208, 0x00120208, 0x00020000, 0x00120000, 0x00020008, 0x00120008, 0x00020200, 0x00120200, 0x00020208, 0x00120208, 0x04020000, 0x04120000, 0x04020008, 0x04120008, 0x04020200, 0x04120200, 0x04020208, 0x04120208, 0x04020000, 0x04120000, 0x04020008, 0x04120008, 0x04020200, 0x04120200, 0x04020208, 0x04120208, 0x00022000, 0x00122000, 0x00022008, 0x00122008, 0x00022200, 0x00122200, 0x00022208, 0x00122208, 0x00022000, 0x00122000, 0x00022008, 0x00122008, 0x00022200, 0x00122200, 0x00022208, 0x00122208, 0x04022000, 0x04122000, 0x04022008, 0x04122008, 0x04022200, 0x04122200, 0x04022208, 0x04122208, 0x04022000, 0x04122000, 0x04022008, 0x04122008, 0x04022200, 0x04122200, 0x04022208, 0x04122208, 0x00020000, 0x00120000, 0x00020008, 0x00120008, 0x00020200, 0x00120200, 0x00020208, 0x00120208, 0x00020000, 0x00120000, 0x00020008, 0x00120008, 0x00020200, 0x00120200, 0x00020208, 0x00120208, 0x04020000, 0x04120000, 0x04020008, 0x04120008, 0x04020200, 0x04120200, 0x04020208, 0x04120208, 0x04020000, 0x04120000, 0x04020008, 0x04120008, 0x04020200, 0x04120200, 0x04020208, 0x04120208, 0x00022000, 0x00122000, 0x00022008, 0x00122008, 0x00022200, 0x00122200, 0x00022208, 0x00122208, 0x00022000, 0x00122000, 0x00022008, 0x00122008, 0x00022200, 0x00122200, 0x00022208, 0x00122208, 0x04022000, 0x04122000, 0x04022008, 0x04122008, 0x04022200, 0x04122200, 0x04022208, 0x04122208, 0x04022000, 0x04122000, 0x04022008, 0x04122008, 0x04022200, 0x04122200, 0x04022208, 0x04122208 ); static $pc2mapd1 = array( 0x00000000, 0x00000001, 0x08000000, 0x08000001, 0x00200000, 0x00200001, 0x08200000, 0x08200001, 0x00000002, 0x00000003, 0x08000002, 0x08000003, 0x00200002, 0x00200003, 0x08200002, 0x08200003 ); static $pc2mapd2 = array( 0x00000000, 0x00100000, 0x00000800, 0x00100800, 0x00000000, 0x00100000, 0x00000800, 0x00100800, 0x04000000, 0x04100000, 0x04000800, 0x04100800, 0x04000000, 0x04100000, 0x04000800, 0x04100800, 0x00000004, 0x00100004, 0x00000804, 0x00100804, 0x00000004, 0x00100004, 0x00000804, 0x00100804, 0x04000004, 0x04100004, 0x04000804, 0x04100804, 0x04000004, 0x04100004, 0x04000804, 0x04100804, 0x00000000, 0x00100000, 0x00000800, 0x00100800, 0x00000000, 0x00100000, 0x00000800, 0x00100800, 0x04000000, 0x04100000, 0x04000800, 0x04100800, 0x04000000, 0x04100000, 0x04000800, 0x04100800, 0x00000004, 0x00100004, 0x00000804, 0x00100804, 0x00000004, 0x00100004, 0x00000804, 0x00100804, 0x04000004, 0x04100004, 0x04000804, 0x04100804, 0x04000004, 0x04100004, 0x04000804, 0x04100804, 0x00000200, 0x00100200, 0x00000A00, 0x00100A00, 0x00000200, 0x00100200, 0x00000A00, 0x00100A00, 0x04000200, 0x04100200, 0x04000A00, 0x04100A00, 0x04000200, 0x04100200, 0x04000A00, 0x04100A00, 0x00000204, 0x00100204, 0x00000A04, 0x00100A04, 0x00000204, 0x00100204, 0x00000A04, 0x00100A04, 0x04000204, 0x04100204, 0x04000A04, 0x04100A04, 0x04000204, 0x04100204, 0x04000A04, 0x04100A04, 0x00000200, 0x00100200, 0x00000A00, 0x00100A00, 0x00000200, 0x00100200, 0x00000A00, 0x00100A00, 0x04000200, 0x04100200, 0x04000A00, 0x04100A00, 0x04000200, 0x04100200, 0x04000A00, 0x04100A00, 0x00000204, 0x00100204, 0x00000A04, 0x00100A04, 0x00000204, 0x00100204, 0x00000A04, 0x00100A04, 0x04000204, 0x04100204, 0x04000A04, 0x04100A04, 0x04000204, 0x04100204, 0x04000A04, 0x04100A04, 0x00020000, 0x00120000, 0x00020800, 0x00120800, 0x00020000, 0x00120000, 0x00020800, 0x00120800, 0x04020000, 0x04120000, 0x04020800, 0x04120800, 0x04020000, 0x04120000, 0x04020800, 0x04120800, 0x00020004, 0x00120004, 0x00020804, 0x00120804, 0x00020004, 0x00120004, 0x00020804, 0x00120804, 0x04020004, 0x04120004, 0x04020804, 0x04120804, 0x04020004, 0x04120004, 0x04020804, 0x04120804, 0x00020000, 0x00120000, 0x00020800, 0x00120800, 0x00020000, 0x00120000, 0x00020800, 0x00120800, 0x04020000, 0x04120000, 0x04020800, 0x04120800, 0x04020000, 0x04120000, 0x04020800, 0x04120800, 0x00020004, 0x00120004, 0x00020804, 0x00120804, 0x00020004, 0x00120004, 0x00020804, 0x00120804, 0x04020004, 0x04120004, 0x04020804, 0x04120804, 0x04020004, 0x04120004, 0x04020804, 0x04120804, 0x00020200, 0x00120200, 0x00020A00, 0x00120A00, 0x00020200, 0x00120200, 0x00020A00, 0x00120A00, 0x04020200, 0x04120200, 0x04020A00, 0x04120A00, 0x04020200, 0x04120200, 0x04020A00, 0x04120A00, 0x00020204, 0x00120204, 0x00020A04, 0x00120A04, 0x00020204, 0x00120204, 0x00020A04, 0x00120A04, 0x04020204, 0x04120204, 0x04020A04, 0x04120A04, 0x04020204, 0x04120204, 0x04020A04, 0x04120A04, 0x00020200, 0x00120200, 0x00020A00, 0x00120A00, 0x00020200, 0x00120200, 0x00020A00, 0x00120A00, 0x04020200, 0x04120200, 0x04020A00, 0x04120A00, 0x04020200, 0x04120200, 0x04020A00, 0x04120A00, 0x00020204, 0x00120204, 0x00020A04, 0x00120A04, 0x00020204, 0x00120204, 0x00020A04, 0x00120A04, 0x04020204, 0x04120204, 0x04020A04, 0x04120A04, 0x04020204, 0x04120204, 0x04020A04, 0x04120A04 ); static $pc2mapd3 = array( 0x00000000, 0x00010000, 0x02000000, 0x02010000, 0x00000020, 0x00010020, 0x02000020, 0x02010020, 0x00040000, 0x00050000, 0x02040000, 0x02050000, 0x00040020, 0x00050020, 0x02040020, 0x02050020, 0x00002000, 0x00012000, 0x02002000, 0x02012000, 0x00002020, 0x00012020, 0x02002020, 0x02012020, 0x00042000, 0x00052000, 0x02042000, 0x02052000, 0x00042020, 0x00052020, 0x02042020, 0x02052020, 0x00000000, 0x00010000, 0x02000000, 0x02010000, 0x00000020, 0x00010020, 0x02000020, 0x02010020, 0x00040000, 0x00050000, 0x02040000, 0x02050000, 0x00040020, 0x00050020, 0x02040020, 0x02050020, 0x00002000, 0x00012000, 0x02002000, 0x02012000, 0x00002020, 0x00012020, 0x02002020, 0x02012020, 0x00042000, 0x00052000, 0x02042000, 0x02052000, 0x00042020, 0x00052020, 0x02042020, 0x02052020, 0x00000010, 0x00010010, 0x02000010, 0x02010010, 0x00000030, 0x00010030, 0x02000030, 0x02010030, 0x00040010, 0x00050010, 0x02040010, 0x02050010, 0x00040030, 0x00050030, 0x02040030, 0x02050030, 0x00002010, 0x00012010, 0x02002010, 0x02012010, 0x00002030, 0x00012030, 0x02002030, 0x02012030, 0x00042010, 0x00052010, 0x02042010, 0x02052010, 0x00042030, 0x00052030, 0x02042030, 0x02052030, 0x00000010, 0x00010010, 0x02000010, 0x02010010, 0x00000030, 0x00010030, 0x02000030, 0x02010030, 0x00040010, 0x00050010, 0x02040010, 0x02050010, 0x00040030, 0x00050030, 0x02040030, 0x02050030, 0x00002010, 0x00012010, 0x02002010, 0x02012010, 0x00002030, 0x00012030, 0x02002030, 0x02012030, 0x00042010, 0x00052010, 0x02042010, 0x02052010, 0x00042030, 0x00052030, 0x02042030, 0x02052030, 0x20000000, 0x20010000, 0x22000000, 0x22010000, 0x20000020, 0x20010020, 0x22000020, 0x22010020, 0x20040000, 0x20050000, 0x22040000, 0x22050000, 0x20040020, 0x20050020, 0x22040020, 0x22050020, 0x20002000, 0x20012000, 0x22002000, 0x22012000, 0x20002020, 0x20012020, 0x22002020, 0x22012020, 0x20042000, 0x20052000, 0x22042000, 0x22052000, 0x20042020, 0x20052020, 0x22042020, 0x22052020, 0x20000000, 0x20010000, 0x22000000, 0x22010000, 0x20000020, 0x20010020, 0x22000020, 0x22010020, 0x20040000, 0x20050000, 0x22040000, 0x22050000, 0x20040020, 0x20050020, 0x22040020, 0x22050020, 0x20002000, 0x20012000, 0x22002000, 0x22012000, 0x20002020, 0x20012020, 0x22002020, 0x22012020, 0x20042000, 0x20052000, 0x22042000, 0x22052000, 0x20042020, 0x20052020, 0x22042020, 0x22052020, 0x20000010, 0x20010010, 0x22000010, 0x22010010, 0x20000030, 0x20010030, 0x22000030, 0x22010030, 0x20040010, 0x20050010, 0x22040010, 0x22050010, 0x20040030, 0x20050030, 0x22040030, 0x22050030, 0x20002010, 0x20012010, 0x22002010, 0x22012010, 0x20002030, 0x20012030, 0x22002030, 0x22012030, 0x20042010, 0x20052010, 0x22042010, 0x22052010, 0x20042030, 0x20052030, 0x22042030, 0x22052030, 0x20000010, 0x20010010, 0x22000010, 0x22010010, 0x20000030, 0x20010030, 0x22000030, 0x22010030, 0x20040010, 0x20050010, 0x22040010, 0x22050010, 0x20040030, 0x20050030, 0x22040030, 0x22050030, 0x20002010, 0x20012010, 0x22002010, 0x22012010, 0x20002030, 0x20012030, 0x22002030, 0x22012030, 0x20042010, 0x20052010, 0x22042010, 0x22052010, 0x20042030, 0x20052030, 0x22042030, 0x22052030 ); static $pc2mapd4 = array( 0x00000000, 0x00000400, 0x01000000, 0x01000400, 0x00000000, 0x00000400, 0x01000000, 0x01000400, 0x00000100, 0x00000500, 0x01000100, 0x01000500, 0x00000100, 0x00000500, 0x01000100, 0x01000500, 0x10000000, 0x10000400, 0x11000000, 0x11000400, 0x10000000, 0x10000400, 0x11000000, 0x11000400, 0x10000100, 0x10000500, 0x11000100, 0x11000500, 0x10000100, 0x10000500, 0x11000100, 0x11000500, 0x00080000, 0x00080400, 0x01080000, 0x01080400, 0x00080000, 0x00080400, 0x01080000, 0x01080400, 0x00080100, 0x00080500, 0x01080100, 0x01080500, 0x00080100, 0x00080500, 0x01080100, 0x01080500, 0x10080000, 0x10080400, 0x11080000, 0x11080400, 0x10080000, 0x10080400, 0x11080000, 0x11080400, 0x10080100, 0x10080500, 0x11080100, 0x11080500, 0x10080100, 0x10080500, 0x11080100, 0x11080500, 0x00000008, 0x00000408, 0x01000008, 0x01000408, 0x00000008, 0x00000408, 0x01000008, 0x01000408, 0x00000108, 0x00000508, 0x01000108, 0x01000508, 0x00000108, 0x00000508, 0x01000108, 0x01000508, 0x10000008, 0x10000408, 0x11000008, 0x11000408, 0x10000008, 0x10000408, 0x11000008, 0x11000408, 0x10000108, 0x10000508, 0x11000108, 0x11000508, 0x10000108, 0x10000508, 0x11000108, 0x11000508, 0x00080008, 0x00080408, 0x01080008, 0x01080408, 0x00080008, 0x00080408, 0x01080008, 0x01080408, 0x00080108, 0x00080508, 0x01080108, 0x01080508, 0x00080108, 0x00080508, 0x01080108, 0x01080508, 0x10080008, 0x10080408, 0x11080008, 0x11080408, 0x10080008, 0x10080408, 0x11080008, 0x11080408, 0x10080108, 0x10080508, 0x11080108, 0x11080508, 0x10080108, 0x10080508, 0x11080108, 0x11080508, 0x00001000, 0x00001400, 0x01001000, 0x01001400, 0x00001000, 0x00001400, 0x01001000, 0x01001400, 0x00001100, 0x00001500, 0x01001100, 0x01001500, 0x00001100, 0x00001500, 0x01001100, 0x01001500, 0x10001000, 0x10001400, 0x11001000, 0x11001400, 0x10001000, 0x10001400, 0x11001000, 0x11001400, 0x10001100, 0x10001500, 0x11001100, 0x11001500, 0x10001100, 0x10001500, 0x11001100, 0x11001500, 0x00081000, 0x00081400, 0x01081000, 0x01081400, 0x00081000, 0x00081400, 0x01081000, 0x01081400, 0x00081100, 0x00081500, 0x01081100, 0x01081500, 0x00081100, 0x00081500, 0x01081100, 0x01081500, 0x10081000, 0x10081400, 0x11081000, 0x11081400, 0x10081000, 0x10081400, 0x11081000, 0x11081400, 0x10081100, 0x10081500, 0x11081100, 0x11081500, 0x10081100, 0x10081500, 0x11081100, 0x11081500, 0x00001008, 0x00001408, 0x01001008, 0x01001408, 0x00001008, 0x00001408, 0x01001008, 0x01001408, 0x00001108, 0x00001508, 0x01001108, 0x01001508, 0x00001108, 0x00001508, 0x01001108, 0x01001508, 0x10001008, 0x10001408, 0x11001008, 0x11001408, 0x10001008, 0x10001408, 0x11001008, 0x11001408, 0x10001108, 0x10001508, 0x11001108, 0x11001508, 0x10001108, 0x10001508, 0x11001108, 0x11001508, 0x00081008, 0x00081408, 0x01081008, 0x01081408, 0x00081008, 0x00081408, 0x01081008, 0x01081408, 0x00081108, 0x00081508, 0x01081108, 0x01081508, 0x00081108, 0x00081508, 0x01081108, 0x01081508, 0x10081008, 0x10081408, 0x11081008, 0x11081408, 0x10081008, 0x10081408, 0x11081008, 0x11081408, 0x10081108, 0x10081508, 0x11081108, 0x11081508, 0x10081108, 0x10081508, 0x11081108, 0x11081508 ); $keys = array(); for ($des_round = 0; $des_round < $this->des_rounds; ++$des_round) { // pad the key and remove extra characters as appropriate. $key = str_pad(substr($this->key, $des_round * 8, 8), 8, "\0"); // Perform the PC/1 transformation and compute C and D. $t = unpack('Nl/Nr', $key); list($l, $r) = array($t['l'], $t['r']); $key = ($this->shuffle[$pc1map[ $r & 0xFF]] & "\x80\x80\x80\x80\x80\x80\x80\x00") | ($this->shuffle[$pc1map[($r >> 8) & 0xFF]] & "\x40\x40\x40\x40\x40\x40\x40\x00") | ($this->shuffle[$pc1map[($r >> 16) & 0xFF]] & "\x20\x20\x20\x20\x20\x20\x20\x00") | ($this->shuffle[$pc1map[($r >> 24) & 0xFF]] & "\x10\x10\x10\x10\x10\x10\x10\x00") | ($this->shuffle[$pc1map[ $l & 0xFF]] & "\x08\x08\x08\x08\x08\x08\x08\x00") | ($this->shuffle[$pc1map[($l >> 8) & 0xFF]] & "\x04\x04\x04\x04\x04\x04\x04\x00") | ($this->shuffle[$pc1map[($l >> 16) & 0xFF]] & "\x02\x02\x02\x02\x02\x02\x02\x00") | ($this->shuffle[$pc1map[($l >> 24) & 0xFF]] & "\x01\x01\x01\x01\x01\x01\x01\x00"); $key = unpack('Nc/Nd', $key); $c = ( $key['c'] >> 4) & 0x0FFFFFFF; $d = (($key['d'] >> 4) & 0x0FFFFFF0) | ($key['c'] & 0x0F); $keys[$des_round] = array( self::ENCRYPT => array(), self::DECRYPT => array_fill(0, 32, 0) ); for ($i = 0, $ki = 31; $i < 16; ++$i, $ki-= 2) { $c <<= $shifts[$i]; $c = ($c | ($c >> 28)) & 0x0FFFFFFF; $d <<= $shifts[$i]; $d = ($d | ($d >> 28)) & 0x0FFFFFFF; // Perform the PC-2 transformation. $cp = $pc2mapc1[ $c >> 24 ] | $pc2mapc2[($c >> 16) & 0xFF] | $pc2mapc3[($c >> 8) & 0xFF] | $pc2mapc4[ $c & 0xFF]; $dp = $pc2mapd1[ $d >> 24 ] | $pc2mapd2[($d >> 16) & 0xFF] | $pc2mapd3[($d >> 8) & 0xFF] | $pc2mapd4[ $d & 0xFF]; // Reorder: odd bytes/even bytes. Push the result in key schedule. $val1 = ( $cp & 0xFF000000) | (($cp << 8) & 0x00FF0000) | (($dp >> 16) & 0x0000FF00) | (($dp >> 8) & 0x000000FF); $val2 = (($cp << 8) & 0xFF000000) | (($cp << 16) & 0x00FF0000) | (($dp >> 8) & 0x0000FF00) | ( $dp & 0x000000FF); $keys[$des_round][self::ENCRYPT][ ] = $val1; $keys[$des_round][self::DECRYPT][$ki - 1] = $val1; $keys[$des_round][self::ENCRYPT][ ] = $val2; $keys[$des_round][self::DECRYPT][$ki ] = $val2; } } switch ($this->des_rounds) { case 3: // 3DES keys $this->keys = array( self::ENCRYPT => array_merge( $keys[0][self::ENCRYPT], $keys[1][self::DECRYPT], $keys[2][self::ENCRYPT] ), self::DECRYPT => array_merge( $keys[2][self::DECRYPT], $keys[1][self::ENCRYPT], $keys[0][self::DECRYPT] ) ); break; // case 1: // DES keys default: $this->keys = array( self::ENCRYPT => $keys[0][self::ENCRYPT], self::DECRYPT => $keys[0][self::DECRYPT] ); } } /** * Setup the performance-optimized function for de/encrypt() * * @see \phpseclib\Crypt\Base::_setupInlineCrypt() * @access private */ function _setupInlineCrypt() { $lambda_functions =& self::_getLambdaFunctions(); // Engine configuration for: // - DES ($des_rounds == 1) or // - 3DES ($des_rounds == 3) $des_rounds = $this->des_rounds; // We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function. // (Currently, for DES, one generated $lambda_function cost on php5.5@32bit ~135kb unfreeable mem and ~230kb on php5.5@64bit) // (Currently, for TripleDES, one generated $lambda_function cost on php5.5@32bit ~240kb unfreeable mem and ~340kb on php5.5@64bit) // After that, we'll still create very fast optimized code but not the hi-ultimative code, for each $mode one $gen_hi_opt_code = (bool)( count($lambda_functions) < 10 ); // Generation of a unique hash for our generated code $code_hash = "Crypt_DES, $des_rounds, {$this->mode}"; if ($gen_hi_opt_code) { // For hi-optimized code, we create for each combination of // $mode, $des_rounds and $this->key its own encrypt/decrypt function. // After max 10 hi-optimized functions, we create generic // (still very fast.. but not ultra) functions for each $mode/$des_rounds // Currently 2 * 5 generic functions will be then max. possible. $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key); } // Is there a re-usable $lambda_functions in there? If not, we have to create it. if (!isset($lambda_functions[$code_hash])) { // Init code for both, encrypt and decrypt. $init_crypt = 'static $sbox1, $sbox2, $sbox3, $sbox4, $sbox5, $sbox6, $sbox7, $sbox8, $shuffleip, $shuffleinvip; if (!$sbox1) { $sbox1 = array_map("intval", $self->sbox1); $sbox2 = array_map("intval", $self->sbox2); $sbox3 = array_map("intval", $self->sbox3); $sbox4 = array_map("intval", $self->sbox4); $sbox5 = array_map("intval", $self->sbox5); $sbox6 = array_map("intval", $self->sbox6); $sbox7 = array_map("intval", $self->sbox7); $sbox8 = array_map("intval", $self->sbox8);' /* Merge $shuffle with $[inv]ipmap */ . ' for ($i = 0; $i < 256; ++$i) { $shuffleip[] = $self->shuffle[$self->ipmap[$i]]; $shuffleinvip[] = $self->shuffle[$self->invipmap[$i]]; } } '; switch (true) { case $gen_hi_opt_code: // In Hi-optimized code mode, we use our [3]DES key schedule as hardcoded integers. // No futher initialisation of the $keys schedule is necessary. // That is the extra performance boost. $k = array( self::ENCRYPT => $this->keys[self::ENCRYPT], self::DECRYPT => $this->keys[self::DECRYPT] ); $init_encrypt = ''; $init_decrypt = ''; break; default: // In generic optimized code mode, we have to use, as the best compromise [currently], // our key schedule as $ke/$kd arrays. (with hardcoded indexes...) $k = array( self::ENCRYPT => array(), self::DECRYPT => array() ); for ($i = 0, $c = count($this->keys[self::ENCRYPT]); $i < $c; ++$i) { $k[self::ENCRYPT][$i] = '$ke[' . $i . ']'; $k[self::DECRYPT][$i] = '$kd[' . $i . ']'; } $init_encrypt = '$ke = $self->keys[$self::ENCRYPT];'; $init_decrypt = '$kd = $self->keys[$self::DECRYPT];'; break; } // Creating code for en- and decryption. $crypt_block = array(); foreach (array(self::ENCRYPT, self::DECRYPT) as $c) { /* Do the initial IP permutation. */ $crypt_block[$c] = ' $in = unpack("N*", $in); $l = $in[1]; $r = $in[2]; $in = unpack("N*", ($shuffleip[ $r & 0xFF] & "\x80\x80\x80\x80\x80\x80\x80\x80") | ($shuffleip[($r >> 8) & 0xFF] & "\x40\x40\x40\x40\x40\x40\x40\x40") | ($shuffleip[($r >> 16) & 0xFF] & "\x20\x20\x20\x20\x20\x20\x20\x20") | ($shuffleip[($r >> 24) & 0xFF] & "\x10\x10\x10\x10\x10\x10\x10\x10") | ($shuffleip[ $l & 0xFF] & "\x08\x08\x08\x08\x08\x08\x08\x08") | ($shuffleip[($l >> 8) & 0xFF] & "\x04\x04\x04\x04\x04\x04\x04\x04") | ($shuffleip[($l >> 16) & 0xFF] & "\x02\x02\x02\x02\x02\x02\x02\x02") | ($shuffleip[($l >> 24) & 0xFF] & "\x01\x01\x01\x01\x01\x01\x01\x01") ); ' . /* Extract L0 and R0 */ ' $l = $in[1]; $r = $in[2]; '; $l = '$l'; $r = '$r'; // Perform DES or 3DES. for ($ki = -1, $des_round = 0; $des_round < $des_rounds; ++$des_round) { // Perform the 16 steps. for ($i = 0; $i < 16; ++$i) { // start of "the Feistel (F) function" - see the following URL: // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png // Merge key schedule. $crypt_block[$c].= ' $b1 = ((' . $r . ' >> 3) & 0x1FFFFFFF) ^ (' . $r . ' << 29) ^ ' . $k[$c][++$ki] . '; $b2 = ((' . $r . ' >> 31) & 0x00000001) ^ (' . $r . ' << 1) ^ ' . $k[$c][++$ki] . ';' . /* S-box indexing. */ $l . ' = $sbox1[($b1 >> 24) & 0x3F] ^ $sbox2[($b2 >> 24) & 0x3F] ^ $sbox3[($b1 >> 16) & 0x3F] ^ $sbox4[($b2 >> 16) & 0x3F] ^ $sbox5[($b1 >> 8) & 0x3F] ^ $sbox6[($b2 >> 8) & 0x3F] ^ $sbox7[ $b1 & 0x3F] ^ $sbox8[ $b2 & 0x3F] ^ ' . $l . '; '; // end of "the Feistel (F) function" // swap L & R list($l, $r) = array($r, $l); } list($l, $r) = array($r, $l); } // Perform the inverse IP permutation. $crypt_block[$c].= '$in = ($shuffleinvip[($l >> 24) & 0xFF] & "\x80\x80\x80\x80\x80\x80\x80\x80") | ($shuffleinvip[($r >> 24) & 0xFF] & "\x40\x40\x40\x40\x40\x40\x40\x40") | ($shuffleinvip[($l >> 16) & 0xFF] & "\x20\x20\x20\x20\x20\x20\x20\x20") | ($shuffleinvip[($r >> 16) & 0xFF] & "\x10\x10\x10\x10\x10\x10\x10\x10") | ($shuffleinvip[($l >> 8) & 0xFF] & "\x08\x08\x08\x08\x08\x08\x08\x08") | ($shuffleinvip[($r >> 8) & 0xFF] & "\x04\x04\x04\x04\x04\x04\x04\x04") | ($shuffleinvip[ $l & 0xFF] & "\x02\x02\x02\x02\x02\x02\x02\x02") | ($shuffleinvip[ $r & 0xFF] & "\x01\x01\x01\x01\x01\x01\x01\x01"); '; } // Creates the inline-crypt function $lambda_functions[$code_hash] = $this->_createInlineCryptFunction( array( 'init_crypt' => $init_crypt, 'init_encrypt' => $init_encrypt, 'init_decrypt' => $init_decrypt, 'encrypt_block' => $crypt_block[self::ENCRYPT], 'decrypt_block' => $crypt_block[self::DECRYPT] ) ); } // Set the inline-crypt function as callback in: $this->inline_crypt $this->inline_crypt = $lambda_functions[$code_hash]; } } <?php /** * Pure-PHP implementation of Blowfish. * * Uses mcrypt, if available, and an internal implementation, otherwise. * * PHP version 5 * * Useful resources are as follows: * * - {@link http://en.wikipedia.org/wiki/Blowfish_(cipher) Wikipedia description of Blowfish} * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $blowfish = new \phpseclib\Crypt\Blowfish(); * * $blowfish->setKey('12345678901234567890123456789012'); * * $plaintext = str_repeat('a', 1024); * * echo $blowfish->decrypt($blowfish->encrypt($plaintext)); * ?> * </code> * * @category Crypt * @package Blowfish * @author Jim Wigginton <terrafrost@php.net> * @author Hans-Juergen Petrich <petrich@tronic-media.com> * @copyright 2007 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; /** * Pure-PHP implementation of Blowfish. * * @package Blowfish * @author Jim Wigginton <terrafrost@php.net> * @author Hans-Juergen Petrich <petrich@tronic-media.com> * @access public */ class Blowfish extends Base { /** * Block Length of the cipher * * @see \phpseclib\Crypt\Base::block_size * @var int * @access private */ var $block_size = 8; /** * The mcrypt specific name of the cipher * * @see \phpseclib\Crypt\Base::cipher_name_mcrypt * @var string * @access private */ var $cipher_name_mcrypt = 'blowfish'; /** * Optimizing value while CFB-encrypting * * @see \phpseclib\Crypt\Base::cfb_init_len * @var int * @access private */ var $cfb_init_len = 500; /** * The fixed subkeys boxes ($sbox0 - $sbox3) with 256 entries each * * S-Box 0 * * @access private * @var array */ var $sbox0 = array( 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a ); /** * S-Box 1 * * @access private * @var array */ var $sbox1 = array( 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7 ); /** * S-Box 2 * * @access private * @var array */ var $sbox2 = array( 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0 ); /** * S-Box 3 * * @access private * @var array */ var $sbox3 = array( 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6 ); /** * P-Array consists of 18 32-bit subkeys * * @var array * @access private */ var $parray = array( 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b ); /** * The BCTX-working Array * * Holds the expanded key [p] and the key-depended s-boxes [sb] * * @var array * @access private */ var $bctx; /** * Holds the last used key * * @var array * @access private */ var $kl; /** * The Key Length (in bytes) * * @see \phpseclib\Crypt\Base::setKeyLength() * @var int * @access private * @internal The max value is 256 / 8 = 32, the min value is 128 / 8 = 16. Exists in conjunction with $Nk * because the encryption / decryption / key schedule creation requires this number and not $key_length. We could * derive this from $key_length or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu * of that, we'll just precompute it once. */ var $key_length = 16; /** * Sets the key length. * * Key lengths can be between 32 and 448 bits. * * @access public * @param int $length */ function setKeyLength($length) { if ($length < 32) { $this->key_length = 4; } elseif ($length > 448) { $this->key_length = 56; } else { $this->key_length = $length >> 3; } parent::setKeyLength($length); } /** * Test for engine validity * * This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine() * * @see \phpseclib\Crypt\Base::isValidEngine() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { if ($engine == self::ENGINE_OPENSSL) { if (version_compare(PHP_VERSION, '5.3.7') < 0 && $this->key_length != 16) { return false; } if ($this->key_length < 16) { return false; } $this->cipher_name_openssl_ecb = 'bf-ecb'; $this->cipher_name_openssl = 'bf-' . $this->_openssl_translate_mode(); } return parent::isValidEngine($engine); } /** * Setup the key (expansion) * * @see \phpseclib\Crypt\Base::_setupKey() * @access private */ function _setupKey() { if (isset($this->kl['key']) && $this->key === $this->kl['key']) { // already expanded return; } $this->kl = array('key' => $this->key); /* key-expanding p[] and S-Box building sb[] */ $this->bctx = array( 'p' => array(), 'sb' => array( $this->sbox0, $this->sbox1, $this->sbox2, $this->sbox3 ) ); // unpack binary string in unsigned chars $key = array_values(unpack('C*', $this->key)); $keyl = count($key); for ($j = 0, $i = 0; $i < 18; ++$i) { // xor P1 with the first 32-bits of the key, xor P2 with the second 32-bits ... for ($data = 0, $k = 0; $k < 4; ++$k) { $data = ($data << 8) | $key[$j]; if (++$j >= $keyl) { $j = 0; } } $this->bctx['p'][] = $this->parray[$i] ^ $data; } // encrypt the zero-string, replace P1 and P2 with the encrypted data, // encrypt P3 and P4 with the new P1 and P2, do it with all P-array and subkeys $data = "\0\0\0\0\0\0\0\0"; for ($i = 0; $i < 18; $i += 2) { list($l, $r) = array_values(unpack('N*', $data = $this->_encryptBlock($data))); $this->bctx['p'][$i ] = $l; $this->bctx['p'][$i + 1] = $r; } for ($i = 0; $i < 4; ++$i) { for ($j = 0; $j < 256; $j += 2) { list($l, $r) = array_values(unpack('N*', $data = $this->_encryptBlock($data))); $this->bctx['sb'][$i][$j ] = $l; $this->bctx['sb'][$i][$j + 1] = $r; } } } /** * Encrypts a block * * @access private * @param string $in * @return string */ function _encryptBlock($in) { $p = $this->bctx["p"]; // extract($this->bctx["sb"], EXTR_PREFIX_ALL, "sb"); // slower $sb_0 = $this->bctx["sb"][0]; $sb_1 = $this->bctx["sb"][1]; $sb_2 = $this->bctx["sb"][2]; $sb_3 = $this->bctx["sb"][3]; $in = unpack("N*", $in); $l = $in[1]; $r = $in[2]; for ($i = 0; $i < 16; $i+= 2) { $l^= $p[$i]; $r^= $this->safe_intval(($this->safe_intval($sb_0[$l >> 24 & 0xff] + $sb_1[$l >> 16 & 0xff]) ^ $sb_2[$l >> 8 & 0xff]) + $sb_3[$l & 0xff]); $r^= $p[$i + 1]; $l^= $this->safe_intval(($this->safe_intval($sb_0[$r >> 24 & 0xff] + $sb_1[$r >> 16 & 0xff]) ^ $sb_2[$r >> 8 & 0xff]) + $sb_3[$r & 0xff]); } return pack("N*", $r ^ $p[17], $l ^ $p[16]); } /** * Decrypts a block * * @access private * @param string $in * @return string */ function _decryptBlock($in) { $p = $this->bctx["p"]; $sb_0 = $this->bctx["sb"][0]; $sb_1 = $this->bctx["sb"][1]; $sb_2 = $this->bctx["sb"][2]; $sb_3 = $this->bctx["sb"][3]; $in = unpack("N*", $in); $l = $in[1]; $r = $in[2]; for ($i = 17; $i > 2; $i-= 2) { $l^= $p[$i]; $r^= $this->safe_intval(($this->safe_intval($sb_0[$l >> 24 & 0xff] + $sb_1[$l >> 16 & 0xff]) ^ $sb_2[$l >> 8 & 0xff]) + $sb_3[$l & 0xff]); $r^= $p[$i - 1]; $l^= $this->safe_intval(($this->safe_intval($sb_0[$r >> 24 & 0xff] + $sb_1[$r >> 16 & 0xff]) ^ $sb_2[$r >> 8 & 0xff]) + $sb_3[$r & 0xff]); } return pack("N*", $r ^ $p[0], $l ^ $p[1]); } /** * Setup the performance-optimized function for de/encrypt() * * @see \phpseclib\Crypt\Base::_setupInlineCrypt() * @access private */ function _setupInlineCrypt() { $lambda_functions =& self::_getLambdaFunctions(); // We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function. // (Currently, for Blowfish, one generated $lambda_function cost on php5.5@32bit ~100kb unfreeable mem and ~180kb on php5.5@64bit) // After that, we'll still create very fast optimized code but not the hi-ultimative code, for each $mode one. $gen_hi_opt_code = (bool)(count($lambda_functions) < 10); // Generation of a unique hash for our generated code $code_hash = "Crypt_Blowfish, {$this->mode}"; if ($gen_hi_opt_code) { $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key); } $safeint = $this->safe_intval_inline(); if (!isset($lambda_functions[$code_hash])) { switch (true) { case $gen_hi_opt_code: $p = $this->bctx['p']; $init_crypt = ' static $sb_0, $sb_1, $sb_2, $sb_3; if (!$sb_0) { $sb_0 = $self->bctx["sb"][0]; $sb_1 = $self->bctx["sb"][1]; $sb_2 = $self->bctx["sb"][2]; $sb_3 = $self->bctx["sb"][3]; } '; break; default: $p = array(); for ($i = 0; $i < 18; ++$i) { $p[] = '$p_' . $i; } $init_crypt = ' list($sb_0, $sb_1, $sb_2, $sb_3) = $self->bctx["sb"]; list(' . implode(',', $p) . ') = $self->bctx["p"]; '; } // Generating encrypt code: $encrypt_block = ' $in = unpack("N*", $in); $l = $in[1]; $r = $in[2]; '; for ($i = 0; $i < 16; $i+= 2) { $encrypt_block.= ' $l^= ' . $p[$i] . '; $r^= ' . sprintf($safeint, '(' . sprintf($safeint, '$sb_0[$l >> 24 & 0xff] + $sb_1[$l >> 16 & 0xff]') . ' ^ $sb_2[$l >> 8 & 0xff]) + $sb_3[$l & 0xff]') . '; $r^= ' . $p[$i + 1] . '; $l^= ' . sprintf($safeint, '(' . sprintf($safeint, '$sb_0[$r >> 24 & 0xff] + $sb_1[$r >> 16 & 0xff]') . ' ^ $sb_2[$r >> 8 & 0xff]) + $sb_3[$r & 0xff]') . '; '; } $encrypt_block.= ' $in = pack("N*", $r ^ ' . $p[17] . ', $l ^ ' . $p[16] . ' ); '; // Generating decrypt code: $decrypt_block = ' $in = unpack("N*", $in); $l = $in[1]; $r = $in[2]; '; for ($i = 17; $i > 2; $i-= 2) { $decrypt_block.= ' $l^= ' . $p[$i] . '; $r^= ' . sprintf($safeint, '(' . sprintf($safeint, '$sb_0[$l >> 24 & 0xff] + $sb_1[$l >> 16 & 0xff]') . ' ^ $sb_2[$l >> 8 & 0xff]) + $sb_3[$l & 0xff]') . '; $r^= ' . $p[$i - 1] . '; $l^= ' . sprintf($safeint, '(' . sprintf($safeint, '$sb_0[$r >> 24 & 0xff] + $sb_1[$r >> 16 & 0xff]') . ' ^ $sb_2[$r >> 8 & 0xff]) + $sb_3[$r & 0xff]') . '; '; } $decrypt_block.= ' $in = pack("N*", $r ^ ' . $p[0] . ', $l ^ ' . $p[1] . ' ); '; $lambda_functions[$code_hash] = $this->_createInlineCryptFunction( array( 'init_crypt' => $init_crypt, 'init_encrypt' => '', 'init_decrypt' => '', 'encrypt_block' => $encrypt_block, 'decrypt_block' => $decrypt_block ) ); } $this->inline_crypt = $lambda_functions[$code_hash]; } } <?php /** * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions. * * Uses hash() or mhash() if available and an internal implementation, otherwise. Currently supports the following: * * md2, md5, md5-96, sha1, sha1-96, sha256, sha256-96, sha384, and sha512, sha512-96 * * If {@link self::setKey() setKey()} is called, {@link self::hash() hash()} will return the HMAC as opposed to * the hash. If no valid algorithm is provided, sha1 will be used. * * PHP version 5 * * {@internal The variable names are the same as those in * {@link http://tools.ietf.org/html/rfc2104#section-2 RFC2104}.}} * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $hash = new \phpseclib\Crypt\Hash('sha1'); * * $hash->setKey('abcdefg'); * * echo base64_encode($hash->hash('abcdefg')); * ?> * </code> * * @category Crypt * @package Hash * @author Jim Wigginton <terrafrost@php.net> * @copyright 2007 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; use phpseclib\Math\BigInteger; /** * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions. * * @package Hash * @author Jim Wigginton <terrafrost@php.net> * @access public */ class Hash { /**#@+ * @access private * @see \phpseclib\Crypt\Hash::__construct() */ /** * Toggles the internal implementation */ const MODE_INTERNAL = 1; /** * Toggles the mhash() implementation, which has been deprecated on PHP 5.3.0+. */ const MODE_MHASH = 2; /** * Toggles the hash() implementation, which works on PHP 5.1.2+. */ const MODE_HASH = 3; /**#@-*/ /** * Hash Parameter * * @see self::setHash() * @var int * @access private */ var $hashParam; /** * Byte-length of compression blocks / key (Internal HMAC) * * @see self::setAlgorithm() * @var int * @access private */ var $b; /** * Byte-length of hash output (Internal HMAC) * * @see self::setHash() * @var int * @access private */ var $l = false; /** * Hash Algorithm * * @see self::setHash() * @var string * @access private */ var $hash; /** * Key * * @see self::setKey() * @var string * @access private */ var $key = false; /** * Computed Key * * @see self::_computeKey() * @var string * @access private */ var $computedKey = false; /** * Outer XOR (Internal HMAC) * * @see self::setKey() * @var string * @access private */ var $opad; /** * Inner XOR (Internal HMAC) * * @see self::setKey() * @var string * @access private */ var $ipad; /** * Engine * * @see self::setHash() * @var string * @access private */ var $engine; /** * Default Constructor. * * @param string $hash * @return \phpseclib\Crypt\Hash * @access public */ function __construct($hash = 'sha1') { if (!defined('CRYPT_HASH_MODE')) { switch (true) { case extension_loaded('hash'): define('CRYPT_HASH_MODE', self::MODE_HASH); break; case extension_loaded('mhash'): define('CRYPT_HASH_MODE', self::MODE_MHASH); break; default: define('CRYPT_HASH_MODE', self::MODE_INTERNAL); } } $this->setHash($hash); } /** * Sets the key for HMACs * * Keys can be of any length. * * @access public * @param string $key */ function setKey($key = false) { $this->key = $key; $this->_computeKey(); } /** * Pre-compute the key used by the HMAC * * Quoting http://tools.ietf.org/html/rfc2104#section-2, "Applications that use keys longer than B bytes * will first hash the key using H and then use the resultant L byte string as the actual key to HMAC." * * As documented in https://www.reddit.com/r/PHP/comments/9nct2l/symfonypolyfill_hash_pbkdf2_correct_fix_for/ * when doing an HMAC multiple times it's faster to compute the hash once instead of computing it during * every call * * @access private */ function _computeKey() { if ($this->key === false) { $this->computedKey = false; return; } if (strlen($this->key) <= $this->b) { $this->computedKey = $this->key; return; } switch ($this->engine) { case self::MODE_MHASH: $this->computedKey = mhash($this->hash, $this->key); break; case self::MODE_HASH: $this->computedKey = hash($this->hash, $this->key, true); break; case self::MODE_INTERNAL: $this->computedKey = call_user_func($this->hash, $this->key); } } /** * Gets the hash function. * * As set by the constructor or by the setHash() method. * * @access public * @return string */ function getHash() { return $this->hashParam; } /** * Sets the hash function. * * @access public * @param string $hash */ function setHash($hash) { $this->hashParam = $hash = strtolower($hash); switch ($hash) { case 'md5-96': case 'sha1-96': case 'sha256-96': case 'sha512-96': $hash = substr($hash, 0, -3); $this->l = 12; // 96 / 8 = 12 break; case 'md2': case 'md5': $this->l = 16; break; case 'sha1': $this->l = 20; break; case 'sha256': $this->l = 32; break; case 'sha384': $this->l = 48; break; case 'sha512': $this->l = 64; } switch ($hash) { case 'md2-96': case 'md2': $this->b = 16; case 'md5-96': case 'sha1-96': case 'sha224-96': case 'sha256-96': case 'md2': case 'md5': case 'sha1': case 'sha224': case 'sha256': $this->b = 64; break; default: $this->b = 128; } switch ($hash) { case 'md2': $this->engine = CRYPT_HASH_MODE == self::MODE_HASH && in_array('md2', hash_algos()) ? self::MODE_HASH : self::MODE_INTERNAL; break; case 'sha384': case 'sha512': $this->engine = CRYPT_HASH_MODE == self::MODE_MHASH ? self::MODE_INTERNAL : CRYPT_HASH_MODE; break; default: $this->engine = CRYPT_HASH_MODE; } switch ($this->engine) { case self::MODE_MHASH: switch ($hash) { case 'md5': $this->hash = MHASH_MD5; break; case 'sha256': $this->hash = MHASH_SHA256; break; case 'sha1': default: $this->hash = MHASH_SHA1; } $this->_computeKey(self::MODE_MHASH); return; case self::MODE_HASH: switch ($hash) { case 'md5': $this->hash = 'md5'; return; case 'md2': case 'sha256': case 'sha384': case 'sha512': $this->hash = $hash; return; case 'sha1': default: $this->hash = 'sha1'; } $this->_computeKey(self::MODE_HASH); return; } switch ($hash) { case 'md2': $this->hash = array($this, '_md2'); break; case 'md5': $this->hash = array($this, '_md5'); break; case 'sha256': $this->hash = array($this, '_sha256'); break; case 'sha384': case 'sha512': $this->hash = array($this, '_sha512'); break; case 'sha1': default: $this->hash = array($this, '_sha1'); } $this->ipad = str_repeat(chr(0x36), $this->b); $this->opad = str_repeat(chr(0x5C), $this->b); $this->_computeKey(self::MODE_INTERNAL); } /** * Compute the HMAC. * * @access public * @param string $text * @return string */ function hash($text) { if (!empty($this->key) || is_string($this->key)) { switch ($this->engine) { case self::MODE_MHASH: $output = mhash($this->hash, $text, $this->computedKey); break; case self::MODE_HASH: $output = hash_hmac($this->hash, $text, $this->computedKey, true); break; case self::MODE_INTERNAL: $key = str_pad($this->computedKey, $this->b, chr(0)); // step 1 $temp = $this->ipad ^ $key; // step 2 $temp .= $text; // step 3 $temp = call_user_func($this->hash, $temp); // step 4 $output = $this->opad ^ $key; // step 5 $output.= $temp; // step 6 $output = call_user_func($this->hash, $output); // step 7 } } else { switch ($this->engine) { case self::MODE_MHASH: $output = mhash($this->hash, $text); break; case self::MODE_HASH: $output = hash($this->hash, $text, true); break; case self::MODE_INTERNAL: $output = call_user_func($this->hash, $text); } } return substr($output, 0, $this->l); } /** * Returns the hash length (in bytes) * * @access public * @return int */ function getLength() { return $this->l; } /** * Wrapper for MD5 * * @access private * @param string $m */ function _md5($m) { return pack('H*', md5($m)); } /** * Wrapper for SHA1 * * @access private * @param string $m */ function _sha1($m) { return pack('H*', sha1($m)); } /** * Pure-PHP implementation of MD2 * * See {@link http://tools.ietf.org/html/rfc1319 RFC1319}. * * @access private * @param string $m */ function _md2($m) { static $s = array( 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27, 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 ); // Step 1. Append Padding Bytes $pad = 16 - (strlen($m) & 0xF); $m.= str_repeat(chr($pad), $pad); $length = strlen($m); // Step 2. Append Checksum $c = str_repeat(chr(0), 16); $l = chr(0); for ($i = 0; $i < $length; $i+= 16) { for ($j = 0; $j < 16; $j++) { // RFC1319 incorrectly states that C[j] should be set to S[c xor L] //$c[$j] = chr($s[ord($m[$i + $j] ^ $l)]); // per <http://www.rfc-editor.org/errata_search.php?rfc=1319>, however, C[j] should be set to S[c xor L] xor C[j] $c[$j] = chr($s[ord($m[$i + $j] ^ $l)] ^ ord($c[$j])); $l = $c[$j]; } } $m.= $c; $length+= 16; // Step 3. Initialize MD Buffer $x = str_repeat(chr(0), 48); // Step 4. Process Message in 16-Byte Blocks for ($i = 0; $i < $length; $i+= 16) { for ($j = 0; $j < 16; $j++) { $x[$j + 16] = $m[$i + $j]; $x[$j + 32] = $x[$j + 16] ^ $x[$j]; } $t = chr(0); for ($j = 0; $j < 18; $j++) { for ($k = 0; $k < 48; $k++) { $x[$k] = $t = $x[$k] ^ chr($s[ord($t)]); //$t = $x[$k] = $x[$k] ^ chr($s[ord($t)]); } $t = chr(ord($t) + $j); } } // Step 5. Output return substr($x, 0, 16); } /** * Pure-PHP implementation of SHA256 * * See {@link http://en.wikipedia.org/wiki/SHA_hash_functions#SHA-256_.28a_SHA-2_variant.29_pseudocode SHA-256 (a SHA-2 variant) pseudocode - Wikipedia}. * * @access private * @param string $m */ function _sha256($m) { if (extension_loaded('suhosin')) { return pack('H*', sha256($m)); } // Initialize variables $hash = array( 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ); // Initialize table of round constants // (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311) static $k = array( 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 ); // Pre-processing $length = strlen($m); // to round to nearest 56 mod 64, we'll add 64 - (length + (64 - 56)) % 64 $m.= str_repeat(chr(0), 64 - (($length + 8) & 0x3F)); $m[$length] = chr(0x80); // we don't support hashing strings 512MB long $m.= pack('N2', 0, $length << 3); // Process the message in successive 512-bit chunks $chunks = str_split($m, 64); foreach ($chunks as $chunk) { $w = array(); for ($i = 0; $i < 16; $i++) { extract(unpack('Ntemp', $this->_string_shift($chunk, 4))); $w[] = $temp; } // Extend the sixteen 32-bit words into sixty-four 32-bit words for ($i = 16; $i < 64; $i++) { // @codingStandardsIgnoreStart $s0 = $this->_rightRotate($w[$i - 15], 7) ^ $this->_rightRotate($w[$i - 15], 18) ^ $this->_rightShift( $w[$i - 15], 3); $s1 = $this->_rightRotate($w[$i - 2], 17) ^ $this->_rightRotate($w[$i - 2], 19) ^ $this->_rightShift( $w[$i - 2], 10); // @codingStandardsIgnoreEnd $w[$i] = $this->_add($w[$i - 16], $s0, $w[$i - 7], $s1); } // Initialize hash value for this chunk list($a, $b, $c, $d, $e, $f, $g, $h) = $hash; // Main loop for ($i = 0; $i < 64; $i++) { $s0 = $this->_rightRotate($a, 2) ^ $this->_rightRotate($a, 13) ^ $this->_rightRotate($a, 22); $maj = ($a & $b) ^ ($a & $c) ^ ($b & $c); $t2 = $this->_add($s0, $maj); $s1 = $this->_rightRotate($e, 6) ^ $this->_rightRotate($e, 11) ^ $this->_rightRotate($e, 25); $ch = ($e & $f) ^ ($this->_not($e) & $g); $t1 = $this->_add($h, $s1, $ch, $k[$i], $w[$i]); $h = $g; $g = $f; $f = $e; $e = $this->_add($d, $t1); $d = $c; $c = $b; $b = $a; $a = $this->_add($t1, $t2); } // Add this chunk's hash to result so far $hash = array( $this->_add($hash[0], $a), $this->_add($hash[1], $b), $this->_add($hash[2], $c), $this->_add($hash[3], $d), $this->_add($hash[4], $e), $this->_add($hash[5], $f), $this->_add($hash[6], $g), $this->_add($hash[7], $h) ); } // Produce the final hash value (big-endian) return pack('N8', $hash[0], $hash[1], $hash[2], $hash[3], $hash[4], $hash[5], $hash[6], $hash[7]); } /** * Pure-PHP implementation of SHA384 and SHA512 * * @access private * @param string $m */ function _sha512($m) { static $init384, $init512, $k; if (!isset($k)) { // Initialize variables $init384 = array( // initial values for SHA384 'cbbb9d5dc1059ed8', '629a292a367cd507', '9159015a3070dd17', '152fecd8f70e5939', '67332667ffc00b31', '8eb44a8768581511', 'db0c2e0d64f98fa7', '47b5481dbefa4fa4' ); $init512 = array( // initial values for SHA512 '6a09e667f3bcc908', 'bb67ae8584caa73b', '3c6ef372fe94f82b', 'a54ff53a5f1d36f1', '510e527fade682d1', '9b05688c2b3e6c1f', '1f83d9abfb41bd6b', '5be0cd19137e2179' ); for ($i = 0; $i < 8; $i++) { $init384[$i] = new BigInteger($init384[$i], 16); $init384[$i]->setPrecision(64); $init512[$i] = new BigInteger($init512[$i], 16); $init512[$i]->setPrecision(64); } // Initialize table of round constants // (first 64 bits of the fractional parts of the cube roots of the first 80 primes 2..409) $k = array( '428a2f98d728ae22', '7137449123ef65cd', 'b5c0fbcfec4d3b2f', 'e9b5dba58189dbbc', '3956c25bf348b538', '59f111f1b605d019', '923f82a4af194f9b', 'ab1c5ed5da6d8118', 'd807aa98a3030242', '12835b0145706fbe', '243185be4ee4b28c', '550c7dc3d5ffb4e2', '72be5d74f27b896f', '80deb1fe3b1696b1', '9bdc06a725c71235', 'c19bf174cf692694', 'e49b69c19ef14ad2', 'efbe4786384f25e3', '0fc19dc68b8cd5b5', '240ca1cc77ac9c65', '2de92c6f592b0275', '4a7484aa6ea6e483', '5cb0a9dcbd41fbd4', '76f988da831153b5', '983e5152ee66dfab', 'a831c66d2db43210', 'b00327c898fb213f', 'bf597fc7beef0ee4', 'c6e00bf33da88fc2', 'd5a79147930aa725', '06ca6351e003826f', '142929670a0e6e70', '27b70a8546d22ffc', '2e1b21385c26c926', '4d2c6dfc5ac42aed', '53380d139d95b3df', '650a73548baf63de', '766a0abb3c77b2a8', '81c2c92e47edaee6', '92722c851482353b', 'a2bfe8a14cf10364', 'a81a664bbc423001', 'c24b8b70d0f89791', 'c76c51a30654be30', 'd192e819d6ef5218', 'd69906245565a910', 'f40e35855771202a', '106aa07032bbd1b8', '19a4c116b8d2d0c8', '1e376c085141ab53', '2748774cdf8eeb99', '34b0bcb5e19b48a8', '391c0cb3c5c95a63', '4ed8aa4ae3418acb', '5b9cca4f7763e373', '682e6ff3d6b2b8a3', '748f82ee5defb2fc', '78a5636f43172f60', '84c87814a1f0ab72', '8cc702081a6439ec', '90befffa23631e28', 'a4506cebde82bde9', 'bef9a3f7b2c67915', 'c67178f2e372532b', 'ca273eceea26619c', 'd186b8c721c0c207', 'eada7dd6cde0eb1e', 'f57d4f7fee6ed178', '06f067aa72176fba', '0a637dc5a2c898a6', '113f9804bef90dae', '1b710b35131c471b', '28db77f523047d84', '32caab7b40c72493', '3c9ebe0a15c9bebc', '431d67c49c100d4c', '4cc5d4becb3e42b6', '597f299cfc657e2a', '5fcb6fab3ad6faec', '6c44198c4a475817' ); for ($i = 0; $i < 80; $i++) { $k[$i] = new BigInteger($k[$i], 16); } } $hash = $this->l == 48 ? $init384 : $init512; // Pre-processing $length = strlen($m); // to round to nearest 112 mod 128, we'll add 128 - (length + (128 - 112)) % 128 $m.= str_repeat(chr(0), 128 - (($length + 16) & 0x7F)); $m[$length] = chr(0x80); // we don't support hashing strings 512MB long $m.= pack('N4', 0, 0, 0, $length << 3); // Process the message in successive 1024-bit chunks $chunks = str_split($m, 128); foreach ($chunks as $chunk) { $w = array(); for ($i = 0; $i < 16; $i++) { $temp = new BigInteger($this->_string_shift($chunk, 8), 256); $temp->setPrecision(64); $w[] = $temp; } // Extend the sixteen 32-bit words into eighty 32-bit words for ($i = 16; $i < 80; $i++) { $temp = array( $w[$i - 15]->bitwise_rightRotate(1), $w[$i - 15]->bitwise_rightRotate(8), $w[$i - 15]->bitwise_rightShift(7) ); $s0 = $temp[0]->bitwise_xor($temp[1]); $s0 = $s0->bitwise_xor($temp[2]); $temp = array( $w[$i - 2]->bitwise_rightRotate(19), $w[$i - 2]->bitwise_rightRotate(61), $w[$i - 2]->bitwise_rightShift(6) ); $s1 = $temp[0]->bitwise_xor($temp[1]); $s1 = $s1->bitwise_xor($temp[2]); $w[$i] = $w[$i - 16]->copy(); $w[$i] = $w[$i]->add($s0); $w[$i] = $w[$i]->add($w[$i - 7]); $w[$i] = $w[$i]->add($s1); } // Initialize hash value for this chunk $a = $hash[0]->copy(); $b = $hash[1]->copy(); $c = $hash[2]->copy(); $d = $hash[3]->copy(); $e = $hash[4]->copy(); $f = $hash[5]->copy(); $g = $hash[6]->copy(); $h = $hash[7]->copy(); // Main loop for ($i = 0; $i < 80; $i++) { $temp = array( $a->bitwise_rightRotate(28), $a->bitwise_rightRotate(34), $a->bitwise_rightRotate(39) ); $s0 = $temp[0]->bitwise_xor($temp[1]); $s0 = $s0->bitwise_xor($temp[2]); $temp = array( $a->bitwise_and($b), $a->bitwise_and($c), $b->bitwise_and($c) ); $maj = $temp[0]->bitwise_xor($temp[1]); $maj = $maj->bitwise_xor($temp[2]); $t2 = $s0->add($maj); $temp = array( $e->bitwise_rightRotate(14), $e->bitwise_rightRotate(18), $e->bitwise_rightRotate(41) ); $s1 = $temp[0]->bitwise_xor($temp[1]); $s1 = $s1->bitwise_xor($temp[2]); $temp = array( $e->bitwise_and($f), $g->bitwise_and($e->bitwise_not()) ); $ch = $temp[0]->bitwise_xor($temp[1]); $t1 = $h->add($s1); $t1 = $t1->add($ch); $t1 = $t1->add($k[$i]); $t1 = $t1->add($w[$i]); $h = $g->copy(); $g = $f->copy(); $f = $e->copy(); $e = $d->add($t1); $d = $c->copy(); $c = $b->copy(); $b = $a->copy(); $a = $t1->add($t2); } // Add this chunk's hash to result so far $hash = array( $hash[0]->add($a), $hash[1]->add($b), $hash[2]->add($c), $hash[3]->add($d), $hash[4]->add($e), $hash[5]->add($f), $hash[6]->add($g), $hash[7]->add($h) ); } // Produce the final hash value (big-endian) // (\phpseclib\Crypt\Hash::hash() trims the output for hashes but not for HMACs. as such, we trim the output here) $temp = $hash[0]->toBytes() . $hash[1]->toBytes() . $hash[2]->toBytes() . $hash[3]->toBytes() . $hash[4]->toBytes() . $hash[5]->toBytes(); if ($this->l != 48) { $temp.= $hash[6]->toBytes() . $hash[7]->toBytes(); } return $temp; } /** * Right Rotate * * @access private * @param int $int * @param int $amt * @see self::_sha256() * @return int */ function _rightRotate($int, $amt) { $invamt = 32 - $amt; $mask = (1 << $invamt) - 1; return (($int << $invamt) & 0xFFFFFFFF) | (($int >> $amt) & $mask); } /** * Right Shift * * @access private * @param int $int * @param int $amt * @see self::_sha256() * @return int */ function _rightShift($int, $amt) { $mask = (1 << (32 - $amt)) - 1; return ($int >> $amt) & $mask; } /** * Not * * @access private * @param int $int * @see self::_sha256() * @return int */ function _not($int) { return ~$int & 0xFFFFFFFF; } /** * Add * * _sha256() adds multiple unsigned 32-bit integers. Since PHP doesn't support unsigned integers and since the * possibility of overflow exists, care has to be taken. BigInteger could be used but this should be faster. * * @param int $... * @return int * @see self::_sha256() * @access private */ function _add() { static $mod; if (!isset($mod)) { $mod = pow(2, 32); } $result = 0; $arguments = func_get_args(); foreach ($arguments as $argument) { $result+= $argument < 0 ? ($argument & 0x7FFFFFFF) + 0x80000000 : $argument; } if ((php_uname('m') & "\xDF\xDF\xDF") != 'ARM') { return fmod($result, $mod); } return (fmod($result, 0x80000000) & 0x7FFFFFFF) | ((fmod(floor($result / 0x80000000), 2) & 1) << 31); } /** * String Shift * * Inspired by array_shift * * @param string $string * @param int $index * @return string * @access private */ function _string_shift(&$string, $index = 1) { $substr = substr($string, 0, $index); $string = substr($string, $index); return $substr; } } <?php /** * Pure-PHP implementation of Twofish. * * Uses mcrypt, if available, and an internal implementation, otherwise. * * PHP version 5 * * Useful resources are as follows: * * - {@link http://en.wikipedia.org/wiki/Twofish Wikipedia description of Twofish} * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $twofish = new \phpseclib\Crypt\Twofish(); * * $twofish->setKey('12345678901234567890123456789012'); * * $plaintext = str_repeat('a', 1024); * * echo $twofish->decrypt($twofish->encrypt($plaintext)); * ?> * </code> * * @category Crypt * @package Twofish * @author Jim Wigginton <terrafrost@php.net> * @author Hans-Juergen Petrich <petrich@tronic-media.com> * @copyright 2007 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; /** * Pure-PHP implementation of Twofish. * * @package Twofish * @author Jim Wigginton <terrafrost@php.net> * @author Hans-Juergen Petrich <petrich@tronic-media.com> * @access public */ class Twofish extends Base { /** * The mcrypt specific name of the cipher * * @see \phpseclib\Crypt\Base::cipher_name_mcrypt * @var string * @access private */ var $cipher_name_mcrypt = 'twofish'; /** * Optimizing value while CFB-encrypting * * @see \phpseclib\Crypt\Base::cfb_init_len * @var int * @access private */ var $cfb_init_len = 800; /** * Q-Table * * @var array * @access private */ var $q0 = array( 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38, 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 0xF2, 0xD0, 0x8B, 0x30, 0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82, 0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE, 0x16, 0x0C, 0xE3, 0x61, 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B, 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 0xE1, 0xE6, 0xBD, 0x45, 0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7, 0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF, 0x33, 0xC9, 0x62, 0x71, 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8, 0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 0xA1, 0x1D, 0xAA, 0xED, 0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90, 0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B, 0x5F, 0x93, 0x0A, 0xEF, 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B, 0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 0x2A, 0xCE, 0xCB, 0x2F, 0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A, 0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17, 0x55, 0x1F, 0x8A, 0x7D, 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72, 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 0x6E, 0x50, 0xDE, 0x68, 0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4, 0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42, 0x4A, 0x5E, 0xC1, 0xE0 ); /** * Q-Table * * @var array * @access private */ var $q1 = array( 0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B, 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1, 0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, 0x5E, 0xBA, 0xAE, 0x5B, 0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5, 0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 0xB2, 0x73, 0x4C, 0x54, 0x92, 0x74, 0x36, 0x51, 0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96, 0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, 0x13, 0x95, 0x9C, 0xC7, 0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8, 0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 0x03, 0x6F, 0x08, 0xBF, 0x40, 0xE7, 0x2B, 0xE2, 0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9, 0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, 0x66, 0x94, 0xA1, 0x1D, 0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E, 0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21, 0xC4, 0x1A, 0xEB, 0xD9, 0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01, 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, 0x4F, 0xF2, 0x65, 0x8E, 0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64, 0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44, 0xE0, 0x4D, 0x43, 0x69, 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, 0x22, 0xC9, 0xC0, 0x9B, 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9, 0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56, 0x55, 0x09, 0xBE, 0x91 ); /** * M-Table * * @var array * @access private */ var $m0 = array( 0xBCBC3275, 0xECEC21F3, 0x202043C6, 0xB3B3C9F4, 0xDADA03DB, 0x02028B7B, 0xE2E22BFB, 0x9E9EFAC8, 0xC9C9EC4A, 0xD4D409D3, 0x18186BE6, 0x1E1E9F6B, 0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x2626B74B, 0x3C3C57D6, 0x93938A32, 0x8282EED8, 0x525298FD, 0x7B7BD437, 0xBBBB3771, 0x5B5B97F1, 0x474783E1, 0x24243C30, 0x5151E20F, 0xBABAC6F8, 0x4A4AF31B, 0xBFBF4887, 0x0D0D70FA, 0xB0B0B306, 0x7575DE3F, 0xD2D2FD5E, 0x7D7D20BA, 0x666631AE, 0x3A3AA35B, 0x59591C8A, 0x00000000, 0xCDCD93BC, 0x1A1AE09D, 0xAEAE2C6D, 0x7F7FABC1, 0x2B2BC7B1, 0xBEBEB90E, 0xE0E0A080, 0x8A8A105D, 0x3B3B52D2, 0x6464BAD5, 0xD8D888A0, 0xE7E7A584, 0x5F5FE807, 0x1B1B1114, 0x2C2CC2B5, 0xFCFCB490, 0x3131272C, 0x808065A3, 0x73732AB2, 0x0C0C8173, 0x79795F4C, 0x6B6B4154, 0x4B4B0292, 0x53536974, 0x94948F36, 0x83831F51, 0x2A2A3638, 0xC4C49CB0, 0x2222C8BD, 0xD5D5F85A, 0xBDBDC3FC, 0x48487860, 0xFFFFCE62, 0x4C4C0796, 0x4141776C, 0xC7C7E642, 0xEBEB24F7, 0x1C1C1410, 0x5D5D637C, 0x36362228, 0x6767C027, 0xE9E9AF8C, 0x4444F913, 0x1414EA95, 0xF5F5BB9C, 0xCFCF18C7, 0x3F3F2D24, 0xC0C0E346, 0x7272DB3B, 0x54546C70, 0x29294CCA, 0xF0F035E3, 0x0808FE85, 0xC6C617CB, 0xF3F34F11, 0x8C8CE4D0, 0xA4A45993, 0xCACA96B8, 0x68683BA6, 0xB8B84D83, 0x38382820, 0xE5E52EFF, 0xADAD569F, 0x0B0B8477, 0xC8C81DC3, 0x9999FFCC, 0x5858ED03, 0x19199A6F, 0x0E0E0A08, 0x95957EBF, 0x70705040, 0xF7F730E7, 0x6E6ECF2B, 0x1F1F6EE2, 0xB5B53D79, 0x09090F0C, 0x616134AA, 0x57571682, 0x9F9F0B41, 0x9D9D803A, 0x111164EA, 0x2525CDB9, 0xAFAFDDE4, 0x4545089A, 0xDFDF8DA4, 0xA3A35C97, 0xEAEAD57E, 0x353558DA, 0xEDEDD07A, 0x4343FC17, 0xF8F8CB66, 0xFBFBB194, 0x3737D3A1, 0xFAFA401D, 0xC2C2683D, 0xB4B4CCF0, 0x32325DDE, 0x9C9C71B3, 0x5656E70B, 0xE3E3DA72, 0x878760A7, 0x15151B1C, 0xF9F93AEF, 0x6363BFD1, 0x3434A953, 0x9A9A853E, 0xB1B1428F, 0x7C7CD133, 0x88889B26, 0x3D3DA65F, 0xA1A1D7EC, 0xE4E4DF76, 0x8181942A, 0x91910149, 0x0F0FFB81, 0xEEEEAA88, 0x161661EE, 0xD7D77321, 0x9797F5C4, 0xA5A5A81A, 0xFEFE3FEB, 0x6D6DB5D9, 0x7878AEC5, 0xC5C56D39, 0x1D1DE599, 0x7676A4CD, 0x3E3EDCAD, 0xCBCB6731, 0xB6B6478B, 0xEFEF5B01, 0x12121E18, 0x6060C523, 0x6A6AB0DD, 0x4D4DF61F, 0xCECEE94E, 0xDEDE7C2D, 0x55559DF9, 0x7E7E5A48, 0x2121B24F, 0x03037AF2, 0xA0A02665, 0x5E5E198E, 0x5A5A6678, 0x65654B5C, 0x62624E58, 0xFDFD4519, 0x0606F48D, 0x404086E5, 0xF2F2BE98, 0x3333AC57, 0x17179067, 0x05058E7F, 0xE8E85E05, 0x4F4F7D64, 0x89896AAF, 0x10109563, 0x74742FB6, 0x0A0A75FE, 0x5C5C92F5, 0x9B9B74B7, 0x2D2D333C, 0x3030D6A5, 0x2E2E49CE, 0x494989E9, 0x46467268, 0x77775544, 0xA8A8D8E0, 0x9696044D, 0x2828BD43, 0xA9A92969, 0xD9D97929, 0x8686912E, 0xD1D187AC, 0xF4F44A15, 0x8D8D1559, 0xD6D682A8, 0xB9B9BC0A, 0x42420D9E, 0xF6F6C16E, 0x2F2FB847, 0xDDDD06DF, 0x23233934, 0xCCCC6235, 0xF1F1C46A, 0xC1C112CF, 0x8585EBDC, 0x8F8F9E22, 0x7171A1C9, 0x9090F0C0, 0xAAAA539B, 0x0101F189, 0x8B8BE1D4, 0x4E4E8CED, 0x8E8E6FAB, 0xABABA212, 0x6F6F3EA2, 0xE6E6540D, 0xDBDBF252, 0x92927BBB, 0xB7B7B602, 0x6969CA2F, 0x3939D9A9, 0xD3D30CD7, 0xA7A72361, 0xA2A2AD1E, 0xC3C399B4, 0x6C6C4450, 0x07070504, 0x04047FF6, 0x272746C2, 0xACACA716, 0xD0D07625, 0x50501386, 0xDCDCF756, 0x84841A55, 0xE1E15109, 0x7A7A25BE, 0x1313EF91 ); /** * M-Table * * @var array * @access private */ var $m1 = array( 0xA9D93939, 0x67901717, 0xB3719C9C, 0xE8D2A6A6, 0x04050707, 0xFD985252, 0xA3658080, 0x76DFE4E4, 0x9A084545, 0x92024B4B, 0x80A0E0E0, 0x78665A5A, 0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x38362A2A, 0x0D54E6E6, 0xC6432020, 0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0xF724EBEB, 0xECD7A1A1, 0x6C774141, 0x43BD2828, 0x7532BCBC, 0x37D47B7B, 0x269B8888, 0xFA700D0D, 0x13F94444, 0x94B1FBFB, 0x485A7E7E, 0xF27A0303, 0xD0E48C8C, 0x8B47B6B6, 0x303C2424, 0x84A5E7E7, 0x54416B6B, 0xDF06DDDD, 0x23C56060, 0x1945FDFD, 0x5BA33A3A, 0x3D68C2C2, 0x59158D8D, 0xF321ECEC, 0xAE316666, 0xA23E6F6F, 0x82165757, 0x63951010, 0x015BEFEF, 0x834DB8B8, 0x2E918686, 0xD9B56D6D, 0x511F8383, 0x9B53AAAA, 0x7C635D5D, 0xA63B6868, 0xEB3FFEFE, 0xA5D63030, 0xBE257A7A, 0x16A7ACAC, 0x0C0F0909, 0xE335F0F0, 0x6123A7A7, 0xC0F09090, 0x8CAFE9E9, 0x3A809D9D, 0xF5925C5C, 0x73810C0C, 0x2C273131, 0x2576D0D0, 0x0BE75656, 0xBB7B9292, 0x4EE9CECE, 0x89F10101, 0x6B9F1E1E, 0x53A93434, 0x6AC4F1F1, 0xB499C3C3, 0xF1975B5B, 0xE1834747, 0xE66B1818, 0xBDC82222, 0x450E9898, 0xE26E1F1F, 0xF4C9B3B3, 0xB62F7474, 0x66CBF8F8, 0xCCFF9999, 0x95EA1414, 0x03ED5858, 0x56F7DCDC, 0xD4E18B8B, 0x1C1B1515, 0x1EADA2A2, 0xD70CD3D3, 0xFB2BE2E2, 0xC31DC8C8, 0x8E195E5E, 0xB5C22C2C, 0xE9894949, 0xCF12C1C1, 0xBF7E9595, 0xBA207D7D, 0xEA641111, 0x77840B0B, 0x396DC5C5, 0xAF6A8989, 0x33D17C7C, 0xC9A17171, 0x62CEFFFF, 0x7137BBBB, 0x81FB0F0F, 0x793DB5B5, 0x0951E1E1, 0xADDC3E3E, 0x242D3F3F, 0xCDA47676, 0xF99D5555, 0xD8EE8282, 0xE5864040, 0xC5AE7878, 0xB9CD2525, 0x4D049696, 0x44557777, 0x080A0E0E, 0x86135050, 0xE730F7F7, 0xA1D33737, 0x1D40FAFA, 0xAA346161, 0xED8C4E4E, 0x06B3B0B0, 0x706C5454, 0xB22A7373, 0xD2523B3B, 0x410B9F9F, 0x7B8B0202, 0xA088D8D8, 0x114FF3F3, 0x3167CBCB, 0xC2462727, 0x27C06767, 0x90B4FCFC, 0x20283838, 0xF67F0404, 0x60784848, 0xFF2EE5E5, 0x96074C4C, 0x5C4B6565, 0xB1C72B2B, 0xAB6F8E8E, 0x9E0D4242, 0x9CBBF5F5, 0x52F2DBDB, 0x1BF34A4A, 0x5FA63D3D, 0x9359A4A4, 0x0ABCB9B9, 0xEF3AF9F9, 0x91EF1313, 0x85FE0808, 0x49019191, 0xEE611616, 0x2D7CDEDE, 0x4FB22121, 0x8F42B1B1, 0x3BDB7272, 0x47B82F2F, 0x8748BFBF, 0x6D2CAEAE, 0x46E3C0C0, 0xD6573C3C, 0x3E859A9A, 0x6929A9A9, 0x647D4F4F, 0x2A948181, 0xCE492E2E, 0xCB17C6C6, 0x2FCA6969, 0xFCC3BDBD, 0x975CA3A3, 0x055EE8E8, 0x7AD0EDED, 0xAC87D1D1, 0x7F8E0505, 0xD5BA6464, 0x1AA8A5A5, 0x4BB72626, 0x0EB9BEBE, 0xA7608787, 0x5AF8D5D5, 0x28223636, 0x14111B1B, 0x3FDE7575, 0x2979D9D9, 0x88AAEEEE, 0x3C332D2D, 0x4C5F7979, 0x02B6B7B7, 0xB896CACA, 0xDA583535, 0xB09CC4C4, 0x17FC4343, 0x551A8484, 0x1FF64D4D, 0x8A1C5959, 0x7D38B2B2, 0x57AC3333, 0xC718CFCF, 0x8DF40606, 0x74695353, 0xB7749B9B, 0xC4F59797, 0x9F56ADAD, 0x72DAE3E3, 0x7ED5EAEA, 0x154AF4F4, 0x229E8F8F, 0x12A2ABAB, 0x584E6262, 0x07E85F5F, 0x99E51D1D, 0x34392323, 0x6EC1F6F6, 0x50446C6C, 0xDE5D3232, 0x68724646, 0x6526A0A0, 0xBC93CDCD, 0xDB03DADA, 0xF8C6BABA, 0xC8FA9E9E, 0xA882D6D6, 0x2BCF6E6E, 0x40507070, 0xDCEB8585, 0xFE750A0A, 0x328A9393, 0xA48DDFDF, 0xCA4C2929, 0x10141C1C, 0x2173D7D7, 0xF0CCB4B4, 0xD309D4D4, 0x5D108A8A, 0x0FE25151, 0x00000000, 0x6F9A1919, 0x9DE01A1A, 0x368F9494, 0x42E6C7C7, 0x4AECC9C9, 0x5EFDD2D2, 0xC1AB7F7F, 0xE0D8A8A8 ); /** * M-Table * * @var array * @access private */ var $m2 = array( 0xBC75BC32, 0xECF3EC21, 0x20C62043, 0xB3F4B3C9, 0xDADBDA03, 0x027B028B, 0xE2FBE22B, 0x9EC89EFA, 0xC94AC9EC, 0xD4D3D409, 0x18E6186B, 0x1E6B1E9F, 0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x264B26B7, 0x3CD63C57, 0x9332938A, 0x82D882EE, 0x52FD5298, 0x7B377BD4, 0xBB71BB37, 0x5BF15B97, 0x47E14783, 0x2430243C, 0x510F51E2, 0xBAF8BAC6, 0x4A1B4AF3, 0xBF87BF48, 0x0DFA0D70, 0xB006B0B3, 0x753F75DE, 0xD25ED2FD, 0x7DBA7D20, 0x66AE6631, 0x3A5B3AA3, 0x598A591C, 0x00000000, 0xCDBCCD93, 0x1A9D1AE0, 0xAE6DAE2C, 0x7FC17FAB, 0x2BB12BC7, 0xBE0EBEB9, 0xE080E0A0, 0x8A5D8A10, 0x3BD23B52, 0x64D564BA, 0xD8A0D888, 0xE784E7A5, 0x5F075FE8, 0x1B141B11, 0x2CB52CC2, 0xFC90FCB4, 0x312C3127, 0x80A38065, 0x73B2732A, 0x0C730C81, 0x794C795F, 0x6B546B41, 0x4B924B02, 0x53745369, 0x9436948F, 0x8351831F, 0x2A382A36, 0xC4B0C49C, 0x22BD22C8, 0xD55AD5F8, 0xBDFCBDC3, 0x48604878, 0xFF62FFCE, 0x4C964C07, 0x416C4177, 0xC742C7E6, 0xEBF7EB24, 0x1C101C14, 0x5D7C5D63, 0x36283622, 0x672767C0, 0xE98CE9AF, 0x441344F9, 0x149514EA, 0xF59CF5BB, 0xCFC7CF18, 0x3F243F2D, 0xC046C0E3, 0x723B72DB, 0x5470546C, 0x29CA294C, 0xF0E3F035, 0x088508FE, 0xC6CBC617, 0xF311F34F, 0x8CD08CE4, 0xA493A459, 0xCAB8CA96, 0x68A6683B, 0xB883B84D, 0x38203828, 0xE5FFE52E, 0xAD9FAD56, 0x0B770B84, 0xC8C3C81D, 0x99CC99FF, 0x580358ED, 0x196F199A, 0x0E080E0A, 0x95BF957E, 0x70407050, 0xF7E7F730, 0x6E2B6ECF, 0x1FE21F6E, 0xB579B53D, 0x090C090F, 0x61AA6134, 0x57825716, 0x9F419F0B, 0x9D3A9D80, 0x11EA1164, 0x25B925CD, 0xAFE4AFDD, 0x459A4508, 0xDFA4DF8D, 0xA397A35C, 0xEA7EEAD5, 0x35DA3558, 0xED7AEDD0, 0x431743FC, 0xF866F8CB, 0xFB94FBB1, 0x37A137D3, 0xFA1DFA40, 0xC23DC268, 0xB4F0B4CC, 0x32DE325D, 0x9CB39C71, 0x560B56E7, 0xE372E3DA, 0x87A78760, 0x151C151B, 0xF9EFF93A, 0x63D163BF, 0x345334A9, 0x9A3E9A85, 0xB18FB142, 0x7C337CD1, 0x8826889B, 0x3D5F3DA6, 0xA1ECA1D7, 0xE476E4DF, 0x812A8194, 0x91499101, 0x0F810FFB, 0xEE88EEAA, 0x16EE1661, 0xD721D773, 0x97C497F5, 0xA51AA5A8, 0xFEEBFE3F, 0x6DD96DB5, 0x78C578AE, 0xC539C56D, 0x1D991DE5, 0x76CD76A4, 0x3EAD3EDC, 0xCB31CB67, 0xB68BB647, 0xEF01EF5B, 0x1218121E, 0x602360C5, 0x6ADD6AB0, 0x4D1F4DF6, 0xCE4ECEE9, 0xDE2DDE7C, 0x55F9559D, 0x7E487E5A, 0x214F21B2, 0x03F2037A, 0xA065A026, 0x5E8E5E19, 0x5A785A66, 0x655C654B, 0x6258624E, 0xFD19FD45, 0x068D06F4, 0x40E54086, 0xF298F2BE, 0x335733AC, 0x17671790, 0x057F058E, 0xE805E85E, 0x4F644F7D, 0x89AF896A, 0x10631095, 0x74B6742F, 0x0AFE0A75, 0x5CF55C92, 0x9BB79B74, 0x2D3C2D33, 0x30A530D6, 0x2ECE2E49, 0x49E94989, 0x46684672, 0x77447755, 0xA8E0A8D8, 0x964D9604, 0x284328BD, 0xA969A929, 0xD929D979, 0x862E8691, 0xD1ACD187, 0xF415F44A, 0x8D598D15, 0xD6A8D682, 0xB90AB9BC, 0x429E420D, 0xF66EF6C1, 0x2F472FB8, 0xDDDFDD06, 0x23342339, 0xCC35CC62, 0xF16AF1C4, 0xC1CFC112, 0x85DC85EB, 0x8F228F9E, 0x71C971A1, 0x90C090F0, 0xAA9BAA53, 0x018901F1, 0x8BD48BE1, 0x4EED4E8C, 0x8EAB8E6F, 0xAB12ABA2, 0x6FA26F3E, 0xE60DE654, 0xDB52DBF2, 0x92BB927B, 0xB702B7B6, 0x692F69CA, 0x39A939D9, 0xD3D7D30C, 0xA761A723, 0xA21EA2AD, 0xC3B4C399, 0x6C506C44, 0x07040705, 0x04F6047F, 0x27C22746, 0xAC16ACA7, 0xD025D076, 0x50865013, 0xDC56DCF7, 0x8455841A, 0xE109E151, 0x7ABE7A25, 0x139113EF ); /** * M-Table * * @var array * @access private */ var $m3 = array( 0xD939A9D9, 0x90176790, 0x719CB371, 0xD2A6E8D2, 0x05070405, 0x9852FD98, 0x6580A365, 0xDFE476DF, 0x08459A08, 0x024B9202, 0xA0E080A0, 0x665A7866, 0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x362A3836, 0x54E60D54, 0x4320C643, 0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x24EBF724, 0xD7A1ECD7, 0x77416C77, 0xBD2843BD, 0x32BC7532, 0xD47B37D4, 0x9B88269B, 0x700DFA70, 0xF94413F9, 0xB1FB94B1, 0x5A7E485A, 0x7A03F27A, 0xE48CD0E4, 0x47B68B47, 0x3C24303C, 0xA5E784A5, 0x416B5441, 0x06DDDF06, 0xC56023C5, 0x45FD1945, 0xA33A5BA3, 0x68C23D68, 0x158D5915, 0x21ECF321, 0x3166AE31, 0x3E6FA23E, 0x16578216, 0x95106395, 0x5BEF015B, 0x4DB8834D, 0x91862E91, 0xB56DD9B5, 0x1F83511F, 0x53AA9B53, 0x635D7C63, 0x3B68A63B, 0x3FFEEB3F, 0xD630A5D6, 0x257ABE25, 0xA7AC16A7, 0x0F090C0F, 0x35F0E335, 0x23A76123, 0xF090C0F0, 0xAFE98CAF, 0x809D3A80, 0x925CF592, 0x810C7381, 0x27312C27, 0x76D02576, 0xE7560BE7, 0x7B92BB7B, 0xE9CE4EE9, 0xF10189F1, 0x9F1E6B9F, 0xA93453A9, 0xC4F16AC4, 0x99C3B499, 0x975BF197, 0x8347E183, 0x6B18E66B, 0xC822BDC8, 0x0E98450E, 0x6E1FE26E, 0xC9B3F4C9, 0x2F74B62F, 0xCBF866CB, 0xFF99CCFF, 0xEA1495EA, 0xED5803ED, 0xF7DC56F7, 0xE18BD4E1, 0x1B151C1B, 0xADA21EAD, 0x0CD3D70C, 0x2BE2FB2B, 0x1DC8C31D, 0x195E8E19, 0xC22CB5C2, 0x8949E989, 0x12C1CF12, 0x7E95BF7E, 0x207DBA20, 0x6411EA64, 0x840B7784, 0x6DC5396D, 0x6A89AF6A, 0xD17C33D1, 0xA171C9A1, 0xCEFF62CE, 0x37BB7137, 0xFB0F81FB, 0x3DB5793D, 0x51E10951, 0xDC3EADDC, 0x2D3F242D, 0xA476CDA4, 0x9D55F99D, 0xEE82D8EE, 0x8640E586, 0xAE78C5AE, 0xCD25B9CD, 0x04964D04, 0x55774455, 0x0A0E080A, 0x13508613, 0x30F7E730, 0xD337A1D3, 0x40FA1D40, 0x3461AA34, 0x8C4EED8C, 0xB3B006B3, 0x6C54706C, 0x2A73B22A, 0x523BD252, 0x0B9F410B, 0x8B027B8B, 0x88D8A088, 0x4FF3114F, 0x67CB3167, 0x4627C246, 0xC06727C0, 0xB4FC90B4, 0x28382028, 0x7F04F67F, 0x78486078, 0x2EE5FF2E, 0x074C9607, 0x4B655C4B, 0xC72BB1C7, 0x6F8EAB6F, 0x0D429E0D, 0xBBF59CBB, 0xF2DB52F2, 0xF34A1BF3, 0xA63D5FA6, 0x59A49359, 0xBCB90ABC, 0x3AF9EF3A, 0xEF1391EF, 0xFE0885FE, 0x01914901, 0x6116EE61, 0x7CDE2D7C, 0xB2214FB2, 0x42B18F42, 0xDB723BDB, 0xB82F47B8, 0x48BF8748, 0x2CAE6D2C, 0xE3C046E3, 0x573CD657, 0x859A3E85, 0x29A96929, 0x7D4F647D, 0x94812A94, 0x492ECE49, 0x17C6CB17, 0xCA692FCA, 0xC3BDFCC3, 0x5CA3975C, 0x5EE8055E, 0xD0ED7AD0, 0x87D1AC87, 0x8E057F8E, 0xBA64D5BA, 0xA8A51AA8, 0xB7264BB7, 0xB9BE0EB9, 0x6087A760, 0xF8D55AF8, 0x22362822, 0x111B1411, 0xDE753FDE, 0x79D92979, 0xAAEE88AA, 0x332D3C33, 0x5F794C5F, 0xB6B702B6, 0x96CAB896, 0x5835DA58, 0x9CC4B09C, 0xFC4317FC, 0x1A84551A, 0xF64D1FF6, 0x1C598A1C, 0x38B27D38, 0xAC3357AC, 0x18CFC718, 0xF4068DF4, 0x69537469, 0x749BB774, 0xF597C4F5, 0x56AD9F56, 0xDAE372DA, 0xD5EA7ED5, 0x4AF4154A, 0x9E8F229E, 0xA2AB12A2, 0x4E62584E, 0xE85F07E8, 0xE51D99E5, 0x39233439, 0xC1F66EC1, 0x446C5044, 0x5D32DE5D, 0x72466872, 0x26A06526, 0x93CDBC93, 0x03DADB03, 0xC6BAF8C6, 0xFA9EC8FA, 0x82D6A882, 0xCF6E2BCF, 0x50704050, 0xEB85DCEB, 0x750AFE75, 0x8A93328A, 0x8DDFA48D, 0x4C29CA4C, 0x141C1014, 0x73D72173, 0xCCB4F0CC, 0x09D4D309, 0x108A5D10, 0xE2510FE2, 0x00000000, 0x9A196F9A, 0xE01A9DE0, 0x8F94368F, 0xE6C742E6, 0xECC94AEC, 0xFDD25EFD, 0xAB7FC1AB, 0xD8A8E0D8 ); /** * The Key Schedule Array * * @var array * @access private */ var $K = array(); /** * The Key depended S-Table 0 * * @var array * @access private */ var $S0 = array(); /** * The Key depended S-Table 1 * * @var array * @access private */ var $S1 = array(); /** * The Key depended S-Table 2 * * @var array * @access private */ var $S2 = array(); /** * The Key depended S-Table 3 * * @var array * @access private */ var $S3 = array(); /** * Holds the last used key * * @var array * @access private */ var $kl; /** * The Key Length (in bytes) * * @see Crypt_Twofish::setKeyLength() * @var int * @access private */ var $key_length = 16; /** * Sets the key length. * * Valid key lengths are 128, 192 or 256 bits * * @access public * @param int $length */ function setKeyLength($length) { switch (true) { case $length <= 128: $this->key_length = 16; break; case $length <= 192: $this->key_length = 24; break; default: $this->key_length = 32; } parent::setKeyLength($length); } /** * Setup the key (expansion) * * @see \phpseclib\Crypt\Base::_setupKey() * @access private */ function _setupKey() { if (isset($this->kl['key']) && $this->key === $this->kl['key']) { // already expanded return; } $this->kl = array('key' => $this->key); /* Key expanding and generating the key-depended s-boxes */ $le_longs = unpack('V*', $this->key); $key = unpack('C*', $this->key); $m0 = $this->m0; $m1 = $this->m1; $m2 = $this->m2; $m3 = $this->m3; $q0 = $this->q0; $q1 = $this->q1; $K = $S0 = $S1 = $S2 = $S3 = array(); switch (strlen($this->key)) { case 16: list($s7, $s6, $s5, $s4) = $this->_mdsrem($le_longs[1], $le_longs[2]); list($s3, $s2, $s1, $s0) = $this->_mdsrem($le_longs[3], $le_longs[4]); for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) { $A = $m0[$q0[$q0[$i] ^ $key[ 9]] ^ $key[1]] ^ $m1[$q0[$q1[$i] ^ $key[10]] ^ $key[2]] ^ $m2[$q1[$q0[$i] ^ $key[11]] ^ $key[3]] ^ $m3[$q1[$q1[$i] ^ $key[12]] ^ $key[4]]; $B = $m0[$q0[$q0[$j] ^ $key[13]] ^ $key[5]] ^ $m1[$q0[$q1[$j] ^ $key[14]] ^ $key[6]] ^ $m2[$q1[$q0[$j] ^ $key[15]] ^ $key[7]] ^ $m3[$q1[$q1[$j] ^ $key[16]] ^ $key[8]]; $B = ($B << 8) | ($B >> 24 & 0xff); $A = $this->safe_intval($A + $B); $K[] = $A; $A = $this->safe_intval($A + $B); $K[] = ($A << 9 | $A >> 23 & 0x1ff); } for ($i = 0; $i < 256; ++$i) { $S0[$i] = $m0[$q0[$q0[$i] ^ $s4] ^ $s0]; $S1[$i] = $m1[$q0[$q1[$i] ^ $s5] ^ $s1]; $S2[$i] = $m2[$q1[$q0[$i] ^ $s6] ^ $s2]; $S3[$i] = $m3[$q1[$q1[$i] ^ $s7] ^ $s3]; } break; case 24: list($sb, $sa, $s9, $s8) = $this->_mdsrem($le_longs[1], $le_longs[2]); list($s7, $s6, $s5, $s4) = $this->_mdsrem($le_longs[3], $le_longs[4]); list($s3, $s2, $s1, $s0) = $this->_mdsrem($le_longs[5], $le_longs[6]); for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) { $A = $m0[$q0[$q0[$q1[$i] ^ $key[17]] ^ $key[ 9]] ^ $key[1]] ^ $m1[$q0[$q1[$q1[$i] ^ $key[18]] ^ $key[10]] ^ $key[2]] ^ $m2[$q1[$q0[$q0[$i] ^ $key[19]] ^ $key[11]] ^ $key[3]] ^ $m3[$q1[$q1[$q0[$i] ^ $key[20]] ^ $key[12]] ^ $key[4]]; $B = $m0[$q0[$q0[$q1[$j] ^ $key[21]] ^ $key[13]] ^ $key[5]] ^ $m1[$q0[$q1[$q1[$j] ^ $key[22]] ^ $key[14]] ^ $key[6]] ^ $m2[$q1[$q0[$q0[$j] ^ $key[23]] ^ $key[15]] ^ $key[7]] ^ $m3[$q1[$q1[$q0[$j] ^ $key[24]] ^ $key[16]] ^ $key[8]]; $B = ($B << 8) | ($B >> 24 & 0xff); $A = $this->safe_intval($A + $B); $K[] = $A; $A = $this->safe_intval($A + $B); $K[] = ($A << 9 | $A >> 23 & 0x1ff); } for ($i = 0; $i < 256; ++$i) { $S0[$i] = $m0[$q0[$q0[$q1[$i] ^ $s8] ^ $s4] ^ $s0]; $S1[$i] = $m1[$q0[$q1[$q1[$i] ^ $s9] ^ $s5] ^ $s1]; $S2[$i] = $m2[$q1[$q0[$q0[$i] ^ $sa] ^ $s6] ^ $s2]; $S3[$i] = $m3[$q1[$q1[$q0[$i] ^ $sb] ^ $s7] ^ $s3]; } break; default: // 32 list($sf, $se, $sd, $sc) = $this->_mdsrem($le_longs[1], $le_longs[2]); list($sb, $sa, $s9, $s8) = $this->_mdsrem($le_longs[3], $le_longs[4]); list($s7, $s6, $s5, $s4) = $this->_mdsrem($le_longs[5], $le_longs[6]); list($s3, $s2, $s1, $s0) = $this->_mdsrem($le_longs[7], $le_longs[8]); for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) { $A = $m0[$q0[$q0[$q1[$q1[$i] ^ $key[25]] ^ $key[17]] ^ $key[ 9]] ^ $key[1]] ^ $m1[$q0[$q1[$q1[$q0[$i] ^ $key[26]] ^ $key[18]] ^ $key[10]] ^ $key[2]] ^ $m2[$q1[$q0[$q0[$q0[$i] ^ $key[27]] ^ $key[19]] ^ $key[11]] ^ $key[3]] ^ $m3[$q1[$q1[$q0[$q1[$i] ^ $key[28]] ^ $key[20]] ^ $key[12]] ^ $key[4]]; $B = $m0[$q0[$q0[$q1[$q1[$j] ^ $key[29]] ^ $key[21]] ^ $key[13]] ^ $key[5]] ^ $m1[$q0[$q1[$q1[$q0[$j] ^ $key[30]] ^ $key[22]] ^ $key[14]] ^ $key[6]] ^ $m2[$q1[$q0[$q0[$q0[$j] ^ $key[31]] ^ $key[23]] ^ $key[15]] ^ $key[7]] ^ $m3[$q1[$q1[$q0[$q1[$j] ^ $key[32]] ^ $key[24]] ^ $key[16]] ^ $key[8]]; $B = ($B << 8) | ($B >> 24 & 0xff); $A = $this->safe_intval($A + $B); $K[] = $A; $A = $this->safe_intval($A + $B); $K[] = ($A << 9 | $A >> 23 & 0x1ff); } for ($i = 0; $i < 256; ++$i) { $S0[$i] = $m0[$q0[$q0[$q1[$q1[$i] ^ $sc] ^ $s8] ^ $s4] ^ $s0]; $S1[$i] = $m1[$q0[$q1[$q1[$q0[$i] ^ $sd] ^ $s9] ^ $s5] ^ $s1]; $S2[$i] = $m2[$q1[$q0[$q0[$q0[$i] ^ $se] ^ $sa] ^ $s6] ^ $s2]; $S3[$i] = $m3[$q1[$q1[$q0[$q1[$i] ^ $sf] ^ $sb] ^ $s7] ^ $s3]; } } $this->K = $K; $this->S0 = $S0; $this->S1 = $S1; $this->S2 = $S2; $this->S3 = $S3; } /** * _mdsrem function using by the twofish cipher algorithm * * @access private * @param string $A * @param string $B * @return array */ function _mdsrem($A, $B) { // No gain by unrolling this loop. for ($i = 0; $i < 8; ++$i) { // Get most significant coefficient. $t = 0xff & ($B >> 24); // Shift the others up. $B = ($B << 8) | (0xff & ($A >> 24)); $A<<= 8; $u = $t << 1; // Subtract the modular polynomial on overflow. if ($t & 0x80) { $u^= 0x14d; } // Remove t * (a * x^2 + 1). $B ^= $t ^ ($u << 16); // Form u = a*t + t/a = t*(a + 1/a). $u^= 0x7fffffff & ($t >> 1); // Add the modular polynomial on underflow. if ($t & 0x01) { $u^= 0xa6 ; } // Remove t * (a + 1/a) * (x^3 + x). $B^= ($u << 24) | ($u << 8); } return array( 0xff & $B >> 24, 0xff & $B >> 16, 0xff & $B >> 8, 0xff & $B); } /** * Encrypts a block * * @access private * @param string $in * @return string */ function _encryptBlock($in) { $S0 = $this->S0; $S1 = $this->S1; $S2 = $this->S2; $S3 = $this->S3; $K = $this->K; $in = unpack("V4", $in); $R0 = $K[0] ^ $in[1]; $R1 = $K[1] ^ $in[2]; $R2 = $K[2] ^ $in[3]; $R3 = $K[3] ^ $in[4]; $ki = 7; while ($ki < 39) { $t0 = $S0[ $R0 & 0xff] ^ $S1[($R0 >> 8) & 0xff] ^ $S2[($R0 >> 16) & 0xff] ^ $S3[($R0 >> 24) & 0xff]; $t1 = $S0[($R1 >> 24) & 0xff] ^ $S1[ $R1 & 0xff] ^ $S2[($R1 >> 8) & 0xff] ^ $S3[($R1 >> 16) & 0xff]; $R2^= $this->safe_intval($t0 + $t1 + $K[++$ki]); $R2 = ($R2 >> 1 & 0x7fffffff) | ($R2 << 31); $R3 = ((($R3 >> 31) & 1) | ($R3 << 1)) ^ $this->safe_intval($t0 + ($t1 << 1) + $K[++$ki]); $t0 = $S0[ $R2 & 0xff] ^ $S1[($R2 >> 8) & 0xff] ^ $S2[($R2 >> 16) & 0xff] ^ $S3[($R2 >> 24) & 0xff]; $t1 = $S0[($R3 >> 24) & 0xff] ^ $S1[ $R3 & 0xff] ^ $S2[($R3 >> 8) & 0xff] ^ $S3[($R3 >> 16) & 0xff]; $R0^= $this->safe_intval($t0 + $t1 + $K[++$ki]); $R0 = ($R0 >> 1 & 0x7fffffff) | ($R0 << 31); $R1 = ((($R1 >> 31) & 1) | ($R1 << 1)) ^ $this->safe_intval($t0 + ($t1 << 1) + $K[++$ki]); } // @codingStandardsIgnoreStart return pack("V4", $K[4] ^ $R2, $K[5] ^ $R3, $K[6] ^ $R0, $K[7] ^ $R1); // @codingStandardsIgnoreEnd } /** * Decrypts a block * * @access private * @param string $in * @return string */ function _decryptBlock($in) { $S0 = $this->S0; $S1 = $this->S1; $S2 = $this->S2; $S3 = $this->S3; $K = $this->K; $in = unpack("V4", $in); $R0 = $K[4] ^ $in[1]; $R1 = $K[5] ^ $in[2]; $R2 = $K[6] ^ $in[3]; $R3 = $K[7] ^ $in[4]; $ki = 40; while ($ki > 8) { $t0 = $S0[$R0 & 0xff] ^ $S1[$R0 >> 8 & 0xff] ^ $S2[$R0 >> 16 & 0xff] ^ $S3[$R0 >> 24 & 0xff]; $t1 = $S0[$R1 >> 24 & 0xff] ^ $S1[$R1 & 0xff] ^ $S2[$R1 >> 8 & 0xff] ^ $S3[$R1 >> 16 & 0xff]; $R3^= $this->safe_intval($t0 + ($t1 << 1) + $K[--$ki]); $R3 = $R3 >> 1 & 0x7fffffff | $R3 << 31; $R2 = ($R2 >> 31 & 0x1 | $R2 << 1) ^ $this->safe_intval($t0 + $t1 + $K[--$ki]); $t0 = $S0[$R2 & 0xff] ^ $S1[$R2 >> 8 & 0xff] ^ $S2[$R2 >> 16 & 0xff] ^ $S3[$R2 >> 24 & 0xff]; $t1 = $S0[$R3 >> 24 & 0xff] ^ $S1[$R3 & 0xff] ^ $S2[$R3 >> 8 & 0xff] ^ $S3[$R3 >> 16 & 0xff]; $R1^= $this->safe_intval($t0 + ($t1 << 1) + $K[--$ki]); $R1 = $R1 >> 1 & 0x7fffffff | $R1 << 31; $R0 = ($R0 >> 31 & 0x1 | $R0 << 1) ^ $this->safe_intval($t0 + $t1 + $K[--$ki]); } // @codingStandardsIgnoreStart return pack("V4", $K[0] ^ $R2, $K[1] ^ $R3, $K[2] ^ $R0, $K[3] ^ $R1); // @codingStandardsIgnoreEnd } /** * Setup the performance-optimized function for de/encrypt() * * @see \phpseclib\Crypt\Base::_setupInlineCrypt() * @access private */ function _setupInlineCrypt() { $lambda_functions =& self::_getLambdaFunctions(); // Max. 10 Ultra-Hi-optimized inline-crypt functions. After that, we'll (still) create very fast code, but not the ultimate fast one. // (Currently, for Crypt_Twofish, one generated $lambda_function cost on php5.5@32bit ~140kb unfreeable mem and ~240kb on php5.5@64bit) $gen_hi_opt_code = (bool)(count($lambda_functions) < 10); // Generation of a unique hash for our generated code $code_hash = "Crypt_Twofish, {$this->mode}"; if ($gen_hi_opt_code) { $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key); } $safeint = $this->safe_intval_inline(); if (!isset($lambda_functions[$code_hash])) { switch (true) { case $gen_hi_opt_code: $K = $this->K; $init_crypt = ' static $S0, $S1, $S2, $S3; if (!$S0) { for ($i = 0; $i < 256; ++$i) { $S0[] = (int)$self->S0[$i]; $S1[] = (int)$self->S1[$i]; $S2[] = (int)$self->S2[$i]; $S3[] = (int)$self->S3[$i]; } } '; break; default: $K = array(); for ($i = 0; $i < 40; ++$i) { $K[] = '$K_' . $i; } $init_crypt = ' $S0 = $self->S0; $S1 = $self->S1; $S2 = $self->S2; $S3 = $self->S3; list(' . implode(',', $K) . ') = $self->K; '; } // Generating encrypt code: $encrypt_block = ' $in = unpack("V4", $in); $R0 = '.$K[0].' ^ $in[1]; $R1 = '.$K[1].' ^ $in[2]; $R2 = '.$K[2].' ^ $in[3]; $R3 = '.$K[3].' ^ $in[4]; '; for ($ki = 7, $i = 0; $i < 8; ++$i) { $encrypt_block.= ' $t0 = $S0[ $R0 & 0xff] ^ $S1[($R0 >> 8) & 0xff] ^ $S2[($R0 >> 16) & 0xff] ^ $S3[($R0 >> 24) & 0xff]; $t1 = $S0[($R1 >> 24) & 0xff] ^ $S1[ $R1 & 0xff] ^ $S2[($R1 >> 8) & 0xff] ^ $S3[($R1 >> 16) & 0xff]; $R2^= ' . sprintf($safeint, '$t0 + $t1 + ' . $K[++$ki]) . '; $R2 = ($R2 >> 1 & 0x7fffffff) | ($R2 << 31); $R3 = ((($R3 >> 31) & 1) | ($R3 << 1)) ^ ' . sprintf($safeint, '($t0 + ($t1 << 1) + ' . $K[++$ki] . ')') . '; $t0 = $S0[ $R2 & 0xff] ^ $S1[($R2 >> 8) & 0xff] ^ $S2[($R2 >> 16) & 0xff] ^ $S3[($R2 >> 24) & 0xff]; $t1 = $S0[($R3 >> 24) & 0xff] ^ $S1[ $R3 & 0xff] ^ $S2[($R3 >> 8) & 0xff] ^ $S3[($R3 >> 16) & 0xff]; $R0^= ' . sprintf($safeint, '($t0 + $t1 + ' . $K[++$ki] . ')') . '; $R0 = ($R0 >> 1 & 0x7fffffff) | ($R0 << 31); $R1 = ((($R1 >> 31) & 1) | ($R1 << 1)) ^ ' . sprintf($safeint, '($t0 + ($t1 << 1) + ' . $K[++$ki] . ')') . '; '; } $encrypt_block.= ' $in = pack("V4", ' . $K[4] . ' ^ $R2, ' . $K[5] . ' ^ $R3, ' . $K[6] . ' ^ $R0, ' . $K[7] . ' ^ $R1); '; // Generating decrypt code: $decrypt_block = ' $in = unpack("V4", $in); $R0 = '.$K[4].' ^ $in[1]; $R1 = '.$K[5].' ^ $in[2]; $R2 = '.$K[6].' ^ $in[3]; $R3 = '.$K[7].' ^ $in[4]; '; for ($ki = 40, $i = 0; $i < 8; ++$i) { $decrypt_block.= ' $t0 = $S0[$R0 & 0xff] ^ $S1[$R0 >> 8 & 0xff] ^ $S2[$R0 >> 16 & 0xff] ^ $S3[$R0 >> 24 & 0xff]; $t1 = $S0[$R1 >> 24 & 0xff] ^ $S1[$R1 & 0xff] ^ $S2[$R1 >> 8 & 0xff] ^ $S3[$R1 >> 16 & 0xff]; $R3^= ' . sprintf($safeint, '$t0 + ($t1 << 1) + ' . $K[--$ki]) . '; $R3 = $R3 >> 1 & 0x7fffffff | $R3 << 31; $R2 = ($R2 >> 31 & 0x1 | $R2 << 1) ^ ' . sprintf($safeint, '($t0 + $t1 + '.$K[--$ki] . ')') . '; $t0 = $S0[$R2 & 0xff] ^ $S1[$R2 >> 8 & 0xff] ^ $S2[$R2 >> 16 & 0xff] ^ $S3[$R2 >> 24 & 0xff]; $t1 = $S0[$R3 >> 24 & 0xff] ^ $S1[$R3 & 0xff] ^ $S2[$R3 >> 8 & 0xff] ^ $S3[$R3 >> 16 & 0xff]; $R1^= ' . sprintf($safeint, '$t0 + ($t1 << 1) + ' . $K[--$ki]) . '; $R1 = $R1 >> 1 & 0x7fffffff | $R1 << 31; $R0 = ($R0 >> 31 & 0x1 | $R0 << 1) ^ ' . sprintf($safeint, '($t0 + $t1 + '.$K[--$ki] . ')') . '; '; } $decrypt_block.= ' $in = pack("V4", ' . $K[0] . ' ^ $R2, ' . $K[1] . ' ^ $R3, ' . $K[2] . ' ^ $R0, ' . $K[3] . ' ^ $R1); '; $lambda_functions[$code_hash] = $this->_createInlineCryptFunction( array( 'init_crypt' => $init_crypt, 'init_encrypt' => '', 'init_decrypt' => '', 'encrypt_block' => $encrypt_block, 'decrypt_block' => $decrypt_block ) ); } $this->inline_crypt = $lambda_functions[$code_hash]; } } <?php /** * Pure-PHP implementation of RC4. * * Uses mcrypt, if available, and an internal implementation, otherwise. * * PHP version 5 * * Useful resources are as follows: * * - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm} * - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4} * * RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not * ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification. * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * $rc4 = new \phpseclib\Crypt\RC4(); * * $rc4->setKey('abcdefgh'); * * $size = 10 * 1024; * $plaintext = ''; * for ($i = 0; $i < $size; $i++) { * $plaintext.= 'a'; * } * * echo $rc4->decrypt($rc4->encrypt($plaintext)); * ?> * </code> * * @category Crypt * @package RC4 * @author Jim Wigginton <terrafrost@php.net> * @copyright 2007 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; /** * Pure-PHP implementation of RC4. * * @package RC4 * @author Jim Wigginton <terrafrost@php.net> * @access public */ class RC4 extends Base { /**#@+ * @access private * @see \phpseclib\Crypt\RC4::_crypt() */ const ENCRYPT = 0; const DECRYPT = 1; /**#@-*/ /** * Block Length of the cipher * * RC4 is a stream cipher * so we the block_size to 0 * * @see \phpseclib\Crypt\Base::block_size * @var int * @access private */ var $block_size = 0; /** * Key Length (in bytes) * * @see \phpseclib\Crypt\RC4::setKeyLength() * @var int * @access private */ var $key_length = 128; // = 1024 bits /** * The mcrypt specific name of the cipher * * @see \phpseclib\Crypt\Base::cipher_name_mcrypt * @var string * @access private */ var $cipher_name_mcrypt = 'arcfour'; /** * Holds whether performance-optimized $inline_crypt() can/should be used. * * @see \phpseclib\Crypt\Base::inline_crypt * @var mixed * @access private */ var $use_inline_crypt = false; // currently not available /** * The Key * * @see self::setKey() * @var string * @access private */ var $key; /** * The Key Stream for decryption and encryption * * @see self::setKey() * @var array * @access private */ var $stream; /** * Default Constructor. * * Determines whether or not the mcrypt extension should be used. * * @see \phpseclib\Crypt\Base::__construct() * @return \phpseclib\Crypt\RC4 * @access public */ function __construct() { parent::__construct(Base::MODE_STREAM); } /** * Test for engine validity * * This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine() * * @see \phpseclib\Crypt\Base::__construct() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { if ($engine == Base::ENGINE_OPENSSL) { if (version_compare(PHP_VERSION, '5.3.7') >= 0) { $this->cipher_name_openssl = 'rc4-40'; } else { switch (strlen($this->key)) { case 5: $this->cipher_name_openssl = 'rc4-40'; break; case 8: $this->cipher_name_openssl = 'rc4-64'; break; case 16: $this->cipher_name_openssl = 'rc4'; break; default: return false; } } } return parent::isValidEngine($engine); } /** * Dummy function. * * Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1]. * If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before * calling setKey(). * * [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol, * the IV's are relatively easy to predict, an attack described by * {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir} * can be used to quickly guess at the rest of the key. The following links elaborate: * * {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009} * {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack} * * @param string $iv * @see self::setKey() * @access public */ function setIV($iv) { } /** * Sets the key length * * Keys can be between 1 and 256 bytes long. * * @access public * @param int $length */ function setKeyLength($length) { if ($length < 8) { $this->key_length = 1; } elseif ($length > 2048) { $this->key_length = 256; } else { $this->key_length = $length >> 3; } parent::setKeyLength($length); } /** * Encrypts a message. * * @see \phpseclib\Crypt\Base::decrypt() * @see self::_crypt() * @access public * @param string $plaintext * @return string $ciphertext */ function encrypt($plaintext) { if ($this->engine != Base::ENGINE_INTERNAL) { return parent::encrypt($plaintext); } return $this->_crypt($plaintext, self::ENCRYPT); } /** * Decrypts a message. * * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)). * At least if the continuous buffer is disabled. * * @see \phpseclib\Crypt\Base::encrypt() * @see self::_crypt() * @access public * @param string $ciphertext * @return string $plaintext */ function decrypt($ciphertext) { if ($this->engine != Base::ENGINE_INTERNAL) { return parent::decrypt($ciphertext); } return $this->_crypt($ciphertext, self::DECRYPT); } /** * Encrypts a block * * @access private * @param string $in */ function _encryptBlock($in) { // RC4 does not utilize this method } /** * Decrypts a block * * @access private * @param string $in */ function _decryptBlock($in) { // RC4 does not utilize this method } /** * Setup the key (expansion) * * @see \phpseclib\Crypt\Base::_setupKey() * @access private */ function _setupKey() { $key = $this->key; $keyLength = strlen($key); $keyStream = range(0, 255); $j = 0; for ($i = 0; $i < 256; $i++) { $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255; $temp = $keyStream[$i]; $keyStream[$i] = $keyStream[$j]; $keyStream[$j] = $temp; } $this->stream = array(); $this->stream[self::DECRYPT] = $this->stream[self::ENCRYPT] = array( 0, // index $i 0, // index $j $keyStream ); } /** * Encrypts or decrypts a message. * * @see self::encrypt() * @see self::decrypt() * @access private * @param string $text * @param int $mode * @return string $text */ function _crypt($text, $mode) { if ($this->changed) { $this->_setup(); $this->changed = false; } $stream = &$this->stream[$mode]; if ($this->continuousBuffer) { $i = &$stream[0]; $j = &$stream[1]; $keyStream = &$stream[2]; } else { $i = $stream[0]; $j = $stream[1]; $keyStream = $stream[2]; } $len = strlen($text); for ($k = 0; $k < $len; ++$k) { $i = ($i + 1) & 255; $ksi = $keyStream[$i]; $j = ($j + $ksi) & 255; $ksj = $keyStream[$j]; $keyStream[$i] = $ksj; $keyStream[$j] = $ksi; $text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]); } return $text; } } <?php /** * Random Number Generator * * PHP version 5 * * Here's a short example of how to use this library: * <code> * <?php * include 'vendor/autoload.php'; * * echo bin2hex(\phpseclib\Crypt\Random::string(8)); * ?> * </code> * * @category Crypt * @package Random * @author Jim Wigginton <terrafrost@php.net> * @copyright 2007 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ namespace phpseclib\Crypt; /** * Pure-PHP Random Number Generator * * @package Random * @author Jim Wigginton <terrafrost@php.net> * @access public */ class Random { /** * Generate a random string. * * Although microoptimizations are generally discouraged as they impair readability this function is ripe with * microoptimizations because this function has the potential of being called a huge number of times. * eg. for RSA key generation. * * @param int $length * @return string */ static function string($length) { if (!$length) { return ''; } if (version_compare(PHP_VERSION, '7.0.0', '>=')) { try { return \random_bytes($length); } catch (\Throwable $e) { // If a sufficient source of randomness is unavailable, random_bytes() will throw an // object that implements the Throwable interface (Exception, TypeError, Error). // We don't actually need to do anything here. The string() method should just continue // as normal. Note, however, that if we don't have a sufficient source of randomness for // random_bytes(), most of the other calls here will fail too, so we'll end up using // the PHP implementation. } } if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { // method 1. prior to PHP 5.3 this would call rand() on windows hence the function_exists('class_alias') call. // ie. class_alias is a function that was introduced in PHP 5.3 if (extension_loaded('mcrypt') && function_exists('class_alias')) { return @mcrypt_create_iv($length); } // method 2. openssl_random_pseudo_bytes was introduced in PHP 5.3.0 but prior to PHP 5.3.4 there was, // to quote <http://php.net/ChangeLog-5.php#5.3.4>, "possible blocking behavior". as of 5.3.4 // openssl_random_pseudo_bytes and mcrypt_create_iv do the exact same thing on Windows. ie. they both // call php_win32_get_random_bytes(): // // https://github.com/php/php-src/blob/7014a0eb6d1611151a286c0ff4f2238f92c120d6/ext/openssl/openssl.c#L5008 // https://github.com/php/php-src/blob/7014a0eb6d1611151a286c0ff4f2238f92c120d6/ext/mcrypt/mcrypt.c#L1392 // // php_win32_get_random_bytes() is defined thusly: // // https://github.com/php/php-src/blob/7014a0eb6d1611151a286c0ff4f2238f92c120d6/win32/winutil.c#L80 // // we're calling it, all the same, in the off chance that the mcrypt extension is not available if (extension_loaded('openssl') && version_compare(PHP_VERSION, '5.3.4', '>=')) { return openssl_random_pseudo_bytes($length); } } else { // method 1. the fastest if (extension_loaded('openssl')) { return openssl_random_pseudo_bytes($length); } // method 2 static $fp = true; if ($fp === true) { // warning's will be output unles the error suppression operator is used. errors such as // "open_basedir restriction in effect", "Permission denied", "No such file or directory", etc. $fp = @fopen('/dev/urandom', 'rb'); } if ($fp !== true && $fp !== false) { // surprisingly faster than !is_bool() or is_resource() return fread($fp, $length); } // method 3. pretty much does the same thing as method 2 per the following url: // https://github.com/php/php-src/blob/7014a0eb6d1611151a286c0ff4f2238f92c120d6/ext/mcrypt/mcrypt.c#L1391 // surprisingly slower than method 2. maybe that's because mcrypt_create_iv does a bunch of error checking that we're // not doing. regardless, this'll only be called if this PHP script couldn't open /dev/urandom due to open_basedir // restrictions or some such if (extension_loaded('mcrypt')) { return @mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); } } // at this point we have no choice but to use a pure-PHP CSPRNG // cascade entropy across multiple PHP instances by fixing the session and collecting all // environmental variables, including the previous session data and the current session // data. // // mt_rand seeds itself by looking at the PID and the time, both of which are (relatively) // easy to guess at. linux uses mouse clicks, keyboard timings, etc, as entropy sources, but // PHP isn't low level to be able to use those as sources and on a web server there's not likely // going to be a ton of keyboard or mouse action. web servers do have one thing that we can use // however, a ton of people visiting the website. obviously you don't want to base your seeding // soley on parameters a potential attacker sends but (1) not everything in $_SERVER is controlled // by the user and (2) this isn't just looking at the data sent by the current user - it's based // on the data sent by all users. one user requests the page and a hash of their info is saved. // another user visits the page and the serialization of their data is utilized along with the // server envirnment stuff and a hash of the previous http request data (which itself utilizes // a hash of the session data before that). certainly an attacker should be assumed to have // full control over his own http requests. he, however, is not going to have control over // everyone's http requests. static $crypto = false, $v; if ($crypto === false) { // save old session data $old_session_id = session_id(); $old_use_cookies = ini_get('session.use_cookies'); $old_session_cache_limiter = session_cache_limiter(); $_OLD_SESSION = isset($_SESSION) ? $_SESSION : false; if ($old_session_id != '') { session_write_close(); } session_id(1); ini_set('session.use_cookies', 0); session_cache_limiter(''); session_start(); $v = $seed = $_SESSION['seed'] = pack('H*', sha1( (isset($_SERVER) ? phpseclib_safe_serialize($_SERVER) : '') . (isset($_POST) ? phpseclib_safe_serialize($_POST) : '') . (isset($_GET) ? phpseclib_safe_serialize($_GET) : '') . (isset($_COOKIE) ? phpseclib_safe_serialize($_COOKIE) : '') . phpseclib_safe_serialize($GLOBALS) . phpseclib_safe_serialize($_SESSION) . phpseclib_safe_serialize($_OLD_SESSION) )); if (!isset($_SESSION['count'])) { $_SESSION['count'] = 0; } $_SESSION['count']++; session_write_close(); // restore old session data if ($old_session_id != '') { session_id($old_session_id); session_start(); ini_set('session.use_cookies', $old_use_cookies); session_cache_limiter($old_session_cache_limiter); } else { if ($_OLD_SESSION !== false) { $_SESSION = $_OLD_SESSION; unset($_OLD_SESSION); } else { unset($_SESSION); } } // in SSH2 a shared secret and an exchange hash are generated through the key exchange process. // the IV client to server is the hash of that "nonce" with the letter A and for the encryption key it's the letter C. // if the hash doesn't produce enough a key or an IV that's long enough concat successive hashes of the // original hash and the current hash. we'll be emulating that. for more info see the following URL: // // http://tools.ietf.org/html/rfc4253#section-7.2 // // see the is_string($crypto) part for an example of how to expand the keys $key = pack('H*', sha1($seed . 'A')); $iv = pack('H*', sha1($seed . 'C')); // ciphers are used as per the nist.gov link below. also, see this link: // // http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator#Designs_based_on_cryptographic_primitives switch (true) { case class_exists('\phpseclib\Crypt\AES'): $crypto = new AES(Base::MODE_CTR); break; case class_exists('\phpseclib\Crypt\Twofish'): $crypto = new Twofish(Base::MODE_CTR); break; case class_exists('\phpseclib\Crypt\Blowfish'): $crypto = new Blowfish(Base::MODE_CTR); break; case class_exists('\phpseclib\Crypt\TripleDES'): $crypto = new TripleDES(Base::MODE_CTR); break; case class_exists('\phpseclib\Crypt\DES'): $crypto = new DES(Base::MODE_CTR); break; case class_exists('\phpseclib\Crypt\RC4'): $crypto = new RC4(); break; default: user_error(__CLASS__ . ' requires at least one symmetric cipher be loaded'); return false; } $crypto->setKey($key); $crypto->setIV($iv); $crypto->enableContinuousBuffer(); } //return $crypto->encrypt(str_repeat("\0", $length)); // the following is based off of ANSI X9.31: // // http://csrc.nist.gov/groups/STM/cavp/documents/rng/931rngext.pdf // // OpenSSL uses that same standard for it's random numbers: // // http://www.opensource.apple.com/source/OpenSSL/OpenSSL-38/openssl/fips-1.0/rand/fips_rand.c // (do a search for "ANS X9.31 A.2.4") $result = ''; while (strlen($result) < $length) { $i = $crypto->encrypt(microtime()); // strlen(microtime()) == 21 $r = $crypto->encrypt($i ^ $v); // strlen($v) == 20 $v = $crypto->encrypt($r ^ $i); // strlen($r) == 20 $result.= $r; } return substr($result, 0, $length); } } if (!function_exists('phpseclib_safe_serialize')) { /** * Safely serialize variables * * If a class has a private __sleep() method it'll give a fatal error on PHP 5.2 and earlier. * PHP 5.3 will emit a warning. * * @param mixed $arr * @access public */ function phpseclib_safe_serialize(&$arr) { if (is_object($arr)) { return ''; } if (!is_array($arr)) { return serialize($arr); } // prevent circular array recursion if (isset($arr['__phpseclib_marker'])) { return ''; } $safearr = array(); $arr['__phpseclib_marker'] = true; foreach (array_keys($arr) as $key) { // do not recurse on the '__phpseclib_marker' key itself, for smaller memory usage if ($key !== '__phpseclib_marker') { $safearr[$key] = phpseclib_safe_serialize($arr[$key]); } } unset($arr['__phpseclib_marker']); return serialize($safearr); } } { "name": "phpseclib/phpseclib", "type": "library", "description": "PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.", "keywords": [ "security", "crypto", "cryptography", "encryption", "signature", "signing", "rsa", "aes", "blowfish", "twofish", "ssh", "sftp", "x509", "x.509", "asn1", "asn.1", "BigInteger" ], "homepage": "http://phpseclib.sourceforge.net", "license": "MIT", "authors": [ { "name": "Jim Wigginton", "email": "terrafrost@php.net", "role": "Lead Developer" }, { "name": "Patrick Monnerat", "email": "pm@datasphere.ch", "role": "Developer" }, { "name": "Andreas Fischer", "email": "bantu@phpbb.com", "role": "Developer" }, { "name": "Hans-Jürgen Petrich", "email": "petrich@tronic-media.com", "role": "Developer" }, { "name": "Graham Campbell", "email": "graham@alt-three.com", "role": "Developer" } ], "require": { "php": ">=5.3.3" }, "require-dev": { "phing/phing": "~2.7", "phpunit/phpunit": "^4.8.35|^5.7|^6.0", "sami/sami": "~2.0", "squizlabs/php_codesniffer": "~2.0" }, "suggest": { "ext-libsodium": "SSH2/SFTP can make use of some algorithms provided by the libsodium-php extension.", "ext-openssl": "Install the OpenSSL extension in order to speed up a wide variety of cryptographic operations.", "ext-mcrypt": "Install the Mcrypt extension in order to speed up a few other cryptographic operations.", "ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations." }, "autoload": { "files": [ "phpseclib/bootstrap.php" ], "psr-4": { "phpseclib\\": "phpseclib/" } } } build: false shallow_clone: false platform: - x86 - x64 clone_folder: C:\projects\phpseclib install: - cinst -y OpenSSL.Light - SET PATH=C:\Program Files\OpenSSL;%PATH% - sc config wuauserv start= auto - net start wuauserv - cinst -y php --version 5.6.30 - cd c:\tools\php56 - copy php.ini-production php.ini - echo date.timezone="UTC" >> php.ini - echo extension_dir=ext >> php.ini - echo extension=php_openssl.dll >> php.ini - echo extension=php_gmp.dll >> php.ini - cd C:\projects\phpseclib - SET PATH=C:\tools\php56;%PATH% - php.exe -r "readfile('http://getcomposer.org/installer');" | php.exe - php.exe composer.phar install --prefer-source --no-interaction test_script: - cd C:\projects\phpseclib - vendor\bin\phpunit.bat tests/Windows32Test.php# Backers phpseclib ongoing development is made possible by [Tidelift](https://tidelift.com/subscription/pkg/packagist-phpseclib-phpseclib?utm_source=packagist-phpseclib-phpseclib&utm_medium=referral&utm_campaign=readme) and by contributions by users like you. Thank you. ## Backers - Zane Hooper# phpseclib - PHP Secure Communications Library [](https://travis-ci.org/phpseclib/phpseclib) ## Supporting phpseclib - [Become a backer or sponsor on Patreon](https://www.patreon.com/phpseclib) - [One-time donation via PayPal or crypto-currencies](http://sourceforge.net/donate/index.php?group_id=198487) - [Subscribe to Tidelift](https://tidelift.com/subscription/pkg/packagist-phpseclib-phpseclib?utm_source=packagist-phpseclib-phpseclib&utm_medium=referral&utm_campaign=readme) ## Introduction MIT-licensed pure-PHP implementations of an arbitrary-precision integer arithmetic library, fully PKCS#1 (v2.1) compliant RSA, DES, 3DES, RC4, Rijndael, AES, Blowfish, Twofish, SSH-1, SSH-2, SFTP, and X.509 * [Browse Git](https://github.com/phpseclib/phpseclib) * [Code Coverage Report](https://coverage.phpseclib.org/2.0/latest/) ## Documentation * [Documentation / Manual](http://phpseclib.sourceforge.net/) * [API Documentation](https://api.phpseclib.org/2.0/) (generated by Sami) ## Branches ### master * Development Branch * Unstable API * Do not use in production ### 2.0 * Long term support (LTS) release * Modernized version of 1.0 * Minimum PHP version: 5.3.3 * PSR-4 autoloading with namespace rooted at `\phpseclib` * Install via Composer: `composer require phpseclib/phpseclib:~2.0` ### 1.0 * Long term support (LTS) release * PHP4 compatible * Composer compatible (PSR-0 autoloading) * Install using Composer: `composer require phpseclib/phpseclib:~1.0` * Install using PEAR: See [phpseclib PEAR Channel Documentation](http://phpseclib.sourceforge.net/pear.htm) * [Download 1.0.17 as ZIP](http://sourceforge.net/projects/phpseclib/files/phpseclib1.0.17.zip/download) ## Security contact information To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. ## Support Need Support? * [Checkout Questions and Answers on Stack Overflow](http://stackoverflow.com/questions/tagged/phpseclib) * [Create a Support Ticket on GitHub](https://github.com/phpseclib/phpseclib/issues/new) * [Browse the Support Forum](http://www.frostjedi.com/phpbb/viewforum.php?f=46) (no longer in use) ## Contributing 1. Fork the Project 2. Ensure you have Composer installed (see [Composer Download Instructions](https://getcomposer.org/download/)) 3. Install Development Dependencies ``` sh composer install ``` 4. Create a Feature Branch 5. (Recommended) Run the Test Suite ``` sh vendor/bin/phpunit ``` 6. (Recommended) Check whether your code conforms to our Coding Standards by running ``` sh vendor/bin/phing -f build/build.xml sniff ``` 7. Send us a Pull Request phpseclib Lead Developer: TerraFrost (Jim Wigginton) phpseclib Developers: monnerat (Patrick Monnerat) bantu (Andreas Fischer) petrich (Hans-Jürgen Petrich) GrahamCampbell (Graham Campbell) <?php namespace Firebase\JWT; class BeforeValidException extends \UnexpectedValueException { } <?php namespace Firebase\JWT; class SignatureInvalidException extends \UnexpectedValueException { } <?php namespace Firebase\JWT; use \DomainException; use \InvalidArgumentException; use \UnexpectedValueException; use \DateTime; /** * JSON Web Token implementation, based on this spec: * https://tools.ietf.org/html/rfc7519 * * PHP version 5 * * @category Authentication * @package Authentication_JWT * @author Neuman Vong <neuman@twilio.com> * @author Anant Narayanan <anant@php.net> * @license http://opensource.org/licenses/BSD-3-Clause 3-clause BSD * @link https://github.com/firebase/php-jwt */ class JWT { /** * When checking nbf, iat or expiration times, * we want to provide some extra leeway time to * account for clock skew. */ public static $leeway = 0; /** * Allow the current timestamp to be specified. * Useful for fixing a value within unit testing. * * Will default to PHP time() value if null. */ public static $timestamp = null; public static $supported_algs = array( 'HS256' => array('hash_hmac', 'SHA256'), 'HS512' => array('hash_hmac', 'SHA512'), 'HS384' => array('hash_hmac', 'SHA384'), 'RS256' => array('openssl', 'SHA256'), 'RS384' => array('openssl', 'SHA384'), 'RS512' => array('openssl', 'SHA512'), ); /** * Decodes a JWT string into a PHP object. * * @param string $jwt The JWT * @param string|array $key The key, or map of keys. * If the algorithm used is asymmetric, this is the public key * @param array $allowed_algs List of supported verification algorithms * Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256' * * @return object The JWT's payload as a PHP object * * @throws UnexpectedValueException Provided JWT was invalid * @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed * @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf' * @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat' * @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim * * @uses jsonDecode * @uses urlsafeB64Decode */ public static function decode($jwt, $key, array $allowed_algs = array()) { $timestamp = is_null(static::$timestamp) ? time() : static::$timestamp; if (empty($key)) { throw new InvalidArgumentException('Key may not be empty'); } $tks = explode('.', $jwt); if (count($tks) != 3) { throw new UnexpectedValueException('Wrong number of segments'); } list($headb64, $bodyb64, $cryptob64) = $tks; if (null === ($header = static::jsonDecode(static::urlsafeB64Decode($headb64)))) { throw new UnexpectedValueException('Invalid header encoding'); } if (null === $payload = static::jsonDecode(static::urlsafeB64Decode($bodyb64))) { throw new UnexpectedValueException('Invalid claims encoding'); } if (false === ($sig = static::urlsafeB64Decode($cryptob64))) { throw new UnexpectedValueException('Invalid signature encoding'); } if (empty($header->alg)) { throw new UnexpectedValueException('Empty algorithm'); } if (empty(static::$supported_algs[$header->alg])) { throw new UnexpectedValueException('Algorithm not supported'); } if (!in_array($header->alg, $allowed_algs)) { throw new UnexpectedValueException('Algorithm not allowed'); } if (is_array($key) || $key instanceof \ArrayAccess) { if (isset($header->kid)) { if (!isset($key[$header->kid])) { throw new UnexpectedValueException('"kid" invalid, unable to lookup correct key'); } $key = $key[$header->kid]; } else { throw new UnexpectedValueException('"kid" empty, unable to lookup correct key'); } } // Check the signature if (!static::verify("$headb64.$bodyb64", $sig, $key, $header->alg)) { throw new SignatureInvalidException('Signature verification failed'); } // Check if the nbf if it is defined. This is the time that the // token can actually be used. If it's not yet that time, abort. if (isset($payload->nbf) && $payload->nbf > ($timestamp + static::$leeway)) { throw new BeforeValidException( 'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf) ); } // Check that this token has been created before 'now'. This prevents // using tokens that have been created for later use (and haven't // correctly used the nbf claim). if (isset($payload->iat) && $payload->iat > ($timestamp + static::$leeway)) { throw new BeforeValidException( 'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat) ); } // Check if this token has expired. if (isset($payload->exp) && ($timestamp - static::$leeway) >= $payload->exp) { throw new ExpiredException('Expired token'); } return $payload; } /** * Converts and signs a PHP object or array into a JWT string. * * @param object|array $payload PHP object or array * @param string $key The secret key. * If the algorithm used is asymmetric, this is the private key * @param string $alg The signing algorithm. * Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256' * @param mixed $keyId * @param array $head An array with header elements to attach * * @return string A signed JWT * * @uses jsonEncode * @uses urlsafeB64Encode */ public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $head = null) { $header = array('typ' => 'JWT', 'alg' => $alg); if ($keyId !== null) { $header['kid'] = $keyId; } if ( isset($head) && is_array($head) ) { $header = array_merge($head, $header); } $segments = array(); $segments[] = static::urlsafeB64Encode(static::jsonEncode($header)); $segments[] = static::urlsafeB64Encode(static::jsonEncode($payload)); $signing_input = implode('.', $segments); $signature = static::sign($signing_input, $key, $alg); $segments[] = static::urlsafeB64Encode($signature); return implode('.', $segments); } /** * Sign a string with a given key and algorithm. * * @param string $msg The message to sign * @param string|resource $key The secret key * @param string $alg The signing algorithm. * Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256' * * @return string An encrypted message * * @throws DomainException Unsupported algorithm was specified */ public static function sign($msg, $key, $alg = 'HS256') { if (empty(static::$supported_algs[$alg])) { throw new DomainException('Algorithm not supported'); } list($function, $algorithm) = static::$supported_algs[$alg]; switch($function) { case 'hash_hmac': return hash_hmac($algorithm, $msg, $key, true); case 'openssl': $signature = ''; $success = openssl_sign($msg, $signature, $key, $algorithm); if (!$success) { throw new DomainException("OpenSSL unable to sign data"); } else { return $signature; } } } /** * Verify a signature with the message, key and method. Not all methods * are symmetric, so we must have a separate verify and sign method. * * @param string $msg The original message (header and body) * @param string $signature The original signature * @param string|resource $key For HS*, a string key works. for RS*, must be a resource of an openssl public key * @param string $alg The algorithm * * @return bool * * @throws DomainException Invalid Algorithm or OpenSSL failure */ private static function verify($msg, $signature, $key, $alg) { if (empty(static::$supported_algs[$alg])) { throw new DomainException('Algorithm not supported'); } list($function, $algorithm) = static::$supported_algs[$alg]; switch($function) { case 'openssl': $success = openssl_verify($msg, $signature, $key, $algorithm); if ($success === 1) { return true; } elseif ($success === 0) { return false; } // returns 1 on success, 0 on failure, -1 on error. throw new DomainException( 'OpenSSL error: ' . openssl_error_string() ); case 'hash_hmac': default: $hash = hash_hmac($algorithm, $msg, $key, true); if (function_exists('hash_equals')) { return hash_equals($signature, $hash); } $len = min(static::safeStrlen($signature), static::safeStrlen($hash)); $status = 0; for ($i = 0; $i < $len; $i++) { $status |= (ord($signature[$i]) ^ ord($hash[$i])); } $status |= (static::safeStrlen($signature) ^ static::safeStrlen($hash)); return ($status === 0); } } /** * Decode a JSON string into a PHP object. * * @param string $input JSON string * * @return object Object representation of JSON string * * @throws DomainException Provided string was invalid JSON */ public static function jsonDecode($input) { if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) { /** In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you * to specify that large ints (like Steam Transaction IDs) should be treated as * strings, rather than the PHP default behaviour of converting them to floats. */ $obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING); } else { /** Not all servers will support that, however, so for older versions we must * manually detect large ints in the JSON string and quote them (thus converting *them to strings) before decoding, hence the preg_replace() call. */ $max_int_length = strlen((string) PHP_INT_MAX) - 1; $json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input); $obj = json_decode($json_without_bigints); } if (function_exists('json_last_error') && $errno = json_last_error()) { static::handleJsonError($errno); } elseif ($obj === null && $input !== 'null') { throw new DomainException('Null result with non-null input'); } return $obj; } /** * Encode a PHP object into a JSON string. * * @param object|array $input A PHP object or array * * @return string JSON representation of the PHP object or array * * @throws DomainException Provided object could not be encoded to valid JSON */ public static function jsonEncode($input) { $json = json_encode($input); if (function_exists('json_last_error') && $errno = json_last_error()) { static::handleJsonError($errno); } elseif ($json === 'null' && $input !== null) { throw new DomainException('Null result with non-null input'); } return $json; } /** * Decode a string with URL-safe Base64. * * @param string $input A Base64 encoded string * * @return string A decoded string */ public static function urlsafeB64Decode($input) { $remainder = strlen($input) % 4; if ($remainder) { $padlen = 4 - $remainder; $input .= str_repeat('=', $padlen); } return base64_decode(strtr($input, '-_', '+/')); } /** * Encode a string with URL-safe Base64. * * @param string $input The string you want encoded * * @return string The base64 encode of what you passed in */ public static function urlsafeB64Encode($input) { return str_replace('=', '', strtr(base64_encode($input), '+/', '-_')); } /** * Helper method to create a JSON error. * * @param int $errno An error number from json_last_error() * * @return void */ private static function handleJsonError($errno) { $messages = array( JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', JSON_ERROR_UTF8 => 'Malformed UTF-8 characters' //PHP >= 5.3.3 ); throw new DomainException( isset($messages[$errno]) ? $messages[$errno] : 'Unknown JSON error: ' . $errno ); } /** * Get the number of bytes in cryptographic strings. * * @param string * * @return int */ private static function safeStrlen($str) { if (function_exists('mb_strlen')) { return mb_strlen($str, '8bit'); } return strlen($str); } } <?php namespace Firebase\JWT; class ExpiredException extends \UnexpectedValueException { } { "name": "firebase/php-jwt", "description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.", "homepage": "https://github.com/firebase/php-jwt", "authors": [ { "name": "Neuman Vong", "email": "neuman+pear@twilio.com", "role": "Developer" }, { "name": "Anant Narayanan", "email": "anant@php.net", "role": "Developer" } ], "license": "BSD-3-Clause", "require": { "php": ">=5.3.0" }, "autoload": { "psr-4": { "Firebase\\JWT\\": "src" } }, "require-dev": { "phpunit/phpunit": " 4.8.35" } } [](https://travis-ci.org/firebase/php-jwt) [](https://packagist.org/packages/firebase/php-jwt) [](https://packagist.org/packages/firebase/php-jwt) [](https://packagist.org/packages/firebase/php-jwt) PHP-JWT ======= A simple library to encode and decode JSON Web Tokens (JWT) in PHP, conforming to [RFC 7519](https://tools.ietf.org/html/rfc7519). Installation ------------ Use composer to manage your dependencies and download PHP-JWT: ```bash composer require firebase/php-jwt ``` Example ------- ```php <?php use \Firebase\JWT\JWT; $key = "example_key"; $token = array( "iss" => "http://example.org", "aud" => "http://example.com", "iat" => 1356999524, "nbf" => 1357000000 ); /** * IMPORTANT: * You must specify supported algorithms for your application. See * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40 * for a list of spec-compliant algorithms. */ $jwt = JWT::encode($token, $key); $decoded = JWT::decode($jwt, $key, array('HS256')); print_r($decoded); /* NOTE: This will now be an object instead of an associative array. To get an associative array, you will need to cast it as such: */ $decoded_array = (array) $decoded; /** * You can add a leeway to account for when there is a clock skew times between * the signing and verifying servers. It is recommended that this leeway should * not be bigger than a few minutes. * * Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef */ JWT::$leeway = 60; // $leeway in seconds $decoded = JWT::decode($jwt, $key, array('HS256')); ?> ``` Example with RS256 (openssl) ---------------------------- ```php <?php use \Firebase\JWT\JWT; $privateKey = <<<EOD -----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQC8kGa1pSjbSYZVebtTRBLxBz5H4i2p/llLCrEeQhta5kaQu/Rn vuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t0tyazyZ8JXw+KgXTxldMPEL9 5+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4ehde/zUxo6UvS7UrBQIDAQAB AoGAb/MXV46XxCFRxNuB8LyAtmLDgi/xRnTAlMHjSACddwkyKem8//8eZtw9fzxz bWZ/1/doQOuHBGYZU8aDzzj59FZ78dyzNFoF91hbvZKkg+6wGyd/LrGVEB+Xre0J Nil0GReM2AHDNZUYRv+HYJPIOrB0CRczLQsgFJ8K6aAD6F0CQQDzbpjYdx10qgK1 cP59UHiHjPZYC0loEsk7s+hUmT3QHerAQJMZWC11Qrn2N+ybwwNblDKv+s5qgMQ5 5tNoQ9IfAkEAxkyffU6ythpg/H0Ixe1I2rd0GbF05biIzO/i77Det3n4YsJVlDck ZkcvY3SK2iRIL4c9yY6hlIhs+K9wXTtGWwJBAO9Dskl48mO7woPR9uD22jDpNSwe k90OMepTjzSvlhjbfuPN1IdhqvSJTDychRwn1kIJ7LQZgQ8fVz9OCFZ/6qMCQGOb qaGwHmUK6xzpUbbacnYrIM6nLSkXgOAwv7XXCojvY614ILTK3iXiLBOxPu5Eu13k eUz9sHyD6vkgZzjtxXECQAkp4Xerf5TGfQXGXhxIX52yH+N2LtujCdkQZjXAsGdm B2zNzvrlgRmgBrklMTrMYgm1NPcW+bRLGcwgW2PTvNM= -----END RSA PRIVATE KEY----- EOD; $publicKey = <<<EOD -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8kGa1pSjbSYZVebtTRBLxBz5H 4i2p/llLCrEeQhta5kaQu/RnvuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t 0tyazyZ8JXw+KgXTxldMPEL95+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4 ehde/zUxo6UvS7UrBQIDAQAB -----END PUBLIC KEY----- EOD; $token = array( "iss" => "example.org", "aud" => "example.com", "iat" => 1356999524, "nbf" => 1357000000 ); $jwt = JWT::encode($token, $privateKey, 'RS256'); echo "Encode:\n" . print_r($jwt, true) . "\n"; $decoded = JWT::decode($jwt, $publicKey, array('RS256')); /* NOTE: This will now be an object instead of an associative array. To get an associative array, you will need to cast it as such: */ $decoded_array = (array) $decoded; echo "Decode:\n" . print_r($decoded_array, true) . "\n"; ?> ``` Changelog --------- #### 5.0.0 / 2017-06-26 - Support RS384 and RS512. See [#117](https://github.com/firebase/php-jwt/pull/117). Thanks [@joostfaassen](https://github.com/joostfaassen)! - Add an example for RS256 openssl. See [#125](https://github.com/firebase/php-jwt/pull/125). Thanks [@akeeman](https://github.com/akeeman)! - Detect invalid Base64 encoding in signature. See [#162](https://github.com/firebase/php-jwt/pull/162). Thanks [@psignoret](https://github.com/psignoret)! - Update `JWT::verify` to handle OpenSSL errors. See [#159](https://github.com/firebase/php-jwt/pull/159). Thanks [@bshaffer](https://github.com/bshaffer)! - Add `array` type hinting to `decode` method See [#101](https://github.com/firebase/php-jwt/pull/101). Thanks [@hywak](https://github.com/hywak)! - Add all JSON error types. See [#110](https://github.com/firebase/php-jwt/pull/110). Thanks [@gbalduzzi](https://github.com/gbalduzzi)! - Bugfix 'kid' not in given key list. See [#129](https://github.com/firebase/php-jwt/pull/129). Thanks [@stampycode](https://github.com/stampycode)! - Miscellaneous cleanup, documentation and test fixes. See [#107](https://github.com/firebase/php-jwt/pull/107), [#115](https://github.com/firebase/php-jwt/pull/115), [#160](https://github.com/firebase/php-jwt/pull/160), [#161](https://github.com/firebase/php-jwt/pull/161), and [#165](https://github.com/firebase/php-jwt/pull/165). Thanks [@akeeman](https://github.com/akeeman), [@chinedufn](https://github.com/chinedufn), and [@bshaffer](https://github.com/bshaffer)! #### 4.0.0 / 2016-07-17 - Add support for late static binding. See [#88](https://github.com/firebase/php-jwt/pull/88) for details. Thanks to [@chappy84](https://github.com/chappy84)! - Use static `$timestamp` instead of `time()` to improve unit testing. See [#93](https://github.com/firebase/php-jwt/pull/93) for details. Thanks to [@josephmcdermott](https://github.com/josephmcdermott)! - Fixes to exceptions classes. See [#81](https://github.com/firebase/php-jwt/pull/81) for details. Thanks to [@Maks3w](https://github.com/Maks3w)! - Fixes to PHPDoc. See [#76](https://github.com/firebase/php-jwt/pull/76) for details. Thanks to [@akeeman](https://github.com/akeeman)! #### 3.0.0 / 2015-07-22 - Minimum PHP version updated from `5.2.0` to `5.3.0`. - Add `\Firebase\JWT` namespace. See [#59](https://github.com/firebase/php-jwt/pull/59) for details. Thanks to [@Dashron](https://github.com/Dashron)! - Require a non-empty key to decode and verify a JWT. See [#60](https://github.com/firebase/php-jwt/pull/60) for details. Thanks to [@sjones608](https://github.com/sjones608)! - Cleaner documentation blocks in the code. See [#62](https://github.com/firebase/php-jwt/pull/62) for details. Thanks to [@johanderuijter](https://github.com/johanderuijter)! #### 2.2.0 / 2015-06-22 - Add support for adding custom, optional JWT headers to `JWT::encode()`. See [#53](https://github.com/firebase/php-jwt/pull/53/files) for details. Thanks to [@mcocaro](https://github.com/mcocaro)! #### 2.1.0 / 2015-05-20 - Add support for adding a leeway to `JWT:decode()` that accounts for clock skew between signing and verifying entities. Thanks to [@lcabral](https://github.com/lcabral)! - Add support for passing an object implementing the `ArrayAccess` interface for `$keys` argument in `JWT::decode()`. Thanks to [@aztech-dev](https://github.com/aztech-dev)! #### 2.0.0 / 2015-04-01 - **Note**: It is strongly recommended that you update to > v2.0.0 to address known security vulnerabilities in prior versions when both symmetric and asymmetric keys are used together. - Update signature for `JWT::decode(...)` to require an array of supported algorithms to use when verifying token signatures. Tests ----- Run the tests using phpunit: ```bash $ pear install PHPUnit $ phpunit --configuration phpunit.xml.dist PHPUnit 3.7.10 by Sebastian Bergmann. ..... Time: 0 seconds, Memory: 2.50Mb OK (5 tests, 5 assertions) ``` New Lines in private keys ----- If your private key contains `\n` characters, be sure to wrap it in double quotes `""` and not single quotes `''` in order to properly interpret the escaped characters. License ------- [3-Clause BSD](http://opensource.org/licenses/BSD-3-Clause). <?php namespace GuzzleHttp\Promise; /** * Get the global task queue used for promise resolution. * * This task queue MUST be run in an event loop in order for promises to be * settled asynchronously. It will be automatically run when synchronously * waiting on a promise. * * <code> * while ($eventLoop->isRunning()) { * GuzzleHttp\Promise\queue()->run(); * } * </code> * * @param TaskQueueInterface $assign Optionally specify a new queue instance. * * @return TaskQueueInterface */ function queue(TaskQueueInterface $assign = null) { static $queue; if ($assign) { $queue = $assign; } elseif (!$queue) { $queue = new TaskQueue(); } return $queue; } /** * Adds a function to run in the task queue when it is next `run()` and returns * a promise that is fulfilled or rejected with the result. * * @param callable $task Task function to run. * * @return PromiseInterface */ function task(callable $task) { $queue = queue(); $promise = new Promise([$queue, 'run']); $queue->add(function () use ($task, $promise) { try { $promise->resolve($task()); } catch (\Throwable $e) { $promise->reject($e); } catch (\Exception $e) { $promise->reject($e); } }); return $promise; } /** * Creates a promise for a value if the value is not a promise. * * @param mixed $value Promise or value. * * @return PromiseInterface */ function promise_for($value) { if ($value instanceof PromiseInterface) { return $value; } // Return a Guzzle promise that shadows the given promise. if (method_exists($value, 'then')) { $wfn = method_exists($value, 'wait') ? [$value, 'wait'] : null; $cfn = method_exists($value, 'cancel') ? [$value, 'cancel'] : null; $promise = new Promise($wfn, $cfn); $value->then([$promise, 'resolve'], [$promise, 'reject']); return $promise; } return new FulfilledPromise($value); } /** * Creates a rejected promise for a reason if the reason is not a promise. If * the provided reason is a promise, then it is returned as-is. * * @param mixed $reason Promise or reason. * * @return PromiseInterface */ function rejection_for($reason) { if ($reason instanceof PromiseInterface) { return $reason; } return new RejectedPromise($reason); } /** * Create an exception for a rejected promise value. * * @param mixed $reason * * @return \Exception|\Throwable */ function exception_for($reason) { return $reason instanceof \Exception || $reason instanceof \Throwable ? $reason : new RejectionException($reason); } /** * Returns an iterator for the given value. * * @param mixed $value * * @return \Iterator */ function iter_for($value) { if ($value instanceof \Iterator) { return $value; } elseif (is_array($value)) { return new \ArrayIterator($value); } else { return new \ArrayIterator([$value]); } } /** * Synchronously waits on a promise to resolve and returns an inspection state * array. * * Returns a state associative array containing a "state" key mapping to a * valid promise state. If the state of the promise is "fulfilled", the array * will contain a "value" key mapping to the fulfilled value of the promise. If * the promise is rejected, the array will contain a "reason" key mapping to * the rejection reason of the promise. * * @param PromiseInterface $promise Promise or value. * * @return array */ function inspect(PromiseInterface $promise) { try { return [ 'state' => PromiseInterface::FULFILLED, 'value' => $promise->wait() ]; } catch (RejectionException $e) { return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()]; } catch (\Throwable $e) { return ['state' => PromiseInterface::REJECTED, 'reason' => $e]; } catch (\Exception $e) { return ['state' => PromiseInterface::REJECTED, 'reason' => $e]; } } /** * Waits on all of the provided promises, but does not unwrap rejected promises * as thrown exception. * * Returns an array of inspection state arrays. * * @param PromiseInterface[] $promises Traversable of promises to wait upon. * * @return array * @see GuzzleHttp\Promise\inspect for the inspection state array format. */ function inspect_all($promises) { $results = []; foreach ($promises as $key => $promise) { $results[$key] = inspect($promise); } return $results; } /** * Waits on all of the provided promises and returns the fulfilled values. * * Returns an array that contains the value of each promise (in the same order * the promises were provided). An exception is thrown if any of the promises * are rejected. * * @param mixed $promises Iterable of PromiseInterface objects to wait on. * * @return array * @throws \Exception on error * @throws \Throwable on error in PHP >=7 */ function unwrap($promises) { $results = []; foreach ($promises as $key => $promise) { $results[$key] = $promise->wait(); } return $results; } /** * Given an array of promises, return a promise that is fulfilled when all the * items in the array are fulfilled. * * The promise's fulfillment value is an array with fulfillment values at * respective positions to the original array. If any promise in the array * rejects, the returned promise is rejected with the rejection reason. * * @param mixed $promises Promises or values. * * @return PromiseInterface */ function all($promises) { $results = []; return each( $promises, function ($value, $idx) use (&$results) { $results[$idx] = $value; }, function ($reason, $idx, Promise $aggregate) { $aggregate->reject($reason); } )->then(function () use (&$results) { ksort($results); return $results; }); } /** * Initiate a competitive race between multiple promises or values (values will * become immediately fulfilled promises). * * When count amount of promises have been fulfilled, the returned promise is * fulfilled with an array that contains the fulfillment values of the winners * in order of resolution. * * This prommise is rejected with a {@see GuzzleHttp\Promise\AggregateException} * if the number of fulfilled promises is less than the desired $count. * * @param int $count Total number of promises. * @param mixed $promises Promises or values. * * @return PromiseInterface */ function some($count, $promises) { $results = []; $rejections = []; return each( $promises, function ($value, $idx, PromiseInterface $p) use (&$results, $count) { if ($p->getState() !== PromiseInterface::PENDING) { return; } $results[$idx] = $value; if (count($results) >= $count) { $p->resolve(null); } }, function ($reason) use (&$rejections) { $rejections[] = $reason; } )->then( function () use (&$results, &$rejections, $count) { if (count($results) !== $count) { throw new AggregateException( 'Not enough promises to fulfill count', $rejections ); } ksort($results); return array_values($results); } ); } /** * Like some(), with 1 as count. However, if the promise fulfills, the * fulfillment value is not an array of 1 but the value directly. * * @param mixed $promises Promises or values. * * @return PromiseInterface */ function any($promises) { return some(1, $promises)->then(function ($values) { return $values[0]; }); } /** * Returns a promise that is fulfilled when all of the provided promises have * been fulfilled or rejected. * * The returned promise is fulfilled with an array of inspection state arrays. * * @param mixed $promises Promises or values. * * @return PromiseInterface * @see GuzzleHttp\Promise\inspect for the inspection state array format. */ function settle($promises) { $results = []; return each( $promises, function ($value, $idx) use (&$results) { $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value]; }, function ($reason, $idx) use (&$results) { $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason]; } )->then(function () use (&$results) { ksort($results); return $results; }); } /** * Given an iterator that yields promises or values, returns a promise that is * fulfilled with a null value when the iterator has been consumed or the * aggregate promise has been fulfilled or rejected. * * $onFulfilled is a function that accepts the fulfilled value, iterator * index, and the aggregate promise. The callback can invoke any necessary side * effects and choose to resolve or reject the aggregate promise if needed. * * $onRejected is a function that accepts the rejection reason, iterator * index, and the aggregate promise. The callback can invoke any necessary side * effects and choose to resolve or reject the aggregate promise if needed. * * @param mixed $iterable Iterator or array to iterate over. * @param callable $onFulfilled * @param callable $onRejected * * @return PromiseInterface */ function each( $iterable, callable $onFulfilled = null, callable $onRejected = null ) { return (new EachPromise($iterable, [ 'fulfilled' => $onFulfilled, 'rejected' => $onRejected ]))->promise(); } /** * Like each, but only allows a certain number of outstanding promises at any * given time. * * $concurrency may be an integer or a function that accepts the number of * pending promises and returns a numeric concurrency limit value to allow for * dynamic a concurrency size. * * @param mixed $iterable * @param int|callable $concurrency * @param callable $onFulfilled * @param callable $onRejected * * @return PromiseInterface */ function each_limit( $iterable, $concurrency, callable $onFulfilled = null, callable $onRejected = null ) { return (new EachPromise($iterable, [ 'fulfilled' => $onFulfilled, 'rejected' => $onRejected, 'concurrency' => $concurrency ]))->promise(); } /** * Like each_limit, but ensures that no promise in the given $iterable argument * is rejected. If any promise is rejected, then the aggregate promise is * rejected with the encountered rejection. * * @param mixed $iterable * @param int|callable $concurrency * @param callable $onFulfilled * * @return PromiseInterface */ function each_limit_all( $iterable, $concurrency, callable $onFulfilled = null ) { return each_limit( $iterable, $concurrency, $onFulfilled, function ($reason, $idx, PromiseInterface $aggregate) { $aggregate->reject($reason); } ); } /** * Returns true if a promise is fulfilled. * * @param PromiseInterface $promise * * @return bool */ function is_fulfilled(PromiseInterface $promise) { return $promise->getState() === PromiseInterface::FULFILLED; } /** * Returns true if a promise is rejected. * * @param PromiseInterface $promise * * @return bool */ function is_rejected(PromiseInterface $promise) { return $promise->getState() === PromiseInterface::REJECTED; } /** * Returns true if a promise is fulfilled or rejected. * * @param PromiseInterface $promise * * @return bool */ function is_settled(PromiseInterface $promise) { return $promise->getState() !== PromiseInterface::PENDING; } /** * @see Coroutine * * @param callable $generatorFn * * @return PromiseInterface */ function coroutine(callable $generatorFn) { return new Coroutine($generatorFn); } <?php // Don't redefine the functions if included multiple times. if (!function_exists('GuzzleHttp\Promise\promise_for')) { require __DIR__ . '/functions.php'; } <?php namespace GuzzleHttp\Promise; /** * Exception that is set as the reason for a promise that has been cancelled. */ class CancellationException extends RejectionException { } <?php namespace GuzzleHttp\Promise; use Exception; use Generator; use Throwable; /** * Creates a promise that is resolved using a generator that yields values or * promises (somewhat similar to C#'s async keyword). * * When called, the coroutine function will start an instance of the generator * and returns a promise that is fulfilled with its final yielded value. * * Control is returned back to the generator when the yielded promise settles. * This can lead to less verbose code when doing lots of sequential async calls * with minimal processing in between. * * use GuzzleHttp\Promise; * * function createPromise($value) { * return new Promise\FulfilledPromise($value); * } * * $promise = Promise\coroutine(function () { * $value = (yield createPromise('a')); * try { * $value = (yield createPromise($value . 'b')); * } catch (\Exception $e) { * // The promise was rejected. * } * yield $value . 'c'; * }); * * // Outputs "abc" * $promise->then(function ($v) { echo $v; }); * * @param callable $generatorFn Generator function to wrap into a promise. * * @return Promise * @link https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration */ final class Coroutine implements PromiseInterface { /** * @var PromiseInterface|null */ private $currentPromise; /** * @var Generator */ private $generator; /** * @var Promise */ private $result; public function __construct(callable $generatorFn) { $this->generator = $generatorFn(); $this->result = new Promise(function () { while (isset($this->currentPromise)) { $this->currentPromise->wait(); } }); $this->nextCoroutine($this->generator->current()); } public function then( callable $onFulfilled = null, callable $onRejected = null ) { return $this->result->then($onFulfilled, $onRejected); } public function otherwise(callable $onRejected) { return $this->result->otherwise($onRejected); } public function wait($unwrap = true) { return $this->result->wait($unwrap); } public function getState() { return $this->result->getState(); } public function resolve($value) { $this->result->resolve($value); } public function reject($reason) { $this->result->reject($reason); } public function cancel() { $this->currentPromise->cancel(); $this->result->cancel(); } private function nextCoroutine($yielded) { $this->currentPromise = promise_for($yielded) ->then([$this, '_handleSuccess'], [$this, '_handleFailure']); } /** * @internal */ public function _handleSuccess($value) { unset($this->currentPromise); try { $next = $this->generator->send($value); if ($this->generator->valid()) { $this->nextCoroutine($next); } else { $this->result->resolve($value); } } catch (Exception $exception) { $this->result->reject($exception); } catch (Throwable $throwable) { $this->result->reject($throwable); } } /** * @internal */ public function _handleFailure($reason) { unset($this->currentPromise); try { $nextYield = $this->generator->throw(exception_for($reason)); // The throw was caught, so keep iterating on the coroutine $this->nextCoroutine($nextYield); } catch (Exception $exception) { $this->result->reject($exception); } catch (Throwable $throwable) { $this->result->reject($throwable); } } } <?php namespace GuzzleHttp\Promise; /** * A promise that has been fulfilled. * * Thenning off of this promise will invoke the onFulfilled callback * immediately and ignore other callbacks. */ class FulfilledPromise implements PromiseInterface { private $value; public function __construct($value) { if (method_exists($value, 'then')) { throw new \InvalidArgumentException( 'You cannot create a FulfilledPromise with a promise.'); } $this->value = $value; } public function then( callable $onFulfilled = null, callable $onRejected = null ) { // Return itself if there is no onFulfilled function. if (!$onFulfilled) { return $this; } $queue = queue(); $p = new Promise([$queue, 'run']); $value = $this->value; $queue->add(static function () use ($p, $value, $onFulfilled) { if ($p->getState() === self::PENDING) { try { $p->resolve($onFulfilled($value)); } catch (\Throwable $e) { $p->reject($e); } catch (\Exception $e) { $p->reject($e); } } }); return $p; } public function otherwise(callable $onRejected) { return $this->then(null, $onRejected); } public function wait($unwrap = true, $defaultDelivery = null) { return $unwrap ? $this->value : null; } public function getState() { return self::FULFILLED; } public function resolve($value) { if ($value !== $this->value) { throw new \LogicException("Cannot resolve a fulfilled promise"); } } public function reject($reason) { throw new \LogicException("Cannot reject a fulfilled promise"); } public function cancel() { // pass } } <?php namespace GuzzleHttp\Promise; /** * Exception thrown when too many errors occur in the some() or any() methods. */ class AggregateException extends RejectionException { public function __construct($msg, array $reasons) { parent::__construct( $reasons, sprintf('%s; %d rejected promises', $msg, count($reasons)) ); } } <?php namespace GuzzleHttp\Promise; /** * A promise represents the eventual result of an asynchronous operation. * * The primary way of interacting with a promise is through its then method, * which registers callbacks to receive either a promise’s eventual value or * the reason why the promise cannot be fulfilled. * * @link https://promisesaplus.com/ */ interface PromiseInterface { const PENDING = 'pending'; const FULFILLED = 'fulfilled'; const REJECTED = 'rejected'; /** * Appends fulfillment and rejection handlers to the promise, and returns * a new promise resolving to the return value of the called handler. * * @param callable $onFulfilled Invoked when the promise fulfills. * @param callable $onRejected Invoked when the promise is rejected. * * @return PromiseInterface */ public function then( callable $onFulfilled = null, callable $onRejected = null ); /** * Appends a rejection handler callback to the promise, and returns a new * promise resolving to the return value of the callback if it is called, * or to its original fulfillment value if the promise is instead * fulfilled. * * @param callable $onRejected Invoked when the promise is rejected. * * @return PromiseInterface */ public function otherwise(callable $onRejected); /** * Get the state of the promise ("pending", "rejected", or "fulfilled"). * * The three states can be checked against the constants defined on * PromiseInterface: PENDING, FULFILLED, and REJECTED. * * @return string */ public function getState(); /** * Resolve the promise with the given value. * * @param mixed $value * @throws \RuntimeException if the promise is already resolved. */ public function resolve($value); /** * Reject the promise with the given reason. * * @param mixed $reason * @throws \RuntimeException if the promise is already resolved. */ public function reject($reason); /** * Cancels the promise if possible. * * @link https://github.com/promises-aplus/cancellation-spec/issues/7 */ public function cancel(); /** * Waits until the promise completes if possible. * * Pass $unwrap as true to unwrap the result of the promise, either * returning the resolved value or throwing the rejected exception. * * If the promise cannot be waited on, then the promise will be rejected. * * @param bool $unwrap * * @return mixed * @throws \LogicException if the promise has no wait function or if the * promise does not settle after waiting. */ public function wait($unwrap = true); } <?php namespace GuzzleHttp\Promise; /** * Represents a promise that iterates over many promises and invokes * side-effect functions in the process. */ class EachPromise implements PromisorInterface { private $pending = []; /** @var \Iterator */ private $iterable; /** @var callable|int */ private $concurrency; /** @var callable */ private $onFulfilled; /** @var callable */ private $onRejected; /** @var Promise */ private $aggregate; /** @var bool */ private $mutex; /** * Configuration hash can include the following key value pairs: * * - fulfilled: (callable) Invoked when a promise fulfills. The function * is invoked with three arguments: the fulfillment value, the index * position from the iterable list of the promise, and the aggregate * promise that manages all of the promises. The aggregate promise may * be resolved from within the callback to short-circuit the promise. * - rejected: (callable) Invoked when a promise is rejected. The * function is invoked with three arguments: the rejection reason, the * index position from the iterable list of the promise, and the * aggregate promise that manages all of the promises. The aggregate * promise may be resolved from within the callback to short-circuit * the promise. * - concurrency: (integer) Pass this configuration option to limit the * allowed number of outstanding concurrently executing promises, * creating a capped pool of promises. There is no limit by default. * * @param mixed $iterable Promises or values to iterate. * @param array $config Configuration options */ public function __construct($iterable, array $config = []) { $this->iterable = iter_for($iterable); if (isset($config['concurrency'])) { $this->concurrency = $config['concurrency']; } if (isset($config['fulfilled'])) { $this->onFulfilled = $config['fulfilled']; } if (isset($config['rejected'])) { $this->onRejected = $config['rejected']; } } public function promise() { if ($this->aggregate) { return $this->aggregate; } try { $this->createPromise(); $this->iterable->rewind(); $this->refillPending(); } catch (\Throwable $e) { $this->aggregate->reject($e); } catch (\Exception $e) { $this->aggregate->reject($e); } return $this->aggregate; } private function createPromise() { $this->mutex = false; $this->aggregate = new Promise(function () { reset($this->pending); if (empty($this->pending) && !$this->iterable->valid()) { $this->aggregate->resolve(null); return; } // Consume a potentially fluctuating list of promises while // ensuring that indexes are maintained (precluding array_shift). while ($promise = current($this->pending)) { next($this->pending); $promise->wait(); if ($this->aggregate->getState() !== PromiseInterface::PENDING) { return; } } }); // Clear the references when the promise is resolved. $clearFn = function () { $this->iterable = $this->concurrency = $this->pending = null; $this->onFulfilled = $this->onRejected = null; }; $this->aggregate->then($clearFn, $clearFn); } private function refillPending() { if (!$this->concurrency) { // Add all pending promises. while ($this->addPending() && $this->advanceIterator()); return; } // Add only up to N pending promises. $concurrency = is_callable($this->concurrency) ? call_user_func($this->concurrency, count($this->pending)) : $this->concurrency; $concurrency = max($concurrency - count($this->pending), 0); // Concurrency may be set to 0 to disallow new promises. if (!$concurrency) { return; } // Add the first pending promise. $this->addPending(); // Note this is special handling for concurrency=1 so that we do // not advance the iterator after adding the first promise. This // helps work around issues with generators that might not have the // next value to yield until promise callbacks are called. while (--$concurrency && $this->advanceIterator() && $this->addPending()); } private function addPending() { if (!$this->iterable || !$this->iterable->valid()) { return false; } $promise = promise_for($this->iterable->current()); $idx = $this->iterable->key(); $this->pending[$idx] = $promise->then( function ($value) use ($idx) { if ($this->onFulfilled) { call_user_func( $this->onFulfilled, $value, $idx, $this->aggregate ); } $this->step($idx); }, function ($reason) use ($idx) { if ($this->onRejected) { call_user_func( $this->onRejected, $reason, $idx, $this->aggregate ); } $this->step($idx); } ); return true; } private function advanceIterator() { // Place a lock on the iterator so that we ensure to not recurse, // preventing fatal generator errors. if ($this->mutex) { return false; } $this->mutex = true; try { $this->iterable->next(); $this->mutex = false; return true; } catch (\Throwable $e) { $this->aggregate->reject($e); $this->mutex = false; return false; } catch (\Exception $e) { $this->aggregate->reject($e); $this->mutex = false; return false; } } private function step($idx) { // If the promise was already resolved, then ignore this step. if ($this->aggregate->getState() !== PromiseInterface::PENDING) { return; } unset($this->pending[$idx]); // Only refill pending promises if we are not locked, preventing the // EachPromise to recursively invoke the provided iterator, which // cause a fatal error: "Cannot resume an already running generator" if ($this->advanceIterator() && !$this->checkIfFinished()) { // Add more pending promises if possible. $this->refillPending(); } } private function checkIfFinished() { if (!$this->pending && !$this->iterable->valid()) { // Resolve the promise if there's nothing left to do. $this->aggregate->resolve(null); return true; } return false; } } <?php namespace GuzzleHttp\Promise; /** * Interface used with classes that return a promise. */ interface PromisorInterface { /** * Returns a promise. * * @return PromiseInterface */ public function promise(); } <?php namespace GuzzleHttp\Promise; /** * Promises/A+ implementation that avoids recursion when possible. * * @link https://promisesaplus.com/ */ class Promise implements PromiseInterface { private $state = self::PENDING; private $result; private $cancelFn; private $waitFn; private $waitList; private $handlers = []; /** * @param callable $waitFn Fn that when invoked resolves the promise. * @param callable $cancelFn Fn that when invoked cancels the promise. */ public function __construct( callable $waitFn = null, callable $cancelFn = null ) { $this->waitFn = $waitFn; $this->cancelFn = $cancelFn; } public function then( callable $onFulfilled = null, callable $onRejected = null ) { if ($this->state === self::PENDING) { $p = new Promise(null, [$this, 'cancel']); $this->handlers[] = [$p, $onFulfilled, $onRejected]; $p->waitList = $this->waitList; $p->waitList[] = $this; return $p; } // Return a fulfilled promise and immediately invoke any callbacks. if ($this->state === self::FULFILLED) { return $onFulfilled ? promise_for($this->result)->then($onFulfilled) : promise_for($this->result); } // It's either cancelled or rejected, so return a rejected promise // and immediately invoke any callbacks. $rejection = rejection_for($this->result); return $onRejected ? $rejection->then(null, $onRejected) : $rejection; } public function otherwise(callable $onRejected) { return $this->then(null, $onRejected); } public function wait($unwrap = true) { $this->waitIfPending(); $inner = $this->result instanceof PromiseInterface ? $this->result->wait($unwrap) : $this->result; if ($unwrap) { if ($this->result instanceof PromiseInterface || $this->state === self::FULFILLED ) { return $inner; } else { // It's rejected so "unwrap" and throw an exception. throw exception_for($inner); } } } public function getState() { return $this->state; } public function cancel() { if ($this->state !== self::PENDING) { return; } $this->waitFn = $this->waitList = null; if ($this->cancelFn) { $fn = $this->cancelFn; $this->cancelFn = null; try { $fn(); } catch (\Throwable $e) { $this->reject($e); } catch (\Exception $e) { $this->reject($e); } } // Reject the promise only if it wasn't rejected in a then callback. if ($this->state === self::PENDING) { $this->reject(new CancellationException('Promise has been cancelled')); } } public function resolve($value) { $this->settle(self::FULFILLED, $value); } public function reject($reason) { $this->settle(self::REJECTED, $reason); } private function settle($state, $value) { if ($this->state !== self::PENDING) { // Ignore calls with the same resolution. if ($state === $this->state && $value === $this->result) { return; } throw $this->state === $state ? new \LogicException("The promise is already {$state}.") : new \LogicException("Cannot change a {$this->state} promise to {$state}"); } if ($value === $this) { throw new \LogicException('Cannot fulfill or reject a promise with itself'); } // Clear out the state of the promise but stash the handlers. $this->state = $state; $this->result = $value; $handlers = $this->handlers; $this->handlers = null; $this->waitList = $this->waitFn = null; $this->cancelFn = null; if (!$handlers) { return; } // If the value was not a settled promise or a thenable, then resolve // it in the task queue using the correct ID. if (!method_exists($value, 'then')) { $id = $state === self::FULFILLED ? 1 : 2; // It's a success, so resolve the handlers in the queue. queue()->add(static function () use ($id, $value, $handlers) { foreach ($handlers as $handler) { self::callHandler($id, $value, $handler); } }); } elseif ($value instanceof Promise && $value->getState() === self::PENDING ) { // We can just merge our handlers onto the next promise. $value->handlers = array_merge($value->handlers, $handlers); } else { // Resolve the handlers when the forwarded promise is resolved. $value->then( static function ($value) use ($handlers) { foreach ($handlers as $handler) { self::callHandler(1, $value, $handler); } }, static function ($reason) use ($handlers) { foreach ($handlers as $handler) { self::callHandler(2, $reason, $handler); } } ); } } /** * Call a stack of handlers using a specific callback index and value. * * @param int $index 1 (resolve) or 2 (reject). * @param mixed $value Value to pass to the callback. * @param array $handler Array of handler data (promise and callbacks). * * @return array Returns the next group to resolve. */ private static function callHandler($index, $value, array $handler) { /** @var PromiseInterface $promise */ $promise = $handler[0]; // The promise may have been cancelled or resolved before placing // this thunk in the queue. if ($promise->getState() !== self::PENDING) { return; } try { if (isset($handler[$index])) { $promise->resolve($handler[$index]($value)); } elseif ($index === 1) { // Forward resolution values as-is. $promise->resolve($value); } else { // Forward rejections down the chain. $promise->reject($value); } } catch (\Throwable $reason) { $promise->reject($reason); } catch (\Exception $reason) { $promise->reject($reason); } } private function waitIfPending() { if ($this->state !== self::PENDING) { return; } elseif ($this->waitFn) { $this->invokeWaitFn(); } elseif ($this->waitList) { $this->invokeWaitList(); } else { // If there's not wait function, then reject the promise. $this->reject('Cannot wait on a promise that has ' . 'no internal wait function. You must provide a wait ' . 'function when constructing the promise to be able to ' . 'wait on a promise.'); } queue()->run(); if ($this->state === self::PENDING) { $this->reject('Invoking the wait callback did not resolve the promise'); } } private function invokeWaitFn() { try { $wfn = $this->waitFn; $this->waitFn = null; $wfn(true); } catch (\Exception $reason) { if ($this->state === self::PENDING) { // The promise has not been resolved yet, so reject the promise // with the exception. $this->reject($reason); } else { // The promise was already resolved, so there's a problem in // the application. throw $reason; } } } private function invokeWaitList() { $waitList = $this->waitList; $this->waitList = null; foreach ($waitList as $result) { while (true) { $result->waitIfPending(); if ($result->result instanceof Promise) { $result = $result->result; } else { if ($result->result instanceof PromiseInterface) { $result->result->wait(false); } break; } } } } } <?php namespace GuzzleHttp\Promise; /** * A special exception that is thrown when waiting on a rejected promise. * * The reason value is available via the getReason() method. */ class RejectionException extends \RuntimeException { /** @var mixed Rejection reason. */ private $reason; /** * @param mixed $reason Rejection reason. * @param string $description Optional description */ public function __construct($reason, $description = null) { $this->reason = $reason; $message = 'The promise was rejected'; if ($description) { $message .= ' with reason: ' . $description; } elseif (is_string($reason) || (is_object($reason) && method_exists($reason, '__toString')) ) { $message .= ' with reason: ' . $this->reason; } elseif ($reason instanceof \JsonSerializable) { $message .= ' with reason: ' . json_encode($this->reason, JSON_PRETTY_PRINT); } parent::__construct($message); } /** * Returns the rejection reason. * * @return mixed */ public function getReason() { return $this->reason; } } <?php namespace GuzzleHttp\Promise; /** * A promise that has been rejected. * * Thenning off of this promise will invoke the onRejected callback * immediately and ignore other callbacks. */ class RejectedPromise implements PromiseInterface { private $reason; public function __construct($reason) { if (method_exists($reason, 'then')) { throw new \InvalidArgumentException( 'You cannot create a RejectedPromise with a promise.'); } $this->reason = $reason; } public function then( callable $onFulfilled = null, callable $onRejected = null ) { // If there's no onRejected callback then just return self. if (!$onRejected) { return $this; } $queue = queue(); $reason = $this->reason; $p = new Promise([$queue, 'run']); $queue->add(static function () use ($p, $reason, $onRejected) { if ($p->getState() === self::PENDING) { try { // Return a resolved promise if onRejected does not throw. $p->resolve($onRejected($reason)); } catch (\Throwable $e) { // onRejected threw, so return a rejected promise. $p->reject($e); } catch (\Exception $e) { // onRejected threw, so return a rejected promise. $p->reject($e); } } }); return $p; } public function otherwise(callable $onRejected) { return $this->then(null, $onRejected); } public function wait($unwrap = true, $defaultDelivery = null) { if ($unwrap) { throw exception_for($this->reason); } } public function getState() { return self::REJECTED; } public function resolve($value) { throw new \LogicException("Cannot resolve a rejected promise"); } public function reject($reason) { if ($reason !== $this->reason) { throw new \LogicException("Cannot reject a rejected promise"); } } public function cancel() { // pass } } <?php namespace GuzzleHttp\Promise; interface TaskQueueInterface { /** * Returns true if the queue is empty. * * @return bool */ public function isEmpty(); /** * Adds a task to the queue that will be executed the next time run is * called. * * @param callable $task */ public function add(callable $task); /** * Execute all of the pending task in the queue. */ public function run(); } <?php namespace GuzzleHttp\Promise; /** * A task queue that executes tasks in a FIFO order. * * This task queue class is used to settle promises asynchronously and * maintains a constant stack size. You can use the task queue asynchronously * by calling the `run()` function of the global task queue in an event loop. * * GuzzleHttp\Promise\queue()->run(); */ class TaskQueue implements TaskQueueInterface { private $enableShutdown = true; private $queue = []; public function __construct($withShutdown = true) { if ($withShutdown) { register_shutdown_function(function () { if ($this->enableShutdown) { // Only run the tasks if an E_ERROR didn't occur. $err = error_get_last(); if (!$err || ($err['type'] ^ E_ERROR)) { $this->run(); } } }); } } public function isEmpty() { return !$this->queue; } public function add(callable $task) { $this->queue[] = $task; } public function run() { /** @var callable $task */ while ($task = array_shift($this->queue)) { $task(); } } /** * The task queue will be run and exhausted by default when the process * exits IFF the exit is not the result of a PHP E_ERROR error. * * You can disable running the automatic shutdown of the queue by calling * this function. If you disable the task queue shutdown process, then you * MUST either run the task queue (as a result of running your event loop * or manually using the run() method) or wait on each outstanding promise. * * Note: This shutdown will occur before any destructors are triggered. */ public function disableShutdown() { $this->enableShutdown = false; } } # CHANGELOG ## 1.3.1 - 2016-12-20 ### Fixed - `wait()` foreign promise compatibility ## 1.3.0 - 2016-11-18 ### Added - Adds support for custom task queues. ### Fixed - Fixed coroutine promise memory leak. ## 1.2.0 - 2016-05-18 ### Changed - Update to now catch `\Throwable` on PHP 7+ ## 1.1.0 - 2016-03-07 ### Changed - Update EachPromise to prevent recurring on a iterator when advancing, as this could trigger fatal generator errors. - Update Promise to allow recursive waiting without unwrapping exceptions. ## 1.0.3 - 2015-10-15 ### Changed - Update EachPromise to immediately resolve when the underlying promise iterator is empty. Previously, such a promise would throw an exception when its `wait` function was called. ## 1.0.2 - 2015-05-15 ### Changed - Conditionally require functions.php. ## 1.0.1 - 2015-06-24 ### Changed - Updating EachPromise to call next on the underlying promise iterator as late as possible to ensure that generators that generate new requests based on callbacks are not iterated until after callbacks are invoked. ## 1.0.0 - 2015-05-12 - Initial release { "name": "guzzlehttp/promises", "description": "Guzzle promises library", "keywords": ["promise"], "license": "MIT", "authors": [ { "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" } ], "require": { "php": ">=5.5.0" }, "require-dev": { "phpunit/phpunit": "^4.0" }, "autoload": { "psr-4": { "GuzzleHttp\\Promise\\": "src/" }, "files": ["src/functions_include.php"] }, "scripts": { "test": "vendor/bin/phpunit", "test-ci": "vendor/bin/phpunit --coverage-text" }, "extra": { "branch-alias": { "dev-master": "1.4-dev" } } } # Guzzle Promises [Promises/A+](https://promisesaplus.com/) implementation that handles promise chaining and resolution iteratively, allowing for "infinite" promise chaining while keeping the stack size constant. Read [this blog post](https://blog.domenic.me/youre-missing-the-point-of-promises/) for a general introduction to promises. - [Features](#features) - [Quick start](#quick-start) - [Synchronous wait](#synchronous-wait) - [Cancellation](#cancellation) - [API](#api) - [Promise](#promise) - [FulfilledPromise](#fulfilledpromise) - [RejectedPromise](#rejectedpromise) - [Promise interop](#promise-interop) - [Implementation notes](#implementation-notes) # Features - [Promises/A+](https://promisesaplus.com/) implementation. - Promise resolution and chaining is handled iteratively, allowing for "infinite" promise chaining. - Promises have a synchronous `wait` method. - Promises can be cancelled. - Works with any object that has a `then` function. - C# style async/await coroutine promises using `GuzzleHttp\Promise\coroutine()`. # Quick start A *promise* represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its `then` method, which registers callbacks to receive either a promise's eventual value or the reason why the promise cannot be fulfilled. ## Callbacks Callbacks are registered with the `then` method by providing an optional `$onFulfilled` followed by an optional `$onRejected` function. ```php use GuzzleHttp\Promise\Promise; $promise = new Promise(); $promise->then( // $onFulfilled function ($value) { echo 'The promise was fulfilled.'; }, // $onRejected function ($reason) { echo 'The promise was rejected.'; } ); ``` *Resolving* a promise means that you either fulfill a promise with a *value* or reject a promise with a *reason*. Resolving a promises triggers callbacks registered with the promises's `then` method. These callbacks are triggered only once and in the order in which they were added. ## Resolving a promise Promises are fulfilled using the `resolve($value)` method. Resolving a promise with any value other than a `GuzzleHttp\Promise\RejectedPromise` will trigger all of the onFulfilled callbacks (resolving a promise with a rejected promise will reject the promise and trigger the `$onRejected` callbacks). ```php use GuzzleHttp\Promise\Promise; $promise = new Promise(); $promise ->then(function ($value) { // Return a value and don't break the chain return "Hello, " . $value; }) // This then is executed after the first then and receives the value // returned from the first then. ->then(function ($value) { echo $value; }); // Resolving the promise triggers the $onFulfilled callbacks and outputs // "Hello, reader". $promise->resolve('reader.'); ``` ## Promise forwarding Promises can be chained one after the other. Each then in the chain is a new promise. The return value of a promise is what's forwarded to the next promise in the chain. Returning a promise in a `then` callback will cause the subsequent promises in the chain to only be fulfilled when the returned promise has been fulfilled. The next promise in the chain will be invoked with the resolved value of the promise. ```php use GuzzleHttp\Promise\Promise; $promise = new Promise(); $nextPromise = new Promise(); $promise ->then(function ($value) use ($nextPromise) { echo $value; return $nextPromise; }) ->then(function ($value) { echo $value; }); // Triggers the first callback and outputs "A" $promise->resolve('A'); // Triggers the second callback and outputs "B" $nextPromise->resolve('B'); ``` ## Promise rejection When a promise is rejected, the `$onRejected` callbacks are invoked with the rejection reason. ```php use GuzzleHttp\Promise\Promise; $promise = new Promise(); $promise->then(null, function ($reason) { echo $reason; }); $promise->reject('Error!'); // Outputs "Error!" ``` ## Rejection forwarding If an exception is thrown in an `$onRejected` callback, subsequent `$onRejected` callbacks are invoked with the thrown exception as the reason. ```php use GuzzleHttp\Promise\Promise; $promise = new Promise(); $promise->then(null, function ($reason) { throw new \Exception($reason); })->then(null, function ($reason) { assert($reason->getMessage() === 'Error!'); }); $promise->reject('Error!'); ``` You can also forward a rejection down the promise chain by returning a `GuzzleHttp\Promise\RejectedPromise` in either an `$onFulfilled` or `$onRejected` callback. ```php use GuzzleHttp\Promise\Promise; use GuzzleHttp\Promise\RejectedPromise; $promise = new Promise(); $promise->then(null, function ($reason) { return new RejectedPromise($reason); })->then(null, function ($reason) { assert($reason === 'Error!'); }); $promise->reject('Error!'); ``` If an exception is not thrown in a `$onRejected` callback and the callback does not return a rejected promise, downstream `$onFulfilled` callbacks are invoked using the value returned from the `$onRejected` callback. ```php use GuzzleHttp\Promise\Promise; use GuzzleHttp\Promise\RejectedPromise; $promise = new Promise(); $promise ->then(null, function ($reason) { return "It's ok"; }) ->then(function ($value) { assert($value === "It's ok"); }); $promise->reject('Error!'); ``` # Synchronous wait You can synchronously force promises to complete using a promise's `wait` method. When creating a promise, you can provide a wait function that is used to synchronously force a promise to complete. When a wait function is invoked it is expected to deliver a value to the promise or reject the promise. If the wait function does not deliver a value, then an exception is thrown. The wait function provided to a promise constructor is invoked when the `wait` function of the promise is called. ```php $promise = new Promise(function () use (&$promise) { $promise->resolve('foo'); }); // Calling wait will return the value of the promise. echo $promise->wait(); // outputs "foo" ``` If an exception is encountered while invoking the wait function of a promise, the promise is rejected with the exception and the exception is thrown. ```php $promise = new Promise(function () use (&$promise) { throw new \Exception('foo'); }); $promise->wait(); // throws the exception. ``` Calling `wait` on a promise that has been fulfilled will not trigger the wait function. It will simply return the previously resolved value. ```php $promise = new Promise(function () { die('this is not called!'); }); $promise->resolve('foo'); echo $promise->wait(); // outputs "foo" ``` Calling `wait` on a promise that has been rejected will throw an exception. If the rejection reason is an instance of `\Exception` the reason is thrown. Otherwise, a `GuzzleHttp\Promise\RejectionException` is thrown and the reason can be obtained by calling the `getReason` method of the exception. ```php $promise = new Promise(); $promise->reject('foo'); $promise->wait(); ``` > PHP Fatal error: Uncaught exception 'GuzzleHttp\Promise\RejectionException' with message 'The promise was rejected with value: foo' ## Unwrapping a promise When synchronously waiting on a promise, you are joining the state of the promise into the current state of execution (i.e., return the value of the promise if it was fulfilled or throw an exception if it was rejected). This is called "unwrapping" the promise. Waiting on a promise will by default unwrap the promise state. You can force a promise to resolve and *not* unwrap the state of the promise by passing `false` to the first argument of the `wait` function: ```php $promise = new Promise(); $promise->reject('foo'); // This will not throw an exception. It simply ensures the promise has // been resolved. $promise->wait(false); ``` When unwrapping a promise, the resolved value of the promise will be waited upon until the unwrapped value is not a promise. This means that if you resolve promise A with a promise B and unwrap promise A, the value returned by the wait function will be the value delivered to promise B. **Note**: when you do not unwrap the promise, no value is returned. # Cancellation You can cancel a promise that has not yet been fulfilled using the `cancel()` method of a promise. When creating a promise you can provide an optional cancel function that when invoked cancels the action of computing a resolution of the promise. # API ## Promise When creating a promise object, you can provide an optional `$waitFn` and `$cancelFn`. `$waitFn` is a function that is invoked with no arguments and is expected to resolve the promise. `$cancelFn` is a function with no arguments that is expected to cancel the computation of a promise. It is invoked when the `cancel()` method of a promise is called. ```php use GuzzleHttp\Promise\Promise; $promise = new Promise( function () use (&$promise) { $promise->resolve('waited'); }, function () { // do something that will cancel the promise computation (e.g., close // a socket, cancel a database query, etc...) } ); assert('waited' === $promise->wait()); ``` A promise has the following methods: - `then(callable $onFulfilled, callable $onRejected) : PromiseInterface` Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler. - `otherwise(callable $onRejected) : PromiseInterface` Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled. - `wait($unwrap = true) : mixed` Synchronously waits on the promise to complete. `$unwrap` controls whether or not the value of the promise is returned for a fulfilled promise or if an exception is thrown if the promise is rejected. This is set to `true` by default. - `cancel()` Attempts to cancel the promise if possible. The promise being cancelled and the parent most ancestor that has not yet been resolved will also be cancelled. Any promises waiting on the cancelled promise to resolve will also be cancelled. - `getState() : string` Returns the state of the promise. One of `pending`, `fulfilled`, or `rejected`. - `resolve($value)` Fulfills the promise with the given `$value`. - `reject($reason)` Rejects the promise with the given `$reason`. ## FulfilledPromise A fulfilled promise can be created to represent a promise that has been fulfilled. ```php use GuzzleHttp\Promise\FulfilledPromise; $promise = new FulfilledPromise('value'); // Fulfilled callbacks are immediately invoked. $promise->then(function ($value) { echo $value; }); ``` ## RejectedPromise A rejected promise can be created to represent a promise that has been rejected. ```php use GuzzleHttp\Promise\RejectedPromise; $promise = new RejectedPromise('Error'); // Rejected callbacks are immediately invoked. $promise->then(null, function ($reason) { echo $reason; }); ``` # Promise interop This library works with foreign promises that have a `then` method. This means you can use Guzzle promises with [React promises](https://github.com/reactphp/promise) for example. When a foreign promise is returned inside of a then method callback, promise resolution will occur recursively. ```php // Create a React promise $deferred = new React\Promise\Deferred(); $reactPromise = $deferred->promise(); // Create a Guzzle promise that is fulfilled with a React promise. $guzzlePromise = new \GuzzleHttp\Promise\Promise(); $guzzlePromise->then(function ($value) use ($reactPromise) { // Do something something with the value... // Return the React promise return $reactPromise; }); ``` Please note that wait and cancel chaining is no longer possible when forwarding a foreign promise. You will need to wrap a third-party promise with a Guzzle promise in order to utilize wait and cancel functions with foreign promises. ## Event Loop Integration In order to keep the stack size constant, Guzzle promises are resolved asynchronously using a task queue. When waiting on promises synchronously, the task queue will be automatically run to ensure that the blocking promise and any forwarded promises are resolved. When using promises asynchronously in an event loop, you will need to run the task queue on each tick of the loop. If you do not run the task queue, then promises will not be resolved. You can run the task queue using the `run()` method of the global task queue instance. ```php // Get the global task queue $queue = \GuzzleHttp\Promise\queue(); $queue->run(); ``` For example, you could use Guzzle promises with React using a periodic timer: ```php $loop = React\EventLoop\Factory::create(); $loop->addPeriodicTimer(0, [$queue, 'run']); ``` *TODO*: Perhaps adding a `futureTick()` on each tick would be faster? # Implementation notes ## Promise resolution and chaining is handled iteratively By shuffling pending handlers from one owner to another, promises are resolved iteratively, allowing for "infinite" then chaining. ```php <?php require 'vendor/autoload.php'; use GuzzleHttp\Promise\Promise; $parent = new Promise(); $p = $parent; for ($i = 0; $i < 1000; $i++) { $p = $p->then(function ($v) { // The stack size remains constant (a good thing) echo xdebug_get_stack_depth() . ', '; return $v + 1; }); } $parent->resolve(0); var_dump($p->wait()); // int(1000) ``` When a promise is fulfilled or rejected with a non-promise value, the promise then takes ownership of the handlers of each child promise and delivers values down the chain without using recursion. When a promise is resolved with another promise, the original promise transfers all of its pending handlers to the new promise. When the new promise is eventually resolved, all of the pending handlers are delivered the forwarded value. ## A promise is the deferred. Some promise libraries implement promises using a deferred object to represent a computation and a promise object to represent the delivery of the result of the computation. This is a nice separation of computation and delivery because consumers of the promise cannot modify the value that will be eventually delivered. One side effect of being able to implement promise resolution and chaining iteratively is that you need to be able for one promise to reach into the state of another promise to shuffle around ownership of handlers. In order to achieve this without making the handlers of a promise publicly mutable, a promise is also the deferred value, allowing promises of the same parent class to reach into and modify the private properties of promises of the same type. While this does allow consumers of the value to modify the resolution or rejection of the deferred, it is a small price to pay for keeping the stack size constant. ```php $promise = new Promise(); $promise->then(function ($value) { echo $value; }); // The promise is the deferred value, so you can deliver a value to it. $promise->resolve('foo'); // prints "foo" ``` all: clean test test: vendor/bin/phpunit coverage: vendor/bin/phpunit --coverage-html=artifacts/coverage view-coverage: open artifacts/coverage/index.html clean: rm -rf artifacts/* <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Lazily reads or writes to a file that is opened only after an IO operation * take place on the stream. */ class LazyOpenStream implements StreamInterface { use StreamDecoratorTrait; /** @var string File to open */ private $filename; /** @var string $mode */ private $mode; /** * @param string $filename File to lazily open * @param string $mode fopen mode to use when opening the stream */ public function __construct($filename, $mode) { $this->filename = $filename; $this->mode = $mode; } /** * Creates the underlying stream lazily when required. * * @return StreamInterface */ protected function createStream() { return stream_for(try_fopen($this->filename, $this->mode)); } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\UriInterface; /** * Resolves a URI reference in the context of a base URI and the opposite way. * * @author Tobias Schultze * * @link https://tools.ietf.org/html/rfc3986#section-5 */ final class UriResolver { /** * Removes dot segments from a path and returns the new path. * * @param string $path * * @return string * @link http://tools.ietf.org/html/rfc3986#section-5.2.4 */ public static function removeDotSegments($path) { if ($path === '' || $path === '/') { return $path; } $results = []; $segments = explode('/', $path); foreach ($segments as $segment) { if ($segment === '..') { array_pop($results); } elseif ($segment !== '.') { $results[] = $segment; } } $newPath = implode('/', $results); if ($path[0] === '/' && (!isset($newPath[0]) || $newPath[0] !== '/')) { // Re-add the leading slash if necessary for cases like "/.." $newPath = '/' . $newPath; } elseif ($newPath !== '' && ($segment === '.' || $segment === '..')) { // Add the trailing slash if necessary // If newPath is not empty, then $segment must be set and is the last segment from the foreach $newPath .= '/'; } return $newPath; } /** * Converts the relative URI into a new URI that is resolved against the base URI. * * @param UriInterface $base Base URI * @param UriInterface $rel Relative URI * * @return UriInterface * @link http://tools.ietf.org/html/rfc3986#section-5.2 */ public static function resolve(UriInterface $base, UriInterface $rel) { if ((string) $rel === '') { // we can simply return the same base URI instance for this same-document reference return $base; } if ($rel->getScheme() != '') { return $rel->withPath(self::removeDotSegments($rel->getPath())); } if ($rel->getAuthority() != '') { $targetAuthority = $rel->getAuthority(); $targetPath = self::removeDotSegments($rel->getPath()); $targetQuery = $rel->getQuery(); } else { $targetAuthority = $base->getAuthority(); if ($rel->getPath() === '') { $targetPath = $base->getPath(); $targetQuery = $rel->getQuery() != '' ? $rel->getQuery() : $base->getQuery(); } else { if ($rel->getPath()[0] === '/') { $targetPath = $rel->getPath(); } else { if ($targetAuthority != '' && $base->getPath() === '') { $targetPath = '/' . $rel->getPath(); } else { $lastSlashPos = strrpos($base->getPath(), '/'); if ($lastSlashPos === false) { $targetPath = $rel->getPath(); } else { $targetPath = substr($base->getPath(), 0, $lastSlashPos + 1) . $rel->getPath(); } } } $targetPath = self::removeDotSegments($targetPath); $targetQuery = $rel->getQuery(); } } return new Uri(Uri::composeComponents( $base->getScheme(), $targetAuthority, $targetPath, $targetQuery, $rel->getFragment() )); } /** * Returns the target URI as a relative reference from the base URI. * * This method is the counterpart to resolve(): * * (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target)) * * One use-case is to use the current request URI as base URI and then generate relative links in your documents * to reduce the document size or offer self-contained downloadable document archives. * * $base = new Uri('http://example.com/a/b/'); * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'. * echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'. * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'. * echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'. * * This method also accepts a target that is already relative and will try to relativize it further. Only a * relative-path reference will be returned as-is. * * echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well * * @param UriInterface $base Base URI * @param UriInterface $target Target URI * * @return UriInterface The relative URI reference */ public static function relativize(UriInterface $base, UriInterface $target) { if ($target->getScheme() !== '' && ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '') ) { return $target; } if (Uri::isRelativePathReference($target)) { // As the target is already highly relative we return it as-is. It would be possible to resolve // the target with `$target = self::resolve($base, $target);` and then try make it more relative // by removing a duplicate query. But let's not do that automatically. return $target; } if ($target->getAuthority() !== '' && $base->getAuthority() !== $target->getAuthority()) { return $target->withScheme(''); } // We must remove the path before removing the authority because if the path starts with two slashes, the URI // would turn invalid. And we also cannot set a relative path before removing the authority, as that is also // invalid. $emptyPathUri = $target->withScheme('')->withPath('')->withUserInfo('')->withPort(null)->withHost(''); if ($base->getPath() !== $target->getPath()) { return $emptyPathUri->withPath(self::getRelativePath($base, $target)); } if ($base->getQuery() === $target->getQuery()) { // Only the target fragment is left. And it must be returned even if base and target fragment are the same. return $emptyPathUri->withQuery(''); } // If the base URI has a query but the target has none, we cannot return an empty path reference as it would // inherit the base query component when resolving. if ($target->getQuery() === '') { $segments = explode('/', $target->getPath()); $lastSegment = end($segments); return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment); } return $emptyPathUri; } private static function getRelativePath(UriInterface $base, UriInterface $target) { $sourceSegments = explode('/', $base->getPath()); $targetSegments = explode('/', $target->getPath()); array_pop($sourceSegments); $targetLastSegment = array_pop($targetSegments); foreach ($sourceSegments as $i => $segment) { if (isset($targetSegments[$i]) && $segment === $targetSegments[$i]) { unset($sourceSegments[$i], $targetSegments[$i]); } else { break; } } $targetSegments[] = $targetLastSegment; $relativePath = str_repeat('../', count($sourceSegments)) . implode('/', $targetSegments); // A reference to am empty last segment or an empty first sub-segment must be prefixed with "./". // This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used // as the first segment of a relative-path reference, as it would be mistaken for a scheme name. if ('' === $relativePath || false !== strpos(explode('/', $relativePath, 2)[0], ':')) { $relativePath = "./$relativePath"; } elseif ('/' === $relativePath[0]) { if ($base->getAuthority() != '' && $base->getPath() === '') { // In this case an extra slash is added by resolve() automatically. So we must not add one here. $relativePath = ".$relativePath"; } else { $relativePath = "./$relativePath"; } } return $relativePath; } private function __construct() { // cannot be instantiated } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Provides a buffer stream that can be written to to fill a buffer, and read * from to remove bytes from the buffer. * * This stream returns a "hwm" metadata value that tells upstream consumers * what the configured high water mark of the stream is, or the maximum * preferred size of the buffer. */ class BufferStream implements StreamInterface { private $hwm; private $buffer = ''; /** * @param int $hwm High water mark, representing the preferred maximum * buffer size. If the size of the buffer exceeds the high * water mark, then calls to write will continue to succeed * but will return false to inform writers to slow down * until the buffer has been drained by reading from it. */ public function __construct($hwm = 16384) { $this->hwm = $hwm; } public function __toString() { return $this->getContents(); } public function getContents() { $buffer = $this->buffer; $this->buffer = ''; return $buffer; } public function close() { $this->buffer = ''; } public function detach() { $this->close(); } public function getSize() { return strlen($this->buffer); } public function isReadable() { return true; } public function isWritable() { return true; } public function isSeekable() { return false; } public function rewind() { $this->seek(0); } public function seek($offset, $whence = SEEK_SET) { throw new \RuntimeException('Cannot seek a BufferStream'); } public function eof() { return strlen($this->buffer) === 0; } public function tell() { throw new \RuntimeException('Cannot determine the position of a BufferStream'); } /** * Reads data from the buffer. */ public function read($length) { $currentLength = strlen($this->buffer); if ($length >= $currentLength) { // No need to slice the buffer because we don't have enough data. $result = $this->buffer; $this->buffer = ''; } else { // Slice up the result to provide a subset of the buffer. $result = substr($this->buffer, 0, $length); $this->buffer = substr($this->buffer, $length); } return $result; } /** * Writes data to the buffer. */ public function write($string) { $this->buffer .= $string; // TODO: What should happen here? if (strlen($this->buffer) >= $this->hwm) { return false; } return strlen($string); } public function getMetadata($key = null) { if ($key == 'hwm') { return $this->hwm; } return $key ? null : []; } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * PHP stream implementation. * * @var $stream */ class Stream implements StreamInterface { /** * Resource modes. * * @var string * * @see http://php.net/manual/function.fopen.php * @see http://php.net/manual/en/function.gzopen.php */ const READABLE_MODES = '/r|a\+|ab\+|w\+|wb\+|x\+|xb\+|c\+|cb\+/'; const WRITABLE_MODES = '/a|w|r\+|rb\+|rw|x|c/'; private $stream; private $size; private $seekable; private $readable; private $writable; private $uri; private $customMetadata; /** * This constructor accepts an associative array of options. * * - size: (int) If a read stream would otherwise have an indeterminate * size, but the size is known due to foreknowledge, then you can * provide that size, in bytes. * - metadata: (array) Any additional metadata to return when the metadata * of the stream is accessed. * * @param resource $stream Stream resource to wrap. * @param array $options Associative array of options. * * @throws \InvalidArgumentException if the stream is not a stream resource */ public function __construct($stream, $options = []) { if (!is_resource($stream)) { throw new \InvalidArgumentException('Stream must be a resource'); } if (isset($options['size'])) { $this->size = $options['size']; } $this->customMetadata = isset($options['metadata']) ? $options['metadata'] : []; $this->stream = $stream; $meta = stream_get_meta_data($this->stream); $this->seekable = $meta['seekable']; $this->readable = (bool)preg_match(self::READABLE_MODES, $meta['mode']); $this->writable = (bool)preg_match(self::WRITABLE_MODES, $meta['mode']); $this->uri = $this->getMetadata('uri'); } /** * Closes the stream when the destructed */ public function __destruct() { $this->close(); } public function __toString() { try { $this->seek(0); return (string) stream_get_contents($this->stream); } catch (\Exception $e) { return ''; } } public function getContents() { if (!isset($this->stream)) { throw new \RuntimeException('Stream is detached'); } $contents = stream_get_contents($this->stream); if ($contents === false) { throw new \RuntimeException('Unable to read stream contents'); } return $contents; } public function close() { if (isset($this->stream)) { if (is_resource($this->stream)) { fclose($this->stream); } $this->detach(); } } public function detach() { if (!isset($this->stream)) { return null; } $result = $this->stream; unset($this->stream); $this->size = $this->uri = null; $this->readable = $this->writable = $this->seekable = false; return $result; } public function getSize() { if ($this->size !== null) { return $this->size; } if (!isset($this->stream)) { return null; } // Clear the stat cache if the stream has a URI if ($this->uri) { clearstatcache(true, $this->uri); } $stats = fstat($this->stream); if (isset($stats['size'])) { $this->size = $stats['size']; return $this->size; } return null; } public function isReadable() { return $this->readable; } public function isWritable() { return $this->writable; } public function isSeekable() { return $this->seekable; } public function eof() { if (!isset($this->stream)) { throw new \RuntimeException('Stream is detached'); } return feof($this->stream); } public function tell() { if (!isset($this->stream)) { throw new \RuntimeException('Stream is detached'); } $result = ftell($this->stream); if ($result === false) { throw new \RuntimeException('Unable to determine stream position'); } return $result; } public function rewind() { $this->seek(0); } public function seek($offset, $whence = SEEK_SET) { $whence = (int) $whence; if (!isset($this->stream)) { throw new \RuntimeException('Stream is detached'); } if (!$this->seekable) { throw new \RuntimeException('Stream is not seekable'); } if (fseek($this->stream, $offset, $whence) === -1) { throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . var_export($whence, true)); } } public function read($length) { if (!isset($this->stream)) { throw new \RuntimeException('Stream is detached'); } if (!$this->readable) { throw new \RuntimeException('Cannot read from non-readable stream'); } if ($length < 0) { throw new \RuntimeException('Length parameter cannot be negative'); } if (0 === $length) { return ''; } $string = fread($this->stream, $length); if (false === $string) { throw new \RuntimeException('Unable to read from stream'); } return $string; } public function write($string) { if (!isset($this->stream)) { throw new \RuntimeException('Stream is detached'); } if (!$this->writable) { throw new \RuntimeException('Cannot write to a non-writable stream'); } // We can't know the size after writing anything $this->size = null; $result = fwrite($this->stream, $string); if ($result === false) { throw new \RuntimeException('Unable to write to stream'); } return $result; } public function getMetadata($key = null) { if (!isset($this->stream)) { return $key ? null : []; } elseif (!$key) { return $this->customMetadata + stream_get_meta_data($this->stream); } elseif (isset($this->customMetadata[$key])) { return $this->customMetadata[$key]; } $meta = stream_get_meta_data($this->stream); return isset($meta[$key]) ? $meta[$key] : null; } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\MessageInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; /** * Returns the string representation of an HTTP message. * * @param MessageInterface $message Message to convert to a string. * * @return string */ function str(MessageInterface $message) { if ($message instanceof RequestInterface) { $msg = trim($message->getMethod() . ' ' . $message->getRequestTarget()) . ' HTTP/' . $message->getProtocolVersion(); if (!$message->hasHeader('host')) { $msg .= "\r\nHost: " . $message->getUri()->getHost(); } } elseif ($message instanceof ResponseInterface) { $msg = 'HTTP/' . $message->getProtocolVersion() . ' ' . $message->getStatusCode() . ' ' . $message->getReasonPhrase(); } else { throw new \InvalidArgumentException('Unknown message type'); } foreach ($message->getHeaders() as $name => $values) { $msg .= "\r\n{$name}: " . implode(', ', $values); } return "{$msg}\r\n\r\n" . $message->getBody(); } /** * Returns a UriInterface for the given value. * * This function accepts a string or {@see Psr\Http\Message\UriInterface} and * returns a UriInterface for the given value. If the value is already a * `UriInterface`, it is returned as-is. * * @param string|UriInterface $uri * * @return UriInterface * @throws \InvalidArgumentException */ function uri_for($uri) { if ($uri instanceof UriInterface) { return $uri; } elseif (is_string($uri)) { return new Uri($uri); } throw new \InvalidArgumentException('URI must be a string or UriInterface'); } /** * Create a new stream based on the input type. * * Options is an associative array that can contain the following keys: * - metadata: Array of custom metadata. * - size: Size of the stream. * * @param resource|string|null|int|float|bool|StreamInterface|callable|\Iterator $resource Entity body data * @param array $options Additional options * * @return StreamInterface * @throws \InvalidArgumentException if the $resource arg is not valid. */ function stream_for($resource = '', array $options = []) { if (is_scalar($resource)) { $stream = fopen('php://temp', 'r+'); if ($resource !== '') { fwrite($stream, $resource); fseek($stream, 0); } return new Stream($stream, $options); } switch (gettype($resource)) { case 'resource': return new Stream($resource, $options); case 'object': if ($resource instanceof StreamInterface) { return $resource; } elseif ($resource instanceof \Iterator) { return new PumpStream(function () use ($resource) { if (!$resource->valid()) { return false; } $result = $resource->current(); $resource->next(); return $result; }, $options); } elseif (method_exists($resource, '__toString')) { return stream_for((string) $resource, $options); } break; case 'NULL': return new Stream(fopen('php://temp', 'r+'), $options); } if (is_callable($resource)) { return new PumpStream($resource, $options); } throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource)); } /** * Parse an array of header values containing ";" separated data into an * array of associative arrays representing the header key value pair * data of the header. When a parameter does not contain a value, but just * contains a key, this function will inject a key with a '' string value. * * @param string|array $header Header to parse into components. * * @return array Returns the parsed header values. */ function parse_header($header) { static $trimmed = "\"' \n\t\r"; $params = $matches = []; foreach (normalize_header($header) as $val) { $part = []; foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) { if (preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) { $m = $matches[0]; if (isset($m[1])) { $part[trim($m[0], $trimmed)] = trim($m[1], $trimmed); } else { $part[] = trim($m[0], $trimmed); } } } if ($part) { $params[] = $part; } } return $params; } /** * Converts an array of header values that may contain comma separated * headers into an array of headers with no comma separated values. * * @param string|array $header Header to normalize. * * @return array Returns the normalized header field values. */ function normalize_header($header) { if (!is_array($header)) { return array_map('trim', explode(',', $header)); } $result = []; foreach ($header as $value) { foreach ((array) $value as $v) { if (strpos($v, ',') === false) { $result[] = $v; continue; } foreach (preg_split('/,(?=([^"]*"[^"]*")*[^"]*$)/', $v) as $vv) { $result[] = trim($vv); } } } return $result; } /** * Clone and modify a request with the given changes. * * The changes can be one of: * - method: (string) Changes the HTTP method. * - set_headers: (array) Sets the given headers. * - remove_headers: (array) Remove the given headers. * - body: (mixed) Sets the given body. * - uri: (UriInterface) Set the URI. * - query: (string) Set the query string value of the URI. * - version: (string) Set the protocol version. * * @param RequestInterface $request Request to clone and modify. * @param array $changes Changes to apply. * * @return RequestInterface */ function modify_request(RequestInterface $request, array $changes) { if (!$changes) { return $request; } $headers = $request->getHeaders(); if (!isset($changes['uri'])) { $uri = $request->getUri(); } else { // Remove the host header if one is on the URI if ($host = $changes['uri']->getHost()) { $changes['set_headers']['Host'] = $host; if ($port = $changes['uri']->getPort()) { $standardPorts = ['http' => 80, 'https' => 443]; $scheme = $changes['uri']->getScheme(); if (isset($standardPorts[$scheme]) && $port != $standardPorts[$scheme]) { $changes['set_headers']['Host'] .= ':'.$port; } } } $uri = $changes['uri']; } if (!empty($changes['remove_headers'])) { $headers = _caseless_remove($changes['remove_headers'], $headers); } if (!empty($changes['set_headers'])) { $headers = _caseless_remove(array_keys($changes['set_headers']), $headers); $headers = $changes['set_headers'] + $headers; } if (isset($changes['query'])) { $uri = $uri->withQuery($changes['query']); } if ($request instanceof ServerRequestInterface) { return (new ServerRequest( isset($changes['method']) ? $changes['method'] : $request->getMethod(), $uri, $headers, isset($changes['body']) ? $changes['body'] : $request->getBody(), isset($changes['version']) ? $changes['version'] : $request->getProtocolVersion(), $request->getServerParams() )) ->withParsedBody($request->getParsedBody()) ->withQueryParams($request->getQueryParams()) ->withCookieParams($request->getCookieParams()) ->withUploadedFiles($request->getUploadedFiles()); } return new Request( isset($changes['method']) ? $changes['method'] : $request->getMethod(), $uri, $headers, isset($changes['body']) ? $changes['body'] : $request->getBody(), isset($changes['version']) ? $changes['version'] : $request->getProtocolVersion() ); } /** * Attempts to rewind a message body and throws an exception on failure. * * The body of the message will only be rewound if a call to `tell()` returns a * value other than `0`. * * @param MessageInterface $message Message to rewind * * @throws \RuntimeException */ function rewind_body(MessageInterface $message) { $body = $message->getBody(); if ($body->tell()) { $body->rewind(); } } /** * Safely opens a PHP stream resource using a filename. * * When fopen fails, PHP normally raises a warning. This function adds an * error handler that checks for errors and throws an exception instead. * * @param string $filename File to open * @param string $mode Mode used to open the file * * @return resource * @throws \RuntimeException if the file cannot be opened */ function try_fopen($filename, $mode) { $ex = null; set_error_handler(function () use ($filename, $mode, &$ex) { $ex = new \RuntimeException(sprintf( 'Unable to open %s using mode %s: %s', $filename, $mode, func_get_args()[1] )); }); $handle = fopen($filename, $mode); restore_error_handler(); if ($ex) { /** @var $ex \RuntimeException */ throw $ex; } return $handle; } /** * Copy the contents of a stream into a string until the given number of * bytes have been read. * * @param StreamInterface $stream Stream to read * @param int $maxLen Maximum number of bytes to read. Pass -1 * to read the entire stream. * @return string * @throws \RuntimeException on error. */ function copy_to_string(StreamInterface $stream, $maxLen = -1) { $buffer = ''; if ($maxLen === -1) { while (!$stream->eof()) { $buf = $stream->read(1048576); // Using a loose equality here to match on '' and false. if ($buf == null) { break; } $buffer .= $buf; } return $buffer; } $len = 0; while (!$stream->eof() && $len < $maxLen) { $buf = $stream->read($maxLen - $len); // Using a loose equality here to match on '' and false. if ($buf == null) { break; } $buffer .= $buf; $len = strlen($buffer); } return $buffer; } /** * Copy the contents of a stream into another stream until the given number * of bytes have been read. * * @param StreamInterface $source Stream to read from * @param StreamInterface $dest Stream to write to * @param int $maxLen Maximum number of bytes to read. Pass -1 * to read the entire stream. * * @throws \RuntimeException on error. */ function copy_to_stream( StreamInterface $source, StreamInterface $dest, $maxLen = -1 ) { $bufferSize = 8192; if ($maxLen === -1) { while (!$source->eof()) { if (!$dest->write($source->read($bufferSize))) { break; } } } else { $remaining = $maxLen; while ($remaining > 0 && !$source->eof()) { $buf = $source->read(min($bufferSize, $remaining)); $len = strlen($buf); if (!$len) { break; } $remaining -= $len; $dest->write($buf); } } } /** * Calculate a hash of a Stream * * @param StreamInterface $stream Stream to calculate the hash for * @param string $algo Hash algorithm (e.g. md5, crc32, etc) * @param bool $rawOutput Whether or not to use raw output * * @return string Returns the hash of the stream * @throws \RuntimeException on error. */ function hash( StreamInterface $stream, $algo, $rawOutput = false ) { $pos = $stream->tell(); if ($pos > 0) { $stream->rewind(); } $ctx = hash_init($algo); while (!$stream->eof()) { hash_update($ctx, $stream->read(1048576)); } $out = hash_final($ctx, (bool) $rawOutput); $stream->seek($pos); return $out; } /** * Read a line from the stream up to the maximum allowed buffer length * * @param StreamInterface $stream Stream to read from * @param int $maxLength Maximum buffer length * * @return string */ function readline(StreamInterface $stream, $maxLength = null) { $buffer = ''; $size = 0; while (!$stream->eof()) { // Using a loose equality here to match on '' and false. if (null == ($byte = $stream->read(1))) { return $buffer; } $buffer .= $byte; // Break when a new line is found or the max length - 1 is reached if ($byte === "\n" || ++$size === $maxLength - 1) { break; } } return $buffer; } /** * Parses a request message string into a request object. * * @param string $message Request message string. * * @return Request */ function parse_request($message) { $data = _parse_message($message); $matches = []; if (!preg_match('/^[\S]+\s+([a-zA-Z]+:\/\/|\/).*/', $data['start-line'], $matches)) { throw new \InvalidArgumentException('Invalid request string'); } $parts = explode(' ', $data['start-line'], 3); $version = isset($parts[2]) ? explode('/', $parts[2])[1] : '1.1'; $request = new Request( $parts[0], $matches[1] === '/' ? _parse_request_uri($parts[1], $data['headers']) : $parts[1], $data['headers'], $data['body'], $version ); return $matches[1] === '/' ? $request : $request->withRequestTarget($parts[1]); } /** * Parses a response message string into a response object. * * @param string $message Response message string. * * @return Response */ function parse_response($message) { $data = _parse_message($message); // According to https://tools.ietf.org/html/rfc7230#section-3.1.2 the space // between status-code and reason-phrase is required. But browsers accept // responses without space and reason as well. if (!preg_match('/^HTTP\/.* [0-9]{3}( .*|$)/', $data['start-line'])) { throw new \InvalidArgumentException('Invalid response string: ' . $data['start-line']); } $parts = explode(' ', $data['start-line'], 3); return new Response( $parts[1], $data['headers'], $data['body'], explode('/', $parts[0])[1], isset($parts[2]) ? $parts[2] : null ); } /** * Parse a query string into an associative array. * * If multiple values are found for the same key, the value of that key * value pair will become an array. This function does not parse nested * PHP style arrays into an associative array (e.g., foo[a]=1&foo[b]=2 will * be parsed into ['foo[a]' => '1', 'foo[b]' => '2']). * * @param string $str Query string to parse * @param int|bool $urlEncoding How the query string is encoded * * @return array */ function parse_query($str, $urlEncoding = true) { $result = []; if ($str === '') { return $result; } if ($urlEncoding === true) { $decoder = function ($value) { return rawurldecode(str_replace('+', ' ', $value)); }; } elseif ($urlEncoding === PHP_QUERY_RFC3986) { $decoder = 'rawurldecode'; } elseif ($urlEncoding === PHP_QUERY_RFC1738) { $decoder = 'urldecode'; } else { $decoder = function ($str) { return $str; }; } foreach (explode('&', $str) as $kvp) { $parts = explode('=', $kvp, 2); $key = $decoder($parts[0]); $value = isset($parts[1]) ? $decoder($parts[1]) : null; if (!isset($result[$key])) { $result[$key] = $value; } else { if (!is_array($result[$key])) { $result[$key] = [$result[$key]]; } $result[$key][] = $value; } } return $result; } /** * Build a query string from an array of key value pairs. * * This function can use the return value of parse_query() to build a query * string. This function does not modify the provided keys when an array is * encountered (like http_build_query would). * * @param array $params Query string parameters. * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986 * to encode using RFC3986, or PHP_QUERY_RFC1738 * to encode using RFC1738. * @return string */ function build_query(array $params, $encoding = PHP_QUERY_RFC3986) { if (!$params) { return ''; } if ($encoding === false) { $encoder = function ($str) { return $str; }; } elseif ($encoding === PHP_QUERY_RFC3986) { $encoder = 'rawurlencode'; } elseif ($encoding === PHP_QUERY_RFC1738) { $encoder = 'urlencode'; } else { throw new \InvalidArgumentException('Invalid type'); } $qs = ''; foreach ($params as $k => $v) { $k = $encoder($k); if (!is_array($v)) { $qs .= $k; if ($v !== null) { $qs .= '=' . $encoder($v); } $qs .= '&'; } else { foreach ($v as $vv) { $qs .= $k; if ($vv !== null) { $qs .= '=' . $encoder($vv); } $qs .= '&'; } } } return $qs ? (string) substr($qs, 0, -1) : ''; } /** * Determines the mimetype of a file by looking at its extension. * * @param $filename * * @return null|string */ function mimetype_from_filename($filename) { return mimetype_from_extension(pathinfo($filename, PATHINFO_EXTENSION)); } /** * Maps a file extensions to a mimetype. * * @param $extension string The file extension. * * @return string|null * @link http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types */ function mimetype_from_extension($extension) { static $mimetypes = [ '3gp' => 'video/3gpp', '7z' => 'application/x-7z-compressed', 'aac' => 'audio/x-aac', 'ai' => 'application/postscript', 'aif' => 'audio/x-aiff', 'asc' => 'text/plain', 'asf' => 'video/x-ms-asf', 'atom' => 'application/atom+xml', 'avi' => 'video/x-msvideo', 'bmp' => 'image/bmp', 'bz2' => 'application/x-bzip2', 'cer' => 'application/pkix-cert', 'crl' => 'application/pkix-crl', 'crt' => 'application/x-x509-ca-cert', 'css' => 'text/css', 'csv' => 'text/csv', 'cu' => 'application/cu-seeme', 'deb' => 'application/x-debian-package', 'doc' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'dvi' => 'application/x-dvi', 'eot' => 'application/vnd.ms-fontobject', 'eps' => 'application/postscript', 'epub' => 'application/epub+zip', 'etx' => 'text/x-setext', 'flac' => 'audio/flac', 'flv' => 'video/x-flv', 'gif' => 'image/gif', 'gz' => 'application/gzip', 'htm' => 'text/html', 'html' => 'text/html', 'ico' => 'image/x-icon', 'ics' => 'text/calendar', 'ini' => 'text/plain', 'iso' => 'application/x-iso9660-image', 'jar' => 'application/java-archive', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'js' => 'text/javascript', 'json' => 'application/json', 'latex' => 'application/x-latex', 'log' => 'text/plain', 'm4a' => 'audio/mp4', 'm4v' => 'video/mp4', 'mid' => 'audio/midi', 'midi' => 'audio/midi', 'mov' => 'video/quicktime', 'mkv' => 'video/x-matroska', 'mp3' => 'audio/mpeg', 'mp4' => 'video/mp4', 'mp4a' => 'audio/mp4', 'mp4v' => 'video/mp4', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpg4' => 'video/mp4', 'oga' => 'audio/ogg', 'ogg' => 'audio/ogg', 'ogv' => 'video/ogg', 'ogx' => 'application/ogg', 'pbm' => 'image/x-portable-bitmap', 'pdf' => 'application/pdf', 'pgm' => 'image/x-portable-graymap', 'png' => 'image/png', 'pnm' => 'image/x-portable-anymap', 'ppm' => 'image/x-portable-pixmap', 'ppt' => 'application/vnd.ms-powerpoint', 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'ps' => 'application/postscript', 'qt' => 'video/quicktime', 'rar' => 'application/x-rar-compressed', 'ras' => 'image/x-cmu-raster', 'rss' => 'application/rss+xml', 'rtf' => 'application/rtf', 'sgm' => 'text/sgml', 'sgml' => 'text/sgml', 'svg' => 'image/svg+xml', 'swf' => 'application/x-shockwave-flash', 'tar' => 'application/x-tar', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'torrent' => 'application/x-bittorrent', 'ttf' => 'application/x-font-ttf', 'txt' => 'text/plain', 'wav' => 'audio/x-wav', 'webm' => 'video/webm', 'webp' => 'image/webp', 'wma' => 'audio/x-ms-wma', 'wmv' => 'video/x-ms-wmv', 'woff' => 'application/x-font-woff', 'wsdl' => 'application/wsdl+xml', 'xbm' => 'image/x-xbitmap', 'xls' => 'application/vnd.ms-excel', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xml' => 'application/xml', 'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', 'yaml' => 'text/yaml', 'yml' => 'text/yaml', 'zip' => 'application/zip', ]; $extension = strtolower($extension); return isset($mimetypes[$extension]) ? $mimetypes[$extension] : null; } /** * Parses an HTTP message into an associative array. * * The array contains the "start-line" key containing the start line of * the message, "headers" key containing an associative array of header * array values, and a "body" key containing the body of the message. * * @param string $message HTTP request or response to parse. * * @return array * @internal */ function _parse_message($message) { if (!$message) { throw new \InvalidArgumentException('Invalid message'); } $message = ltrim($message, "\r\n"); $messageParts = preg_split("/\r?\n\r?\n/", $message, 2); if ($messageParts === false || count($messageParts) !== 2) { throw new \InvalidArgumentException('Invalid message: Missing header delimiter'); } list($rawHeaders, $body) = $messageParts; $rawHeaders .= "\r\n"; // Put back the delimiter we split previously $headerParts = preg_split("/\r?\n/", $rawHeaders, 2); if ($headerParts === false || count($headerParts) !== 2) { throw new \InvalidArgumentException('Invalid message: Missing status line'); } list($startLine, $rawHeaders) = $headerParts; if (preg_match("/(?:^HTTP\/|^[A-Z]+ \S+ HTTP\/)(\d+(?:\.\d+)?)/i", $startLine, $matches) && $matches[1] === '1.0') { // Header folding is deprecated for HTTP/1.1, but allowed in HTTP/1.0 $rawHeaders = preg_replace(Rfc7230::HEADER_FOLD_REGEX, ' ', $rawHeaders); } /** @var array[] $headerLines */ $count = preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, PREG_SET_ORDER); // If these aren't the same, then one line didn't match and there's an invalid header. if ($count !== substr_count($rawHeaders, "\n")) { // Folding is deprecated, see https://tools.ietf.org/html/rfc7230#section-3.2.4 if (preg_match(Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) { throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding'); } throw new \InvalidArgumentException('Invalid header syntax'); } $headers = []; foreach ($headerLines as $headerLine) { $headers[$headerLine[1]][] = $headerLine[2]; } return [ 'start-line' => $startLine, 'headers' => $headers, 'body' => $body, ]; } /** * Constructs a URI for an HTTP request message. * * @param string $path Path from the start-line * @param array $headers Array of headers (each value an array). * * @return string * @internal */ function _parse_request_uri($path, array $headers) { $hostKey = array_filter(array_keys($headers), function ($k) { return strtolower($k) === 'host'; }); // If no host is found, then a full URI cannot be constructed. if (!$hostKey) { return $path; } $host = $headers[reset($hostKey)][0]; $scheme = substr($host, -4) === ':443' ? 'https' : 'http'; return $scheme . '://' . $host . '/' . ltrim($path, '/'); } /** * Get a short summary of the message body * * Will return `null` if the response is not printable. * * @param MessageInterface $message The message to get the body summary * @param int $truncateAt The maximum allowed size of the summary * * @return null|string */ function get_message_body_summary(MessageInterface $message, $truncateAt = 120) { $body = $message->getBody(); if (!$body->isSeekable() || !$body->isReadable()) { return null; } $size = $body->getSize(); if ($size === 0) { return null; } $summary = $body->read($truncateAt); $body->rewind(); if ($size > $truncateAt) { $summary .= ' (truncated...)'; } // Matches any printable character, including unicode characters: // letters, marks, numbers, punctuation, spacing, and separators. if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/', $summary)) { return null; } return $summary; } /** @internal */ function _caseless_remove($keys, array $data) { $result = []; foreach ($keys as &$key) { $key = strtolower($key); } foreach ($data as $k => $v) { if (!in_array(strtolower($k), $keys)) { $result[$k] = $v; } } return $result; } <?php // Don't redefine the functions if included multiple times. if (!function_exists('GuzzleHttp\Psr7\str')) { require __DIR__ . '/functions.php'; } <?php namespace GuzzleHttp\Psr7; use InvalidArgumentException; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UploadedFileInterface; use RuntimeException; class UploadedFile implements UploadedFileInterface { /** * @var int[] */ private static $errors = [ UPLOAD_ERR_OK, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_EXTENSION, ]; /** * @var string */ private $clientFilename; /** * @var string */ private $clientMediaType; /** * @var int */ private $error; /** * @var null|string */ private $file; /** * @var bool */ private $moved = false; /** * @var int */ private $size; /** * @var StreamInterface|null */ private $stream; /** * @param StreamInterface|string|resource $streamOrFile * @param int $size * @param int $errorStatus * @param string|null $clientFilename * @param string|null $clientMediaType */ public function __construct( $streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null ) { $this->setError($errorStatus); $this->setSize($size); $this->setClientFilename($clientFilename); $this->setClientMediaType($clientMediaType); if ($this->isOk()) { $this->setStreamOrFile($streamOrFile); } } /** * Depending on the value set file or stream variable * * @param mixed $streamOrFile * @throws InvalidArgumentException */ private function setStreamOrFile($streamOrFile) { if (is_string($streamOrFile)) { $this->file = $streamOrFile; } elseif (is_resource($streamOrFile)) { $this->stream = new Stream($streamOrFile); } elseif ($streamOrFile instanceof StreamInterface) { $this->stream = $streamOrFile; } else { throw new InvalidArgumentException( 'Invalid stream or file provided for UploadedFile' ); } } /** * @param int $error * @throws InvalidArgumentException */ private function setError($error) { if (false === is_int($error)) { throw new InvalidArgumentException( 'Upload file error status must be an integer' ); } if (false === in_array($error, UploadedFile::$errors)) { throw new InvalidArgumentException( 'Invalid error status for UploadedFile' ); } $this->error = $error; } /** * @param int $size * @throws InvalidArgumentException */ private function setSize($size) { if (false === is_int($size)) { throw new InvalidArgumentException( 'Upload file size must be an integer' ); } $this->size = $size; } /** * @param mixed $param * @return boolean */ private function isStringOrNull($param) { return in_array(gettype($param), ['string', 'NULL']); } /** * @param mixed $param * @return boolean */ private function isStringNotEmpty($param) { return is_string($param) && false === empty($param); } /** * @param string|null $clientFilename * @throws InvalidArgumentException */ private function setClientFilename($clientFilename) { if (false === $this->isStringOrNull($clientFilename)) { throw new InvalidArgumentException( 'Upload file client filename must be a string or null' ); } $this->clientFilename = $clientFilename; } /** * @param string|null $clientMediaType * @throws InvalidArgumentException */ private function setClientMediaType($clientMediaType) { if (false === $this->isStringOrNull($clientMediaType)) { throw new InvalidArgumentException( 'Upload file client media type must be a string or null' ); } $this->clientMediaType = $clientMediaType; } /** * Return true if there is no upload error * * @return boolean */ private function isOk() { return $this->error === UPLOAD_ERR_OK; } /** * @return boolean */ public function isMoved() { return $this->moved; } /** * @throws RuntimeException if is moved or not ok */ private function validateActive() { if (false === $this->isOk()) { throw new RuntimeException('Cannot retrieve stream due to upload error'); } if ($this->isMoved()) { throw new RuntimeException('Cannot retrieve stream after it has already been moved'); } } /** * {@inheritdoc} * @throws RuntimeException if the upload was not successful. */ public function getStream() { $this->validateActive(); if ($this->stream instanceof StreamInterface) { return $this->stream; } return new LazyOpenStream($this->file, 'r+'); } /** * {@inheritdoc} * * @see http://php.net/is_uploaded_file * @see http://php.net/move_uploaded_file * @param string $targetPath Path to which to move the uploaded file. * @throws RuntimeException if the upload was not successful. * @throws InvalidArgumentException if the $path specified is invalid. * @throws RuntimeException on any error during the move operation, or on * the second or subsequent call to the method. */ public function moveTo($targetPath) { $this->validateActive(); if (false === $this->isStringNotEmpty($targetPath)) { throw new InvalidArgumentException( 'Invalid path provided for move operation; must be a non-empty string' ); } if ($this->file) { $this->moved = php_sapi_name() == 'cli' ? rename($this->file, $targetPath) : move_uploaded_file($this->file, $targetPath); } else { copy_to_stream( $this->getStream(), new LazyOpenStream($targetPath, 'w') ); $this->moved = true; } if (false === $this->moved) { throw new RuntimeException( sprintf('Uploaded file could not be moved to %s', $targetPath) ); } } /** * {@inheritdoc} * * @return int|null The file size in bytes or null if unknown. */ public function getSize() { return $this->size; } /** * {@inheritdoc} * * @see http://php.net/manual/en/features.file-upload.errors.php * @return int One of PHP's UPLOAD_ERR_XXX constants. */ public function getError() { return $this->error; } /** * {@inheritdoc} * * @return string|null The filename sent by the client or null if none * was provided. */ public function getClientFilename() { return $this->clientFilename; } /** * {@inheritdoc} */ public function getClientMediaType() { return $this->clientMediaType; } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Stream decorator trait * @property StreamInterface stream */ trait StreamDecoratorTrait { /** * @param StreamInterface $stream Stream to decorate */ public function __construct(StreamInterface $stream) { $this->stream = $stream; } /** * Magic method used to create a new stream if streams are not added in * the constructor of a decorator (e.g., LazyOpenStream). * * @param string $name Name of the property (allows "stream" only). * * @return StreamInterface */ public function __get($name) { if ($name == 'stream') { $this->stream = $this->createStream(); return $this->stream; } throw new \UnexpectedValueException("$name not found on class"); } public function __toString() { try { if ($this->isSeekable()) { $this->seek(0); } return $this->getContents(); } catch (\Exception $e) { // Really, PHP? https://bugs.php.net/bug.php?id=53648 trigger_error('StreamDecorator::__toString exception: ' . (string) $e, E_USER_ERROR); return ''; } } public function getContents() { return copy_to_string($this); } /** * Allow decorators to implement custom methods * * @param string $method Missing method name * @param array $args Method arguments * * @return mixed */ public function __call($method, array $args) { $result = call_user_func_array([$this->stream, $method], $args); // Always return the wrapped object if the result is a return $this return $result === $this->stream ? $this : $result; } public function close() { $this->stream->close(); } public function getMetadata($key = null) { return $this->stream->getMetadata($key); } public function detach() { return $this->stream->detach(); } public function getSize() { return $this->stream->getSize(); } public function eof() { return $this->stream->eof(); } public function tell() { return $this->stream->tell(); } public function isReadable() { return $this->stream->isReadable(); } public function isWritable() { return $this->stream->isWritable(); } public function isSeekable() { return $this->stream->isSeekable(); } public function rewind() { $this->seek(0); } public function seek($offset, $whence = SEEK_SET) { $this->stream->seek($offset, $whence); } public function read($length) { return $this->stream->read($length); } public function write($string) { return $this->stream->write($string); } /** * Implement in subclasses to dynamically create streams when requested. * * @return StreamInterface * @throws \BadMethodCallException */ protected function createStream() { throw new \BadMethodCallException('Not implemented'); } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Provides a read only stream that pumps data from a PHP callable. * * When invoking the provided callable, the PumpStream will pass the amount of * data requested to read to the callable. The callable can choose to ignore * this value and return fewer or more bytes than requested. Any extra data * returned by the provided callable is buffered internally until drained using * the read() function of the PumpStream. The provided callable MUST return * false when there is no more data to read. */ class PumpStream implements StreamInterface { /** @var callable */ private $source; /** @var int */ private $size; /** @var int */ private $tellPos = 0; /** @var array */ private $metadata; /** @var BufferStream */ private $buffer; /** * @param callable $source Source of the stream data. The callable MAY * accept an integer argument used to control the * amount of data to return. The callable MUST * return a string when called, or false on error * or EOF. * @param array $options Stream options: * - metadata: Hash of metadata to use with stream. * - size: Size of the stream, if known. */ public function __construct(callable $source, array $options = []) { $this->source = $source; $this->size = isset($options['size']) ? $options['size'] : null; $this->metadata = isset($options['metadata']) ? $options['metadata'] : []; $this->buffer = new BufferStream(); } public function __toString() { try { return copy_to_string($this); } catch (\Exception $e) { return ''; } } public function close() { $this->detach(); } public function detach() { $this->tellPos = false; $this->source = null; } public function getSize() { return $this->size; } public function tell() { return $this->tellPos; } public function eof() { return !$this->source; } public function isSeekable() { return false; } public function rewind() { $this->seek(0); } public function seek($offset, $whence = SEEK_SET) { throw new \RuntimeException('Cannot seek a PumpStream'); } public function isWritable() { return false; } public function write($string) { throw new \RuntimeException('Cannot write to a PumpStream'); } public function isReadable() { return true; } public function read($length) { $data = $this->buffer->read($length); $readLen = strlen($data); $this->tellPos += $readLen; $remaining = $length - $readLen; if ($remaining) { $this->pump($remaining); $data .= $this->buffer->read($remaining); $this->tellPos += strlen($data) - $readLen; } return $data; } public function getContents() { $result = ''; while (!$this->eof()) { $result .= $this->read(1000000); } return $result; } public function getMetadata($key = null) { if (!$key) { return $this->metadata; } return isset($this->metadata[$key]) ? $this->metadata[$key] : null; } private function pump($length) { if ($this->source) { do { $data = call_user_func($this->source, $length); if ($data === false || $data === null) { $this->source = null; return; } $this->buffer->write($data); $length -= strlen($data); } while ($length > 0); } } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Converts Guzzle streams into PHP stream resources. */ class StreamWrapper { /** @var resource */ public $context; /** @var StreamInterface */ private $stream; /** @var string r, r+, or w */ private $mode; /** * Returns a resource representing the stream. * * @param StreamInterface $stream The stream to get a resource for * * @return resource * @throws \InvalidArgumentException if stream is not readable or writable */ public static function getResource(StreamInterface $stream) { self::register(); if ($stream->isReadable()) { $mode = $stream->isWritable() ? 'r+' : 'r'; } elseif ($stream->isWritable()) { $mode = 'w'; } else { throw new \InvalidArgumentException('The stream must be readable, ' . 'writable, or both.'); } return fopen('guzzle://stream', $mode, null, self::createStreamContext($stream)); } /** * Creates a stream context that can be used to open a stream as a php stream resource. * * @param StreamInterface $stream * * @return resource */ public static function createStreamContext(StreamInterface $stream) { return stream_context_create([ 'guzzle' => ['stream' => $stream] ]); } /** * Registers the stream wrapper if needed */ public static function register() { if (!in_array('guzzle', stream_get_wrappers())) { stream_wrapper_register('guzzle', __CLASS__); } } public function stream_open($path, $mode, $options, &$opened_path) { $options = stream_context_get_options($this->context); if (!isset($options['guzzle']['stream'])) { return false; } $this->mode = $mode; $this->stream = $options['guzzle']['stream']; return true; } public function stream_read($count) { return $this->stream->read($count); } public function stream_write($data) { return (int) $this->stream->write($data); } public function stream_tell() { return $this->stream->tell(); } public function stream_eof() { return $this->stream->eof(); } public function stream_seek($offset, $whence) { $this->stream->seek($offset, $whence); return true; } public function stream_cast($cast_as) { $stream = clone($this->stream); return $stream->detach(); } public function stream_stat() { static $modeMap = [ 'r' => 33060, 'rb' => 33060, 'r+' => 33206, 'w' => 33188, 'wb' => 33188 ]; return [ 'dev' => 0, 'ino' => 0, 'mode' => $modeMap[$this->mode], 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => $this->stream->getSize() ?: 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0 ]; } public function url_stat($path, $flags) { return [ 'dev' => 0, 'ino' => 0, 'mode' => 0, 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0 ]; } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Compose stream implementations based on a hash of functions. * * Allows for easy testing and extension of a provided stream without needing * to create a concrete class for a simple extension point. */ class FnStream implements StreamInterface { /** @var array */ private $methods; /** @var array Methods that must be implemented in the given array */ private static $slots = ['__toString', 'close', 'detach', 'rewind', 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', 'isReadable', 'read', 'getContents', 'getMetadata']; /** * @param array $methods Hash of method name to a callable. */ public function __construct(array $methods) { $this->methods = $methods; // Create the functions on the class foreach ($methods as $name => $fn) { $this->{'_fn_' . $name} = $fn; } } /** * Lazily determine which methods are not implemented. * @throws \BadMethodCallException */ public function __get($name) { throw new \BadMethodCallException(str_replace('_fn_', '', $name) . '() is not implemented in the FnStream'); } /** * The close method is called on the underlying stream only if possible. */ public function __destruct() { if (isset($this->_fn_close)) { call_user_func($this->_fn_close); } } /** * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. * @throws \LogicException */ public function __wakeup() { throw new \LogicException('FnStream should never be unserialized'); } /** * Adds custom functionality to an underlying stream by intercepting * specific method calls. * * @param StreamInterface $stream Stream to decorate * @param array $methods Hash of method name to a closure * * @return FnStream */ public static function decorate(StreamInterface $stream, array $methods) { // If any of the required methods were not provided, then simply // proxy to the decorated stream. foreach (array_diff(self::$slots, array_keys($methods)) as $diff) { $methods[$diff] = [$stream, $diff]; } return new self($methods); } public function __toString() { return call_user_func($this->_fn___toString); } public function close() { return call_user_func($this->_fn_close); } public function detach() { return call_user_func($this->_fn_detach); } public function getSize() { return call_user_func($this->_fn_getSize); } public function tell() { return call_user_func($this->_fn_tell); } public function eof() { return call_user_func($this->_fn_eof); } public function isSeekable() { return call_user_func($this->_fn_isSeekable); } public function rewind() { call_user_func($this->_fn_rewind); } public function seek($offset, $whence = SEEK_SET) { call_user_func($this->_fn_seek, $offset, $whence); } public function isWritable() { return call_user_func($this->_fn_isWritable); } public function write($string) { return call_user_func($this->_fn_write, $string); } public function isReadable() { return call_user_func($this->_fn_isReadable); } public function read($length) { return call_user_func($this->_fn_read, $length); } public function getContents() { return call_user_func($this->_fn_getContents); } public function getMetadata($key = null) { return call_user_func($this->_fn_getMetadata, $key); } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; /** * PSR-7 response implementation. */ class Response implements ResponseInterface { use MessageTrait; /** @var array Map of standard HTTP status code/reason phrases */ private static $phrases = [ 100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-status', 208 => 'Already Reported', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 306 => 'Switch Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', 418 => 'I\'m a teapot', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 425 => 'Unordered Collection', 426 => 'Upgrade Required', 428 => 'Precondition Required', 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large', 451 => 'Unavailable For Legal Reasons', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', 508 => 'Loop Detected', 511 => 'Network Authentication Required', ]; /** @var string */ private $reasonPhrase = ''; /** @var int */ private $statusCode = 200; /** * @param int $status Status code * @param array $headers Response headers * @param string|null|resource|StreamInterface $body Response body * @param string $version Protocol version * @param string|null $reason Reason phrase (when empty a default will be used based on the status code) */ public function __construct( $status = 200, array $headers = [], $body = null, $version = '1.1', $reason = null ) { $this->assertStatusCodeIsInteger($status); $status = (int) $status; $this->assertStatusCodeRange($status); $this->statusCode = $status; if ($body !== '' && $body !== null) { $this->stream = stream_for($body); } $this->setHeaders($headers); if ($reason == '' && isset(self::$phrases[$this->statusCode])) { $this->reasonPhrase = self::$phrases[$this->statusCode]; } else { $this->reasonPhrase = (string) $reason; } $this->protocol = $version; } public function getStatusCode() { return $this->statusCode; } public function getReasonPhrase() { return $this->reasonPhrase; } public function withStatus($code, $reasonPhrase = '') { $this->assertStatusCodeIsInteger($code); $code = (int) $code; $this->assertStatusCodeRange($code); $new = clone $this; $new->statusCode = $code; if ($reasonPhrase == '' && isset(self::$phrases[$new->statusCode])) { $reasonPhrase = self::$phrases[$new->statusCode]; } $new->reasonPhrase = $reasonPhrase; return $new; } private function assertStatusCodeIsInteger($statusCode) { if (filter_var($statusCode, FILTER_VALIDATE_INT) === false) { throw new \InvalidArgumentException('Status code must be an integer value.'); } } private function assertStatusCodeRange($statusCode) { if ($statusCode < 100 || $statusCode >= 600) { throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 5xx.'); } } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Reads from multiple streams, one after the other. * * This is a read-only stream decorator. */ class AppendStream implements StreamInterface { /** @var StreamInterface[] Streams being decorated */ private $streams = []; private $seekable = true; private $current = 0; private $pos = 0; /** * @param StreamInterface[] $streams Streams to decorate. Each stream must * be readable. */ public function __construct(array $streams = []) { foreach ($streams as $stream) { $this->addStream($stream); } } public function __toString() { try { $this->rewind(); return $this->getContents(); } catch (\Exception $e) { return ''; } } /** * Add a stream to the AppendStream * * @param StreamInterface $stream Stream to append. Must be readable. * * @throws \InvalidArgumentException if the stream is not readable */ public function addStream(StreamInterface $stream) { if (!$stream->isReadable()) { throw new \InvalidArgumentException('Each stream must be readable'); } // The stream is only seekable if all streams are seekable if (!$stream->isSeekable()) { $this->seekable = false; } $this->streams[] = $stream; } public function getContents() { return copy_to_string($this); } /** * Closes each attached stream. * * {@inheritdoc} */ public function close() { $this->pos = $this->current = 0; $this->seekable = true; foreach ($this->streams as $stream) { $stream->close(); } $this->streams = []; } /** * Detaches each attached stream. * * Returns null as it's not clear which underlying stream resource to return. * * {@inheritdoc} */ public function detach() { $this->pos = $this->current = 0; $this->seekable = true; foreach ($this->streams as $stream) { $stream->detach(); } $this->streams = []; } public function tell() { return $this->pos; } /** * Tries to calculate the size by adding the size of each stream. * * If any of the streams do not return a valid number, then the size of the * append stream cannot be determined and null is returned. * * {@inheritdoc} */ public function getSize() { $size = 0; foreach ($this->streams as $stream) { $s = $stream->getSize(); if ($s === null) { return null; } $size += $s; } return $size; } public function eof() { return !$this->streams || ($this->current >= count($this->streams) - 1 && $this->streams[$this->current]->eof()); } public function rewind() { $this->seek(0); } /** * Attempts to seek to the given position. Only supports SEEK_SET. * * {@inheritdoc} */ public function seek($offset, $whence = SEEK_SET) { if (!$this->seekable) { throw new \RuntimeException('This AppendStream is not seekable'); } elseif ($whence !== SEEK_SET) { throw new \RuntimeException('The AppendStream can only seek with SEEK_SET'); } $this->pos = $this->current = 0; // Rewind each stream foreach ($this->streams as $i => $stream) { try { $stream->rewind(); } catch (\Exception $e) { throw new \RuntimeException('Unable to seek stream ' . $i . ' of the AppendStream', 0, $e); } } // Seek to the actual position by reading from each stream while ($this->pos < $offset && !$this->eof()) { $result = $this->read(min(8096, $offset - $this->pos)); if ($result === '') { break; } } } /** * Reads from all of the appended streams until the length is met or EOF. * * {@inheritdoc} */ public function read($length) { $buffer = ''; $total = count($this->streams) - 1; $remaining = $length; $progressToNext = false; while ($remaining > 0) { // Progress to the next stream if needed. if ($progressToNext || $this->streams[$this->current]->eof()) { $progressToNext = false; if ($this->current === $total) { break; } $this->current++; } $result = $this->streams[$this->current]->read($remaining); // Using a loose comparison here to match on '', false, and null if ($result == null) { $progressToNext = true; continue; } $buffer .= $result; $remaining = $length - strlen($buffer); } $this->pos += strlen($buffer); return $buffer; } public function isReadable() { return true; } public function isWritable() { return false; } public function isSeekable() { return $this->seekable; } public function write($string) { throw new \RuntimeException('Cannot write to an AppendStream'); } public function getMetadata($key = null) { return $key ? null : []; } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Stream decorator that can cache previously read bytes from a sequentially * read stream. */ class CachingStream implements StreamInterface { use StreamDecoratorTrait; /** @var StreamInterface Stream being wrapped */ private $remoteStream; /** @var int Number of bytes to skip reading due to a write on the buffer */ private $skipReadBytes = 0; /** * We will treat the buffer object as the body of the stream * * @param StreamInterface $stream Stream to cache * @param StreamInterface $target Optionally specify where data is cached */ public function __construct( StreamInterface $stream, StreamInterface $target = null ) { $this->remoteStream = $stream; $this->stream = $target ?: new Stream(fopen('php://temp', 'r+')); } public function getSize() { return max($this->stream->getSize(), $this->remoteStream->getSize()); } public function rewind() { $this->seek(0); } public function seek($offset, $whence = SEEK_SET) { if ($whence == SEEK_SET) { $byte = $offset; } elseif ($whence == SEEK_CUR) { $byte = $offset + $this->tell(); } elseif ($whence == SEEK_END) { $size = $this->remoteStream->getSize(); if ($size === null) { $size = $this->cacheEntireStream(); } $byte = $size + $offset; } else { throw new \InvalidArgumentException('Invalid whence'); } $diff = $byte - $this->stream->getSize(); if ($diff > 0) { // Read the remoteStream until we have read in at least the amount // of bytes requested, or we reach the end of the file. while ($diff > 0 && !$this->remoteStream->eof()) { $this->read($diff); $diff = $byte - $this->stream->getSize(); } } else { // We can just do a normal seek since we've already seen this byte. $this->stream->seek($byte); } } public function read($length) { // Perform a regular read on any previously read data from the buffer $data = $this->stream->read($length); $remaining = $length - strlen($data); // More data was requested so read from the remote stream if ($remaining) { // If data was written to the buffer in a position that would have // been filled from the remote stream, then we must skip bytes on // the remote stream to emulate overwriting bytes from that // position. This mimics the behavior of other PHP stream wrappers. $remoteData = $this->remoteStream->read( $remaining + $this->skipReadBytes ); if ($this->skipReadBytes) { $len = strlen($remoteData); $remoteData = substr($remoteData, $this->skipReadBytes); $this->skipReadBytes = max(0, $this->skipReadBytes - $len); } $data .= $remoteData; $this->stream->write($remoteData); } return $data; } public function write($string) { // When appending to the end of the currently read stream, you'll want // to skip bytes from being read from the remote stream to emulate // other stream wrappers. Basically replacing bytes of data of a fixed // length. $overflow = (strlen($string) + $this->tell()) - $this->remoteStream->tell(); if ($overflow > 0) { $this->skipReadBytes += $overflow; } return $this->stream->write($string); } public function eof() { return $this->stream->eof() && $this->remoteStream->eof(); } /** * Close both the remote stream and buffer stream */ public function close() { $this->remoteStream->close() && $this->stream->close(); } private function cacheEntireStream() { $target = new FnStream(['write' => 'strlen']); copy_to_stream($this, $target); return $this->tell(); } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Stream decorator that prevents a stream from being seeked */ class NoSeekStream implements StreamInterface { use StreamDecoratorTrait; public function seek($offset, $whence = SEEK_SET) { throw new \RuntimeException('Cannot seek a NoSeekStream'); } public function isSeekable() { return false; } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Stream that when read returns bytes for a streaming multipart or * multipart/form-data stream. */ class MultipartStream implements StreamInterface { use StreamDecoratorTrait; private $boundary; /** * @param array $elements Array of associative arrays, each containing a * required "name" key mapping to the form field, * name, a required "contents" key mapping to a * StreamInterface/resource/string, an optional * "headers" associative array of custom headers, * and an optional "filename" key mapping to a * string to send as the filename in the part. * @param string $boundary You can optionally provide a specific boundary * * @throws \InvalidArgumentException */ public function __construct(array $elements = [], $boundary = null) { $this->boundary = $boundary ?: sha1(uniqid('', true)); $this->stream = $this->createStream($elements); } /** * Get the boundary * * @return string */ public function getBoundary() { return $this->boundary; } public function isWritable() { return false; } /** * Get the headers needed before transferring the content of a POST file */ private function getHeaders(array $headers) { $str = ''; foreach ($headers as $key => $value) { $str .= "{$key}: {$value}\r\n"; } return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n"; } /** * Create the aggregate stream that will be used to upload the POST data */ protected function createStream(array $elements) { $stream = new AppendStream(); foreach ($elements as $element) { $this->addElement($stream, $element); } // Add the trailing boundary with CRLF $stream->addStream(stream_for("--{$this->boundary}--\r\n")); return $stream; } private function addElement(AppendStream $stream, array $element) { foreach (['contents', 'name'] as $key) { if (!array_key_exists($key, $element)) { throw new \InvalidArgumentException("A '{$key}' key is required"); } } $element['contents'] = stream_for($element['contents']); if (empty($element['filename'])) { $uri = $element['contents']->getMetadata('uri'); if (substr($uri, 0, 6) !== 'php://') { $element['filename'] = $uri; } } list($body, $headers) = $this->createElement( $element['name'], $element['contents'], isset($element['filename']) ? $element['filename'] : null, isset($element['headers']) ? $element['headers'] : [] ); $stream->addStream(stream_for($this->getHeaders($headers))); $stream->addStream($body); $stream->addStream(stream_for("\r\n")); } /** * @return array */ private function createElement($name, StreamInterface $stream, $filename, array $headers) { // Set a default content-disposition header if one was no provided $disposition = $this->getHeader($headers, 'content-disposition'); if (!$disposition) { $headers['Content-Disposition'] = ($filename === '0' || $filename) ? sprintf('form-data; name="%s"; filename="%s"', $name, basename($filename)) : "form-data; name=\"{$name}\""; } // Set a default content-length header if one was no provided $length = $this->getHeader($headers, 'content-length'); if (!$length) { if ($length = $stream->getSize()) { $headers['Content-Length'] = (string) $length; } } // Set a default Content-Type if one was not supplied $type = $this->getHeader($headers, 'content-type'); if (!$type && ($filename === '0' || $filename)) { if ($type = mimetype_from_filename($filename)) { $headers['Content-Type'] = $type; } } return [$stream, $headers]; } private function getHeader(array $headers, $key) { $lowercaseHeader = strtolower($key); foreach ($headers as $k => $v) { if (strtolower($k) === $lowercaseHeader) { return $v; } } return null; } } <?php namespace GuzzleHttp\Psr7; use InvalidArgumentException; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UriInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UploadedFileInterface; /** * Server-side HTTP request * * Extends the Request definition to add methods for accessing incoming data, * specifically server parameters, cookies, matched path parameters, query * string arguments, body parameters, and upload file information. * * "Attributes" are discovered via decomposing the request (and usually * specifically the URI path), and typically will be injected by the application. * * Requests are considered immutable; all methods that might change state are * implemented such that they retain the internal state of the current * message and return a new instance that contains the changed state. */ class ServerRequest extends Request implements ServerRequestInterface { /** * @var array */ private $attributes = []; /** * @var array */ private $cookieParams = []; /** * @var null|array|object */ private $parsedBody; /** * @var array */ private $queryParams = []; /** * @var array */ private $serverParams; /** * @var array */ private $uploadedFiles = []; /** * @param string $method HTTP method * @param string|UriInterface $uri URI * @param array $headers Request headers * @param string|null|resource|StreamInterface $body Request body * @param string $version Protocol version * @param array $serverParams Typically the $_SERVER superglobal */ public function __construct( $method, $uri, array $headers = [], $body = null, $version = '1.1', array $serverParams = [] ) { $this->serverParams = $serverParams; parent::__construct($method, $uri, $headers, $body, $version); } /** * Return an UploadedFile instance array. * * @param array $files A array which respect $_FILES structure * @throws InvalidArgumentException for unrecognized values * @return array */ public static function normalizeFiles(array $files) { $normalized = []; foreach ($files as $key => $value) { if ($value instanceof UploadedFileInterface) { $normalized[$key] = $value; } elseif (is_array($value) && isset($value['tmp_name'])) { $normalized[$key] = self::createUploadedFileFromSpec($value); } elseif (is_array($value)) { $normalized[$key] = self::normalizeFiles($value); continue; } else { throw new InvalidArgumentException('Invalid value in files specification'); } } return $normalized; } /** * Create and return an UploadedFile instance from a $_FILES specification. * * If the specification represents an array of values, this method will * delegate to normalizeNestedFileSpec() and return that return value. * * @param array $value $_FILES struct * @return array|UploadedFileInterface */ private static function createUploadedFileFromSpec(array $value) { if (is_array($value['tmp_name'])) { return self::normalizeNestedFileSpec($value); } return new UploadedFile( $value['tmp_name'], (int) $value['size'], (int) $value['error'], $value['name'], $value['type'] ); } /** * Normalize an array of file specifications. * * Loops through all nested files and returns a normalized array of * UploadedFileInterface instances. * * @param array $files * @return UploadedFileInterface[] */ private static function normalizeNestedFileSpec(array $files = []) { $normalizedFiles = []; foreach (array_keys($files['tmp_name']) as $key) { $spec = [ 'tmp_name' => $files['tmp_name'][$key], 'size' => $files['size'][$key], 'error' => $files['error'][$key], 'name' => $files['name'][$key], 'type' => $files['type'][$key], ]; $normalizedFiles[$key] = self::createUploadedFileFromSpec($spec); } return $normalizedFiles; } /** * Return a ServerRequest populated with superglobals: * $_GET * $_POST * $_COOKIE * $_FILES * $_SERVER * * @return ServerRequestInterface */ public static function fromGlobals() { $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; $headers = getallheaders(); $uri = self::getUriFromGlobals(); $body = new CachingStream(new LazyOpenStream('php://input', 'r+')); $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1'; $serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER); return $serverRequest ->withCookieParams($_COOKIE) ->withQueryParams($_GET) ->withParsedBody($_POST) ->withUploadedFiles(self::normalizeFiles($_FILES)); } private static function extractHostAndPortFromAuthority($authority) { $uri = 'http://'.$authority; $parts = parse_url($uri); if (false === $parts) { return [null, null]; } $host = isset($parts['host']) ? $parts['host'] : null; $port = isset($parts['port']) ? $parts['port'] : null; return [$host, $port]; } /** * Get a Uri populated with values from $_SERVER. * * @return UriInterface */ public static function getUriFromGlobals() { $uri = new Uri(''); $uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http'); $hasPort = false; if (isset($_SERVER['HTTP_HOST'])) { list($host, $port) = self::extractHostAndPortFromAuthority($_SERVER['HTTP_HOST']); if ($host !== null) { $uri = $uri->withHost($host); } if ($port !== null) { $hasPort = true; $uri = $uri->withPort($port); } } elseif (isset($_SERVER['SERVER_NAME'])) { $uri = $uri->withHost($_SERVER['SERVER_NAME']); } elseif (isset($_SERVER['SERVER_ADDR'])) { $uri = $uri->withHost($_SERVER['SERVER_ADDR']); } if (!$hasPort && isset($_SERVER['SERVER_PORT'])) { $uri = $uri->withPort($_SERVER['SERVER_PORT']); } $hasQuery = false; if (isset($_SERVER['REQUEST_URI'])) { $requestUriParts = explode('?', $_SERVER['REQUEST_URI'], 2); $uri = $uri->withPath($requestUriParts[0]); if (isset($requestUriParts[1])) { $hasQuery = true; $uri = $uri->withQuery($requestUriParts[1]); } } if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) { $uri = $uri->withQuery($_SERVER['QUERY_STRING']); } return $uri; } /** * {@inheritdoc} */ public function getServerParams() { return $this->serverParams; } /** * {@inheritdoc} */ public function getUploadedFiles() { return $this->uploadedFiles; } /** * {@inheritdoc} */ public function withUploadedFiles(array $uploadedFiles) { $new = clone $this; $new->uploadedFiles = $uploadedFiles; return $new; } /** * {@inheritdoc} */ public function getCookieParams() { return $this->cookieParams; } /** * {@inheritdoc} */ public function withCookieParams(array $cookies) { $new = clone $this; $new->cookieParams = $cookies; return $new; } /** * {@inheritdoc} */ public function getQueryParams() { return $this->queryParams; } /** * {@inheritdoc} */ public function withQueryParams(array $query) { $new = clone $this; $new->queryParams = $query; return $new; } /** * {@inheritdoc} */ public function getParsedBody() { return $this->parsedBody; } /** * {@inheritdoc} */ public function withParsedBody($data) { $new = clone $this; $new->parsedBody = $data; return $new; } /** * {@inheritdoc} */ public function getAttributes() { return $this->attributes; } /** * {@inheritdoc} */ public function getAttribute($attribute, $default = null) { if (false === array_key_exists($attribute, $this->attributes)) { return $default; } return $this->attributes[$attribute]; } /** * {@inheritdoc} */ public function withAttribute($attribute, $value) { $new = clone $this; $new->attributes[$attribute] = $value; return $new; } /** * {@inheritdoc} */ public function withoutAttribute($attribute) { if (false === array_key_exists($attribute, $this->attributes)) { return $this; } $new = clone $this; unset($new->attributes[$attribute]); return $new; } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Decorator used to return only a subset of a stream */ class LimitStream implements StreamInterface { use StreamDecoratorTrait; /** @var int Offset to start reading from */ private $offset; /** @var int Limit the number of bytes that can be read */ private $limit; /** * @param StreamInterface $stream Stream to wrap * @param int $limit Total number of bytes to allow to be read * from the stream. Pass -1 for no limit. * @param int $offset Position to seek to before reading (only * works on seekable streams). */ public function __construct( StreamInterface $stream, $limit = -1, $offset = 0 ) { $this->stream = $stream; $this->setLimit($limit); $this->setOffset($offset); } public function eof() { // Always return true if the underlying stream is EOF if ($this->stream->eof()) { return true; } // No limit and the underlying stream is not at EOF if ($this->limit == -1) { return false; } return $this->stream->tell() >= $this->offset + $this->limit; } /** * Returns the size of the limited subset of data * {@inheritdoc} */ public function getSize() { if (null === ($length = $this->stream->getSize())) { return null; } elseif ($this->limit == -1) { return $length - $this->offset; } else { return min($this->limit, $length - $this->offset); } } /** * Allow for a bounded seek on the read limited stream * {@inheritdoc} */ public function seek($offset, $whence = SEEK_SET) { if ($whence !== SEEK_SET || $offset < 0) { throw new \RuntimeException(sprintf( 'Cannot seek to offset %s with whence %s', $offset, $whence )); } $offset += $this->offset; if ($this->limit !== -1) { if ($offset > $this->offset + $this->limit) { $offset = $this->offset + $this->limit; } } $this->stream->seek($offset); } /** * Give a relative tell() * {@inheritdoc} */ public function tell() { return $this->stream->tell() - $this->offset; } /** * Set the offset to start limiting from * * @param int $offset Offset to seek to and begin byte limiting from * * @throws \RuntimeException if the stream cannot be seeked. */ public function setOffset($offset) { $current = $this->stream->tell(); if ($current !== $offset) { // If the stream cannot seek to the offset position, then read to it if ($this->stream->isSeekable()) { $this->stream->seek($offset); } elseif ($current > $offset) { throw new \RuntimeException("Could not seek to stream offset $offset"); } else { $this->stream->read($offset - $current); } } $this->offset = $offset; } /** * Set the limit of bytes that the decorator allows to be read from the * stream. * * @param int $limit Number of bytes to allow to be read from the stream. * Use -1 for no limit. */ public function setLimit($limit) { $this->limit = $limit; } public function read($length) { if ($this->limit == -1) { return $this->stream->read($length); } // Check if the current position is less than the total allowed // bytes + original offset $remaining = ($this->offset + $this->limit) - $this->stream->tell(); if ($remaining > 0) { // Only return the amount of requested data, ensuring that the byte // limit is not exceeded return $this->stream->read(min($remaining, $length)); } return ''; } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Uses PHP's zlib.inflate filter to inflate deflate or gzipped content. * * This stream decorator skips the first 10 bytes of the given stream to remove * the gzip header, converts the provided stream to a PHP stream resource, * then appends the zlib.inflate filter. The stream is then converted back * to a Guzzle stream resource to be used as a Guzzle stream. * * @link http://tools.ietf.org/html/rfc1952 * @link http://php.net/manual/en/filters.compression.php */ class InflateStream implements StreamInterface { use StreamDecoratorTrait; public function __construct(StreamInterface $stream) { // read the first 10 bytes, ie. gzip header $header = $stream->read(10); $filenameHeaderLength = $this->getLengthOfPossibleFilenameHeader($stream, $header); // Skip the header, that is 10 + length of filename + 1 (nil) bytes $stream = new LimitStream($stream, -1, 10 + $filenameHeaderLength); $resource = StreamWrapper::getResource($stream); stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ); $this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource)); } /** * @param StreamInterface $stream * @param $header * @return int */ private function getLengthOfPossibleFilenameHeader(StreamInterface $stream, $header) { $filename_header_length = 0; if (substr(bin2hex($header), 6, 2) === '08') { // we have a filename, read until nil $filename_header_length = 1; while ($stream->read(1) !== chr(0)) { $filename_header_length++; } } return $filename_header_length; } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\UriInterface; /** * PSR-7 URI implementation. * * @author Michael Dowling * @author Tobias Schultze * @author Matthew Weier O'Phinney */ class Uri implements UriInterface { /** * Absolute http and https URIs require a host per RFC 7230 Section 2.7 * but in generic URIs the host can be empty. So for http(s) URIs * we apply this default host when no host is given yet to form a * valid URI. */ const HTTP_DEFAULT_HOST = 'localhost'; private static $defaultPorts = [ 'http' => 80, 'https' => 443, 'ftp' => 21, 'gopher' => 70, 'nntp' => 119, 'news' => 119, 'telnet' => 23, 'tn3270' => 23, 'imap' => 143, 'pop' => 110, 'ldap' => 389, ]; private static $charUnreserved = 'a-zA-Z0-9_\-\.~'; private static $charSubDelims = '!\$&\'\(\)\*\+,;='; private static $replaceQuery = ['=' => '%3D', '&' => '%26']; /** @var string Uri scheme. */ private $scheme = ''; /** @var string Uri user info. */ private $userInfo = ''; /** @var string Uri host. */ private $host = ''; /** @var int|null Uri port. */ private $port; /** @var string Uri path. */ private $path = ''; /** @var string Uri query string. */ private $query = ''; /** @var string Uri fragment. */ private $fragment = ''; /** * @param string $uri URI to parse */ public function __construct($uri = '') { // weak type check to also accept null until we can add scalar type hints if ($uri != '') { $parts = parse_url($uri); if ($parts === false) { throw new \InvalidArgumentException("Unable to parse URI: $uri"); } $this->applyParts($parts); } } public function __toString() { return self::composeComponents( $this->scheme, $this->getAuthority(), $this->path, $this->query, $this->fragment ); } /** * Composes a URI reference string from its various components. * * Usually this method does not need to be called manually but instead is used indirectly via * `Psr\Http\Message\UriInterface::__toString`. * * PSR-7 UriInterface treats an empty component the same as a missing component as * getQuery(), getFragment() etc. always return a string. This explains the slight * difference to RFC 3986 Section 5.3. * * Another adjustment is that the authority separator is added even when the authority is missing/empty * for the "file" scheme. This is because PHP stream functions like `file_get_contents` only work with * `file:///myfile` but not with `file:/myfile` although they are equivalent according to RFC 3986. But * `file:///` is the more common syntax for the file scheme anyway (Chrome for example redirects to * that format). * * @param string $scheme * @param string $authority * @param string $path * @param string $query * @param string $fragment * * @return string * * @link https://tools.ietf.org/html/rfc3986#section-5.3 */ public static function composeComponents($scheme, $authority, $path, $query, $fragment) { $uri = ''; // weak type checks to also accept null until we can add scalar type hints if ($scheme != '') { $uri .= $scheme . ':'; } if ($authority != ''|| $scheme === 'file') { $uri .= '//' . $authority; } $uri .= $path; if ($query != '') { $uri .= '?' . $query; } if ($fragment != '') { $uri .= '#' . $fragment; } return $uri; } /** * Whether the URI has the default port of the current scheme. * * `Psr\Http\Message\UriInterface::getPort` may return null or the standard port. This method can be used * independently of the implementation. * * @param UriInterface $uri * * @return bool */ public static function isDefaultPort(UriInterface $uri) { return $uri->getPort() === null || (isset(self::$defaultPorts[$uri->getScheme()]) && $uri->getPort() === self::$defaultPorts[$uri->getScheme()]); } /** * Whether the URI is absolute, i.e. it has a scheme. * * An instance of UriInterface can either be an absolute URI or a relative reference. This method returns true * if it is the former. An absolute URI has a scheme. A relative reference is used to express a URI relative * to another URI, the base URI. Relative references can be divided into several forms: * - network-path references, e.g. '//example.com/path' * - absolute-path references, e.g. '/path' * - relative-path references, e.g. 'subpath' * * @param UriInterface $uri * * @return bool * @see Uri::isNetworkPathReference * @see Uri::isAbsolutePathReference * @see Uri::isRelativePathReference * @link https://tools.ietf.org/html/rfc3986#section-4 */ public static function isAbsolute(UriInterface $uri) { return $uri->getScheme() !== ''; } /** * Whether the URI is a network-path reference. * * A relative reference that begins with two slash characters is termed an network-path reference. * * @param UriInterface $uri * * @return bool * @link https://tools.ietf.org/html/rfc3986#section-4.2 */ public static function isNetworkPathReference(UriInterface $uri) { return $uri->getScheme() === '' && $uri->getAuthority() !== ''; } /** * Whether the URI is a absolute-path reference. * * A relative reference that begins with a single slash character is termed an absolute-path reference. * * @param UriInterface $uri * * @return bool * @link https://tools.ietf.org/html/rfc3986#section-4.2 */ public static function isAbsolutePathReference(UriInterface $uri) { return $uri->getScheme() === '' && $uri->getAuthority() === '' && isset($uri->getPath()[0]) && $uri->getPath()[0] === '/'; } /** * Whether the URI is a relative-path reference. * * A relative reference that does not begin with a slash character is termed a relative-path reference. * * @param UriInterface $uri * * @return bool * @link https://tools.ietf.org/html/rfc3986#section-4.2 */ public static function isRelativePathReference(UriInterface $uri) { return $uri->getScheme() === '' && $uri->getAuthority() === '' && (!isset($uri->getPath()[0]) || $uri->getPath()[0] !== '/'); } /** * Whether the URI is a same-document reference. * * A same-document reference refers to a URI that is, aside from its fragment * component, identical to the base URI. When no base URI is given, only an empty * URI reference (apart from its fragment) is considered a same-document reference. * * @param UriInterface $uri The URI to check * @param UriInterface|null $base An optional base URI to compare against * * @return bool * @link https://tools.ietf.org/html/rfc3986#section-4.4 */ public static function isSameDocumentReference(UriInterface $uri, UriInterface $base = null) { if ($base !== null) { $uri = UriResolver::resolve($base, $uri); return ($uri->getScheme() === $base->getScheme()) && ($uri->getAuthority() === $base->getAuthority()) && ($uri->getPath() === $base->getPath()) && ($uri->getQuery() === $base->getQuery()); } return $uri->getScheme() === '' && $uri->getAuthority() === '' && $uri->getPath() === '' && $uri->getQuery() === ''; } /** * Removes dot segments from a path and returns the new path. * * @param string $path * * @return string * * @deprecated since version 1.4. Use UriResolver::removeDotSegments instead. * @see UriResolver::removeDotSegments */ public static function removeDotSegments($path) { return UriResolver::removeDotSegments($path); } /** * Converts the relative URI into a new URI that is resolved against the base URI. * * @param UriInterface $base Base URI * @param string|UriInterface $rel Relative URI * * @return UriInterface * * @deprecated since version 1.4. Use UriResolver::resolve instead. * @see UriResolver::resolve */ public static function resolve(UriInterface $base, $rel) { if (!($rel instanceof UriInterface)) { $rel = new self($rel); } return UriResolver::resolve($base, $rel); } /** * Creates a new URI with a specific query string value removed. * * Any existing query string values that exactly match the provided key are * removed. * * @param UriInterface $uri URI to use as a base. * @param string $key Query string key to remove. * * @return UriInterface */ public static function withoutQueryValue(UriInterface $uri, $key) { $result = self::getFilteredQueryString($uri, [$key]); return $uri->withQuery(implode('&', $result)); } /** * Creates a new URI with a specific query string value. * * Any existing query string values that exactly match the provided key are * removed and replaced with the given key value pair. * * A value of null will set the query string key without a value, e.g. "key" * instead of "key=value". * * @param UriInterface $uri URI to use as a base. * @param string $key Key to set. * @param string|null $value Value to set * * @return UriInterface */ public static function withQueryValue(UriInterface $uri, $key, $value) { $result = self::getFilteredQueryString($uri, [$key]); $result[] = self::generateQueryString($key, $value); return $uri->withQuery(implode('&', $result)); } /** * Creates a new URI with multiple specific query string values. * * It has the same behavior as withQueryValue() but for an associative array of key => value. * * @param UriInterface $uri URI to use as a base. * @param array $keyValueArray Associative array of key and values * * @return UriInterface */ public static function withQueryValues(UriInterface $uri, array $keyValueArray) { $result = self::getFilteredQueryString($uri, array_keys($keyValueArray)); foreach ($keyValueArray as $key => $value) { $result[] = self::generateQueryString($key, $value); } return $uri->withQuery(implode('&', $result)); } /** * Creates a URI from a hash of `parse_url` components. * * @param array $parts * * @return UriInterface * @link http://php.net/manual/en/function.parse-url.php * * @throws \InvalidArgumentException If the components do not form a valid URI. */ public static function fromParts(array $parts) { $uri = new self(); $uri->applyParts($parts); $uri->validateState(); return $uri; } public function getScheme() { return $this->scheme; } public function getAuthority() { $authority = $this->host; if ($this->userInfo !== '') { $authority = $this->userInfo . '@' . $authority; } if ($this->port !== null) { $authority .= ':' . $this->port; } return $authority; } public function getUserInfo() { return $this->userInfo; } public function getHost() { return $this->host; } public function getPort() { return $this->port; } public function getPath() { return $this->path; } public function getQuery() { return $this->query; } public function getFragment() { return $this->fragment; } public function withScheme($scheme) { $scheme = $this->filterScheme($scheme); if ($this->scheme === $scheme) { return $this; } $new = clone $this; $new->scheme = $scheme; $new->removeDefaultPort(); $new->validateState(); return $new; } public function withUserInfo($user, $password = null) { $info = $this->filterUserInfoComponent($user); if ($password !== null) { $info .= ':' . $this->filterUserInfoComponent($password); } if ($this->userInfo === $info) { return $this; } $new = clone $this; $new->userInfo = $info; $new->validateState(); return $new; } public function withHost($host) { $host = $this->filterHost($host); if ($this->host === $host) { return $this; } $new = clone $this; $new->host = $host; $new->validateState(); return $new; } public function withPort($port) { $port = $this->filterPort($port); if ($this->port === $port) { return $this; } $new = clone $this; $new->port = $port; $new->removeDefaultPort(); $new->validateState(); return $new; } public function withPath($path) { $path = $this->filterPath($path); if ($this->path === $path) { return $this; } $new = clone $this; $new->path = $path; $new->validateState(); return $new; } public function withQuery($query) { $query = $this->filterQueryAndFragment($query); if ($this->query === $query) { return $this; } $new = clone $this; $new->query = $query; return $new; } public function withFragment($fragment) { $fragment = $this->filterQueryAndFragment($fragment); if ($this->fragment === $fragment) { return $this; } $new = clone $this; $new->fragment = $fragment; return $new; } /** * Apply parse_url parts to a URI. * * @param array $parts Array of parse_url parts to apply. */ private function applyParts(array $parts) { $this->scheme = isset($parts['scheme']) ? $this->filterScheme($parts['scheme']) : ''; $this->userInfo = isset($parts['user']) ? $this->filterUserInfoComponent($parts['user']) : ''; $this->host = isset($parts['host']) ? $this->filterHost($parts['host']) : ''; $this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null; $this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : ''; $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; $this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : ''; if (isset($parts['pass'])) { $this->userInfo .= ':' . $this->filterUserInfoComponent($parts['pass']); } $this->removeDefaultPort(); } /** * @param string $scheme * * @return string * * @throws \InvalidArgumentException If the scheme is invalid. */ private function filterScheme($scheme) { if (!is_string($scheme)) { throw new \InvalidArgumentException('Scheme must be a string'); } return strtolower($scheme); } /** * @param string $component * * @return string * * @throws \InvalidArgumentException If the user info is invalid. */ private function filterUserInfoComponent($component) { if (!is_string($component)) { throw new \InvalidArgumentException('User info must be a string'); } return preg_replace_callback( '/(?:[^%' . self::$charUnreserved . self::$charSubDelims . ']+|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $component ); } /** * @param string $host * * @return string * * @throws \InvalidArgumentException If the host is invalid. */ private function filterHost($host) { if (!is_string($host)) { throw new \InvalidArgumentException('Host must be a string'); } return strtolower($host); } /** * @param int|null $port * * @return int|null * * @throws \InvalidArgumentException If the port is invalid. */ private function filterPort($port) { if ($port === null) { return null; } $port = (int) $port; if (0 > $port || 0xffff < $port) { throw new \InvalidArgumentException( sprintf('Invalid port: %d. Must be between 0 and 65535', $port) ); } return $port; } /** * @param UriInterface $uri * @param array $keys * * @return array */ private static function getFilteredQueryString(UriInterface $uri, array $keys) { $current = $uri->getQuery(); if ($current === '') { return []; } $decodedKeys = array_map('rawurldecode', $keys); return array_filter(explode('&', $current), function ($part) use ($decodedKeys) { return !in_array(rawurldecode(explode('=', $part)[0]), $decodedKeys, true); }); } /** * @param string $key * @param string|null $value * * @return string */ private static function generateQueryString($key, $value) { // Query string separators ("=", "&") within the key or value need to be encoded // (while preventing double-encoding) before setting the query string. All other // chars that need percent-encoding will be encoded by withQuery(). $queryString = strtr($key, self::$replaceQuery); if ($value !== null) { $queryString .= '=' . strtr($value, self::$replaceQuery); } return $queryString; } private function removeDefaultPort() { if ($this->port !== null && self::isDefaultPort($this)) { $this->port = null; } } /** * Filters the path of a URI * * @param string $path * * @return string * * @throws \InvalidArgumentException If the path is invalid. */ private function filterPath($path) { if (!is_string($path)) { throw new \InvalidArgumentException('Path must be a string'); } return preg_replace_callback( '/(?:[^' . self::$charUnreserved . self::$charSubDelims . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $path ); } /** * Filters the query string or fragment of a URI. * * @param string $str * * @return string * * @throws \InvalidArgumentException If the query or fragment is invalid. */ private function filterQueryAndFragment($str) { if (!is_string($str)) { throw new \InvalidArgumentException('Query and fragment must be a string'); } return preg_replace_callback( '/(?:[^' . self::$charUnreserved . self::$charSubDelims . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', [$this, 'rawurlencodeMatchZero'], $str ); } private function rawurlencodeMatchZero(array $match) { return rawurlencode($match[0]); } private function validateState() { if ($this->host === '' && ($this->scheme === 'http' || $this->scheme === 'https')) { $this->host = self::HTTP_DEFAULT_HOST; } if ($this->getAuthority() === '') { if (0 === strpos($this->path, '//')) { throw new \InvalidArgumentException('The path of a URI without an authority must not start with two slashes "//"'); } if ($this->scheme === '' && false !== strpos(explode('/', $this->path, 2)[0], ':')) { throw new \InvalidArgumentException('A relative URI must not have a path beginning with a segment containing a colon'); } } elseif (isset($this->path[0]) && $this->path[0] !== '/') { @trigger_error( 'The path of a URI with an authority must start with a slash "/" or be empty. Automagically fixing the URI ' . 'by adding a leading slash to the path is deprecated since version 1.4 and will throw an exception instead.', E_USER_DEPRECATED ); $this->path = '/'. $this->path; //throw new \InvalidArgumentException('The path of a URI with an authority must start with a slash "/" or be empty'); } } } <?php namespace GuzzleHttp\Psr7; final class Rfc7230 { /** * Header related regular expressions (copied from amphp/http package) * (Note: once we require PHP 7.x we could just depend on the upstream package) * * Note: header delimiter (\r\n) is modified to \r?\n to accept line feed only delimiters for BC reasons. * * @link https://github.com/amphp/http/blob/v1.0.1/src/Rfc7230.php#L12-L15 * @license https://github.com/amphp/http/blob/v1.0.1/LICENSE */ const HEADER_REGEX = "(^([^()<>@,;:\\\"/[\]?={}\x01-\x20\x7F]++):[ \t]*+((?:[ \t]*+[\x21-\x7E\x80-\xFF]++)*+)[ \t]*+\r?\n)m"; const HEADER_FOLD_REGEX = "(\r?\n[ \t]++)"; } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\UriInterface; /** * Provides methods to normalize and compare URIs. * * @author Tobias Schultze * * @link https://tools.ietf.org/html/rfc3986#section-6 */ final class UriNormalizer { /** * Default normalizations which only include the ones that preserve semantics. * * self::CAPITALIZE_PERCENT_ENCODING | self::DECODE_UNRESERVED_CHARACTERS | self::CONVERT_EMPTY_PATH | * self::REMOVE_DEFAULT_HOST | self::REMOVE_DEFAULT_PORT | self::REMOVE_DOT_SEGMENTS */ const PRESERVING_NORMALIZATIONS = 63; /** * All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized. * * Example: http://example.org/a%c2%b1b → http://example.org/a%C2%B1b */ const CAPITALIZE_PERCENT_ENCODING = 1; /** * Decodes percent-encoded octets of unreserved characters. * * For consistency, percent-encoded octets in the ranges of ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39), * hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and, * when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers. * * Example: http://example.org/%7Eusern%61me/ → http://example.org/~username/ */ const DECODE_UNRESERVED_CHARACTERS = 2; /** * Converts the empty path to "/" for http and https URIs. * * Example: http://example.org → http://example.org/ */ const CONVERT_EMPTY_PATH = 4; /** * Removes the default host of the given URI scheme from the URI. * * Only the "file" scheme defines the default host "localhost". * All of `file:/myfile`, `file:///myfile`, and `file://localhost/myfile` * are equivalent according to RFC 3986. The first format is not accepted * by PHPs stream functions and thus already normalized implicitly to the * second format in the Uri class. See `GuzzleHttp\Psr7\Uri::composeComponents`. * * Example: file://localhost/myfile → file:///myfile */ const REMOVE_DEFAULT_HOST = 8; /** * Removes the default port of the given URI scheme from the URI. * * Example: http://example.org:80/ → http://example.org/ */ const REMOVE_DEFAULT_PORT = 16; /** * Removes unnecessary dot-segments. * * Dot-segments in relative-path references are not removed as it would * change the semantics of the URI reference. * * Example: http://example.org/../a/b/../c/./d.html → http://example.org/a/c/d.html */ const REMOVE_DOT_SEGMENTS = 32; /** * Paths which include two or more adjacent slashes are converted to one. * * Webservers usually ignore duplicate slashes and treat those URIs equivalent. * But in theory those URIs do not need to be equivalent. So this normalization * may change the semantics. Encoded slashes (%2F) are not removed. * * Example: http://example.org//foo///bar.html → http://example.org/foo/bar.html */ const REMOVE_DUPLICATE_SLASHES = 64; /** * Sort query parameters with their values in alphabetical order. * * However, the order of parameters in a URI may be significant (this is not defined by the standard). * So this normalization is not safe and may change the semantics of the URI. * * Example: ?lang=en&article=fred → ?article=fred&lang=en * * Note: The sorting is neither locale nor Unicode aware (the URI query does not get decoded at all) as the * purpose is to be able to compare URIs in a reproducible way, not to have the params sorted perfectly. */ const SORT_QUERY_PARAMETERS = 128; /** * Returns a normalized URI. * * The scheme and host component are already normalized to lowercase per PSR-7 UriInterface. * This methods adds additional normalizations that can be configured with the $flags parameter. * * PSR-7 UriInterface cannot distinguish between an empty component and a missing component as * getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are * treated equivalent which is not necessarily true according to RFC 3986. But that difference * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well. * * @param UriInterface $uri The URI to normalize * @param int $flags A bitmask of normalizations to apply, see constants * * @return UriInterface The normalized URI * @link https://tools.ietf.org/html/rfc3986#section-6.2 */ public static function normalize(UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS) { if ($flags & self::CAPITALIZE_PERCENT_ENCODING) { $uri = self::capitalizePercentEncoding($uri); } if ($flags & self::DECODE_UNRESERVED_CHARACTERS) { $uri = self::decodeUnreservedCharacters($uri); } if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' && ($uri->getScheme() === 'http' || $uri->getScheme() === 'https') ) { $uri = $uri->withPath('/'); } if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') { $uri = $uri->withHost(''); } if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && Uri::isDefaultPort($uri)) { $uri = $uri->withPort(null); } if ($flags & self::REMOVE_DOT_SEGMENTS && !Uri::isRelativePathReference($uri)) { $uri = $uri->withPath(UriResolver::removeDotSegments($uri->getPath())); } if ($flags & self::REMOVE_DUPLICATE_SLASHES) { $uri = $uri->withPath(preg_replace('#//++#', '/', $uri->getPath())); } if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') { $queryKeyValues = explode('&', $uri->getQuery()); sort($queryKeyValues); $uri = $uri->withQuery(implode('&', $queryKeyValues)); } return $uri; } /** * Whether two URIs can be considered equivalent. * * Both URIs are normalized automatically before comparison with the given $normalizations bitmask. The method also * accepts relative URI references and returns true when they are equivalent. This of course assumes they will be * resolved against the same base URI. If this is not the case, determination of equivalence or difference of * relative references does not mean anything. * * @param UriInterface $uri1 An URI to compare * @param UriInterface $uri2 An URI to compare * @param int $normalizations A bitmask of normalizations to apply, see constants * * @return bool * @link https://tools.ietf.org/html/rfc3986#section-6.1 */ public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, $normalizations = self::PRESERVING_NORMALIZATIONS) { return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations); } private static function capitalizePercentEncoding(UriInterface $uri) { $regex = '/(?:%[A-Fa-f0-9]{2})++/'; $callback = function (array $match) { return strtoupper($match[0]); }; return $uri->withPath( preg_replace_callback($regex, $callback, $uri->getPath()) )->withQuery( preg_replace_callback($regex, $callback, $uri->getQuery()) ); } private static function decodeUnreservedCharacters(UriInterface $uri) { $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; $callback = function (array $match) { return rawurldecode($match[0]); }; return $uri->withPath( preg_replace_callback($regex, $callback, $uri->getPath()) )->withQuery( preg_replace_callback($regex, $callback, $uri->getQuery()) ); } private function __construct() { // cannot be instantiated } } <?php namespace GuzzleHttp\Psr7; use InvalidArgumentException; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; /** * PSR-7 request implementation. */ class Request implements RequestInterface { use MessageTrait; /** @var string */ private $method; /** @var null|string */ private $requestTarget; /** @var UriInterface */ private $uri; /** * @param string $method HTTP method * @param string|UriInterface $uri URI * @param array $headers Request headers * @param string|null|resource|StreamInterface $body Request body * @param string $version Protocol version */ public function __construct( $method, $uri, array $headers = [], $body = null, $version = '1.1' ) { $this->assertMethod($method); if (!($uri instanceof UriInterface)) { $uri = new Uri($uri); } $this->method = strtoupper($method); $this->uri = $uri; $this->setHeaders($headers); $this->protocol = $version; if (!isset($this->headerNames['host'])) { $this->updateHostFromUri(); } if ($body !== '' && $body !== null) { $this->stream = stream_for($body); } } public function getRequestTarget() { if ($this->requestTarget !== null) { return $this->requestTarget; } $target = $this->uri->getPath(); if ($target == '') { $target = '/'; } if ($this->uri->getQuery() != '') { $target .= '?' . $this->uri->getQuery(); } return $target; } public function withRequestTarget($requestTarget) { if (preg_match('#\s#', $requestTarget)) { throw new InvalidArgumentException( 'Invalid request target provided; cannot contain whitespace' ); } $new = clone $this; $new->requestTarget = $requestTarget; return $new; } public function getMethod() { return $this->method; } public function withMethod($method) { $this->assertMethod($method); $new = clone $this; $new->method = strtoupper($method); return $new; } public function getUri() { return $this->uri; } public function withUri(UriInterface $uri, $preserveHost = false) { if ($uri === $this->uri) { return $this; } $new = clone $this; $new->uri = $uri; if (!$preserveHost || !isset($this->headerNames['host'])) { $new->updateHostFromUri(); } return $new; } private function updateHostFromUri() { $host = $this->uri->getHost(); if ($host == '') { return; } if (($port = $this->uri->getPort()) !== null) { $host .= ':' . $port; } if (isset($this->headerNames['host'])) { $header = $this->headerNames['host']; } else { $header = 'Host'; $this->headerNames['host'] = 'Host'; } // Ensure Host is the first header. // See: http://tools.ietf.org/html/rfc7230#section-5.4 $this->headers = [$header => [$host]] + $this->headers; } private function assertMethod($method) { if (!is_string($method) || $method === '') { throw new \InvalidArgumentException('Method must be a non-empty string.'); } } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Stream decorator that begins dropping data once the size of the underlying * stream becomes too full. */ class DroppingStream implements StreamInterface { use StreamDecoratorTrait; private $maxLength; /** * @param StreamInterface $stream Underlying stream to decorate. * @param int $maxLength Maximum size before dropping data. */ public function __construct(StreamInterface $stream, $maxLength) { $this->stream = $stream; $this->maxLength = $maxLength; } public function write($string) { $diff = $this->maxLength - $this->stream->getSize(); // Begin returning 0 when the underlying stream is too large. if ($diff <= 0) { return 0; } // Write the stream or a subset of the stream if needed. if (strlen($string) < $diff) { return $this->stream->write($string); } return $this->stream->write(substr($string, 0, $diff)); } } <?php namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; /** * Trait implementing functionality common to requests and responses. */ trait MessageTrait { /** @var array Map of all registered headers, as original name => array of values */ private $headers = []; /** @var array Map of lowercase header name => original name at registration */ private $headerNames = []; /** @var string */ private $protocol = '1.1'; /** @var StreamInterface */ private $stream; public function getProtocolVersion() { return $this->protocol; } public function withProtocolVersion($version) { if ($this->protocol === $version) { return $this; } $new = clone $this; $new->protocol = $version; return $new; } public function getHeaders() { return $this->headers; } public function hasHeader($header) { return isset($this->headerNames[strtolower($header)]); } public function getHeader($header) { $header = strtolower($header); if (!isset($this->headerNames[$header])) { return []; } $header = $this->headerNames[$header]; return $this->headers[$header]; } public function getHeaderLine($header) { return implode(', ', $this->getHeader($header)); } public function withHeader($header, $value) { $this->assertHeader($header); $value = $this->normalizeHeaderValue($value); $normalized = strtolower($header); $new = clone $this; if (isset($new->headerNames[$normalized])) { unset($new->headers[$new->headerNames[$normalized]]); } $new->headerNames[$normalized] = $header; $new->headers[$header] = $value; return $new; } public function withAddedHeader($header, $value) { $this->assertHeader($header); $value = $this->normalizeHeaderValue($value); $normalized = strtolower($header); $new = clone $this; if (isset($new->headerNames[$normalized])) { $header = $this->headerNames[$normalized]; $new->headers[$header] = array_merge($this->headers[$header], $value); } else { $new->headerNames[$normalized] = $header; $new->headers[$header] = $value; } return $new; } public function withoutHeader($header) { $normalized = strtolower($header); if (!isset($this->headerNames[$normalized])) { return $this; } $header = $this->headerNames[$normalized]; $new = clone $this; unset($new->headers[$header], $new->headerNames[$normalized]); return $new; } public function getBody() { if (!$this->stream) { $this->stream = stream_for(''); } return $this->stream; } public function withBody(StreamInterface $body) { if ($body === $this->stream) { return $this; } $new = clone $this; $new->stream = $body; return $new; } private function setHeaders(array $headers) { $this->headerNames = $this->headers = []; foreach ($headers as $header => $value) { if (is_int($header)) { // Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec // and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass. $header = (string) $header; } $this->assertHeader($header); $value = $this->normalizeHeaderValue($value); $normalized = strtolower($header); if (isset($this->headerNames[$normalized])) { $header = $this->headerNames[$normalized]; $this->headers[$header] = array_merge($this->headers[$header], $value); } else { $this->headerNames[$normalized] = $header; $this->headers[$header] = $value; } } } private function normalizeHeaderValue($value) { if (!is_array($value)) { return $this->trimHeaderValues([$value]); } if (count($value) === 0) { throw new \InvalidArgumentException('Header value can not be an empty array.'); } return $this->trimHeaderValues($value); } /** * Trims whitespace from the header values. * * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. * * header-field = field-name ":" OWS field-value OWS * OWS = *( SP / HTAB ) * * @param string[] $values Header values * * @return string[] Trimmed header values * * @see https://tools.ietf.org/html/rfc7230#section-3.2.4 */ private function trimHeaderValues(array $values) { return array_map(function ($value) { if (!is_scalar($value) && null !== $value) { throw new \InvalidArgumentException(sprintf( 'Header value must be scalar or null but %s provided.', is_object($value) ? get_class($value) : gettype($value) )); } return trim((string) $value, " \t"); }, $values); } private function assertHeader($header) { if (!is_string($header)) { throw new \InvalidArgumentException(sprintf( 'Header name must be a string but %s provided.', is_object($header) ? get_class($header) : gettype($header) )); } if ($header === '') { throw new \InvalidArgumentException('Header name can not be empty.'); } } } # Change Log All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] ## [1.6.0] ### Added - Allowed version `^3.0` of `ralouphie/getallheaders` dependency (#244) - Added MIME type for WEBP image format (#246) - Added more validation of values according to PSR-7 and RFC standards, e.g. status code range (#250, #272) ### Changed - Tests don't pass with HHVM 4.0, so HHVM support got dropped. Other libraries like composer have done the same. (#262) - Accept port number 0 to be valid (#270) ### Fixed - Fixed subsequent reads from `php://input` in ServerRequest (#247) - Fixed readable/writable detection for certain stream modes (#248) - Fixed encoding of special characters in the `userInfo` component of an URI (#253) ## [1.5.2] - 2018-12-04 ### Fixed - Check body size when getting the message summary ## [1.5.1] - 2018-12-04 ### Fixed - Get the summary of a body only if it is readable ## [1.5.0] - 2018-12-03 ### Added - Response first-line to response string exception (fixes #145) - A test for #129 behavior - `get_message_body_summary` function in order to get the message summary - `3gp` and `mkv` mime types ### Changed - Clarify exception message when stream is detached ### Deprecated - Deprecated parsing folded header lines as per RFC 7230 ### Fixed - Fix `AppendStream::detach` to not close streams - `InflateStream` preserves `isSeekable` attribute of the underlying stream - `ServerRequest::getUriFromGlobals` to support URLs in query parameters Several other fixes and improvements. ## [1.4.2] - 2017-03-20 ### Fixed - Reverted BC break to `Uri::resolve` and `Uri::removeDotSegments` by removing calls to `trigger_error` when deprecated methods are invoked. ## [1.4.1] - 2017-02-27 ### Added - Rriggering of silenced deprecation warnings. ### Fixed - Reverted BC break by reintroducing behavior to automagically fix a URI with a relative path and an authority by adding a leading slash to the path. It's only deprecated now. ## [1.4.0] - 2017-02-21 ### Added - Added common URI utility methods based on RFC 3986 (see documentation in the readme): - `Uri::isDefaultPort` - `Uri::isAbsolute` - `Uri::isNetworkPathReference` - `Uri::isAbsolutePathReference` - `Uri::isRelativePathReference` - `Uri::isSameDocumentReference` - `Uri::composeComponents` - `UriNormalizer::normalize` - `UriNormalizer::isEquivalent` - `UriResolver::relativize` ### Changed - Ensure `ServerRequest::getUriFromGlobals` returns a URI in absolute form. - Allow `parse_response` to parse a response without delimiting space and reason. - Ensure each URI modification results in a valid URI according to PSR-7 discussions. Invalid modifications will throw an exception instead of returning a wrong URI or doing some magic. - `(new Uri)->withPath('foo')->withHost('example.com')` will throw an exception because the path of a URI with an authority must start with a slash "/" or be empty - `(new Uri())->withScheme('http')` will return `'http://localhost'` ### Deprecated - `Uri::resolve` in favor of `UriResolver::resolve` - `Uri::removeDotSegments` in favor of `UriResolver::removeDotSegments` ### Fixed - `Stream::read` when length parameter <= 0. - `copy_to_stream` reads bytes in chunks instead of `maxLen` into memory. - `ServerRequest::getUriFromGlobals` when `Host` header contains port. - Compatibility of URIs with `file` scheme and empty host. ## [1.3.1] - 2016-06-25 ### Fixed - `Uri::__toString` for network path references, e.g. `//example.org`. - Missing lowercase normalization for host. - Handling of URI components in case they are `'0'` in a lot of places, e.g. as a user info password. - `Uri::withAddedHeader` to correctly merge headers with different case. - Trimming of header values in `Uri::withAddedHeader`. Header values may be surrounded by whitespace which should be ignored according to RFC 7230 Section 3.2.4. This does not apply to header names. - `Uri::withAddedHeader` with an array of header values. - `Uri::resolve` when base path has no slash and handling of fragment. - Handling of encoding in `Uri::with(out)QueryValue` so one can pass the key/value both in encoded as well as decoded form to those methods. This is consistent with withPath, withQuery etc. - `ServerRequest::withoutAttribute` when attribute value is null. ## [1.3.0] - 2016-04-13 ### Added - Remaining interfaces needed for full PSR7 compatibility (ServerRequestInterface, UploadedFileInterface, etc.). - Support for stream_for from scalars. ### Changed - Can now extend Uri. ### Fixed - A bug in validating request methods by making it more permissive. ## [1.2.3] - 2016-02-18 ### Fixed - Support in `GuzzleHttp\Psr7\CachingStream` for seeking forward on remote streams, which can sometimes return fewer bytes than requested with `fread`. - Handling of gzipped responses with FNAME headers. ## [1.2.2] - 2016-01-22 ### Added - Support for URIs without any authority. - Support for HTTP 451 'Unavailable For Legal Reasons.' - Support for using '0' as a filename. - Support for including non-standard ports in Host headers. ## [1.2.1] - 2015-11-02 ### Changes - Now supporting negative offsets when seeking to SEEK_END. ## [1.2.0] - 2015-08-15 ### Changed - Body as `"0"` is now properly added to a response. - Now allowing forward seeking in CachingStream. - Now properly parsing HTTP requests that contain proxy targets in `parse_request`. - functions.php is now conditionally required. - user-info is no longer dropped when resolving URIs. ## [1.1.0] - 2015-06-24 ### Changed - URIs can now be relative. - `multipart/form-data` headers are now overridden case-insensitively. - URI paths no longer encode the following characters because they are allowed in URIs: "(", ")", "*", "!", "'" - A port is no longer added to a URI when the scheme is missing and no port is present. ## 1.0.0 - 2015-05-19 Initial release. Currently unsupported: - `Psr\Http\Message\ServerRequestInterface` - `Psr\Http\Message\UploadedFileInterface` [Unreleased]: https://github.com/guzzle/psr7/compare/1.6.0...HEAD [1.6.0]: https://github.com/guzzle/psr7/compare/1.5.2...1.6.0 [1.5.2]: https://github.com/guzzle/psr7/compare/1.5.1...1.5.2 [1.5.1]: https://github.com/guzzle/psr7/compare/1.5.0...1.5.1 [1.5.0]: https://github.com/guzzle/psr7/compare/1.4.2...1.5.0 [1.4.2]: https://github.com/guzzle/psr7/compare/1.4.1...1.4.2 [1.4.1]: https://github.com/guzzle/psr7/compare/1.4.0...1.4.1 [1.4.0]: https://github.com/guzzle/psr7/compare/1.3.1...1.4.0 [1.3.1]: https://github.com/guzzle/psr7/compare/1.3.0...1.3.1 [1.3.0]: https://github.com/guzzle/psr7/compare/1.2.3...1.3.0 [1.2.3]: https://github.com/guzzle/psr7/compare/1.2.2...1.2.3 [1.2.2]: https://github.com/guzzle/psr7/compare/1.2.1...1.2.2 [1.2.1]: https://github.com/guzzle/psr7/compare/1.2.0...1.2.1 [1.2.0]: https://github.com/guzzle/psr7/compare/1.1.0...1.2.0 [1.1.0]: https://github.com/guzzle/psr7/compare/1.0.0...1.1.0 { "name": "guzzlehttp/psr7", "type": "library", "description": "PSR-7 message implementation that also provides common utility methods", "keywords": ["request", "response", "message", "stream", "http", "uri", "url", "psr-7"], "license": "MIT", "authors": [ { "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" }, { "name": "Tobias Schultze", "homepage": "https://github.com/Tobion" } ], "require": { "php": ">=5.4.0", "psr/http-message": "~1.0", "ralouphie/getallheaders": "^2.0.5 || ^3.0.0" }, "require-dev": { "phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.8", "ext-zlib": "*" }, "provide": { "psr/http-message-implementation": "1.0" }, "suggest": { "zendframework/zend-httphandlerrunner": "Emit PSR-7 responses" }, "autoload": { "psr-4": { "GuzzleHttp\\Psr7\\": "src/" }, "files": ["src/functions_include.php"] }, "autoload-dev": { "psr-4": { "GuzzleHttp\\Tests\\Psr7\\": "tests/" } }, "extra": { "branch-alias": { "dev-master": "1.6-dev" } } } # PSR-7 Message Implementation This repository contains a full [PSR-7](http://www.php-fig.org/psr/psr-7/) message implementation, several stream decorators, and some helpful functionality like query string parsing. [](https://travis-ci.org/guzzle/psr7) # Stream implementation This package comes with a number of stream implementations and stream decorators. ## AppendStream `GuzzleHttp\Psr7\AppendStream` Reads from multiple streams, one after the other. ```php use GuzzleHttp\Psr7; $a = Psr7\stream_for('abc, '); $b = Psr7\stream_for('123.'); $composed = new Psr7\AppendStream([$a, $b]); $composed->addStream(Psr7\stream_for(' Above all listen to me')); echo $composed; // abc, 123. Above all listen to me. ``` ## BufferStream `GuzzleHttp\Psr7\BufferStream` Provides a buffer stream that can be written to fill a buffer, and read from to remove bytes from the buffer. This stream returns a "hwm" metadata value that tells upstream consumers what the configured high water mark of the stream is, or the maximum preferred size of the buffer. ```php use GuzzleHttp\Psr7; // When more than 1024 bytes are in the buffer, it will begin returning // false to writes. This is an indication that writers should slow down. $buffer = new Psr7\BufferStream(1024); ``` ## CachingStream The CachingStream is used to allow seeking over previously read bytes on non-seekable streams. This can be useful when transferring a non-seekable entity body fails due to needing to rewind the stream (for example, resulting from a redirect). Data that is read from the remote stream will be buffered in a PHP temp stream so that previously read bytes are cached first in memory, then on disk. ```php use GuzzleHttp\Psr7; $original = Psr7\stream_for(fopen('http://www.google.com', 'r')); $stream = new Psr7\CachingStream($original); $stream->read(1024); echo $stream->tell(); // 1024 $stream->seek(0); echo $stream->tell(); // 0 ``` ## DroppingStream `GuzzleHttp\Psr7\DroppingStream` Stream decorator that begins dropping data once the size of the underlying stream becomes too full. ```php use GuzzleHttp\Psr7; // Create an empty stream $stream = Psr7\stream_for(); // Start dropping data when the stream has more than 10 bytes $dropping = new Psr7\DroppingStream($stream, 10); $dropping->write('01234567890123456789'); echo $stream; // 0123456789 ``` ## FnStream `GuzzleHttp\Psr7\FnStream` Compose stream implementations based on a hash of functions. Allows for easy testing and extension of a provided stream without needing to create a concrete class for a simple extension point. ```php use GuzzleHttp\Psr7; $stream = Psr7\stream_for('hi'); $fnStream = Psr7\FnStream::decorate($stream, [ 'rewind' => function () use ($stream) { echo 'About to rewind - '; $stream->rewind(); echo 'rewound!'; } ]); $fnStream->rewind(); // Outputs: About to rewind - rewound! ``` ## InflateStream `GuzzleHttp\Psr7\InflateStream` Uses PHP's zlib.inflate filter to inflate deflate or gzipped content. This stream decorator skips the first 10 bytes of the given stream to remove the gzip header, converts the provided stream to a PHP stream resource, then appends the zlib.inflate filter. The stream is then converted back to a Guzzle stream resource to be used as a Guzzle stream. ## LazyOpenStream `GuzzleHttp\Psr7\LazyOpenStream` Lazily reads or writes to a file that is opened only after an IO operation take place on the stream. ```php use GuzzleHttp\Psr7; $stream = new Psr7\LazyOpenStream('/path/to/file', 'r'); // The file has not yet been opened... echo $stream->read(10); // The file is opened and read from only when needed. ``` ## LimitStream `GuzzleHttp\Psr7\LimitStream` LimitStream can be used to read a subset or slice of an existing stream object. This can be useful for breaking a large file into smaller pieces to be sent in chunks (e.g. Amazon S3's multipart upload API). ```php use GuzzleHttp\Psr7; $original = Psr7\stream_for(fopen('/tmp/test.txt', 'r+')); echo $original->getSize(); // >>> 1048576 // Limit the size of the body to 1024 bytes and start reading from byte 2048 $stream = new Psr7\LimitStream($original, 1024, 2048); echo $stream->getSize(); // >>> 1024 echo $stream->tell(); // >>> 0 ``` ## MultipartStream `GuzzleHttp\Psr7\MultipartStream` Stream that when read returns bytes for a streaming multipart or multipart/form-data stream. ## NoSeekStream `GuzzleHttp\Psr7\NoSeekStream` NoSeekStream wraps a stream and does not allow seeking. ```php use GuzzleHttp\Psr7; $original = Psr7\stream_for('foo'); $noSeek = new Psr7\NoSeekStream($original); echo $noSeek->read(3); // foo var_export($noSeek->isSeekable()); // false $noSeek->seek(0); var_export($noSeek->read(3)); // NULL ``` ## PumpStream `GuzzleHttp\Psr7\PumpStream` Provides a read only stream that pumps data from a PHP callable. When invoking the provided callable, the PumpStream will pass the amount of data requested to read to the callable. The callable can choose to ignore this value and return fewer or more bytes than requested. Any extra data returned by the provided callable is buffered internally until drained using the read() function of the PumpStream. The provided callable MUST return false when there is no more data to read. ## Implementing stream decorators Creating a stream decorator is very easy thanks to the `GuzzleHttp\Psr7\StreamDecoratorTrait`. This trait provides methods that implement `Psr\Http\Message\StreamInterface` by proxying to an underlying stream. Just `use` the `StreamDecoratorTrait` and implement your custom methods. For example, let's say we wanted to call a specific function each time the last byte is read from a stream. This could be implemented by overriding the `read()` method. ```php use Psr\Http\Message\StreamInterface; use GuzzleHttp\Psr7\StreamDecoratorTrait; class EofCallbackStream implements StreamInterface { use StreamDecoratorTrait; private $callback; public function __construct(StreamInterface $stream, callable $cb) { $this->stream = $stream; $this->callback = $cb; } public function read($length) { $result = $this->stream->read($length); // Invoke the callback when EOF is hit. if ($this->eof()) { call_user_func($this->callback); } return $result; } } ``` This decorator could be added to any existing stream and used like so: ```php use GuzzleHttp\Psr7; $original = Psr7\stream_for('foo'); $eofStream = new EofCallbackStream($original, function () { echo 'EOF!'; }); $eofStream->read(2); $eofStream->read(1); // echoes "EOF!" $eofStream->seek(0); $eofStream->read(3); // echoes "EOF!" ``` ## PHP StreamWrapper You can use the `GuzzleHttp\Psr7\StreamWrapper` class if you need to use a PSR-7 stream as a PHP stream resource. Use the `GuzzleHttp\Psr7\StreamWrapper::getResource()` method to create a PHP stream from a PSR-7 stream. ```php use GuzzleHttp\Psr7\StreamWrapper; $stream = GuzzleHttp\Psr7\stream_for('hello!'); $resource = StreamWrapper::getResource($stream); echo fread($resource, 6); // outputs hello! ``` # Function API There are various functions available under the `GuzzleHttp\Psr7` namespace. ## `function str` `function str(MessageInterface $message)` Returns the string representation of an HTTP message. ```php $request = new GuzzleHttp\Psr7\Request('GET', 'http://example.com'); echo GuzzleHttp\Psr7\str($request); ``` ## `function uri_for` `function uri_for($uri)` This function accepts a string or `Psr\Http\Message\UriInterface` and returns a UriInterface for the given value. If the value is already a `UriInterface`, it is returned as-is. ```php $uri = GuzzleHttp\Psr7\uri_for('http://example.com'); assert($uri === GuzzleHttp\Psr7\uri_for($uri)); ``` ## `function stream_for` `function stream_for($resource = '', array $options = [])` Create a new stream based on the input type. Options is an associative array that can contain the following keys: * - metadata: Array of custom metadata. * - size: Size of the stream. This method accepts the following `$resource` types: - `Psr\Http\Message\StreamInterface`: Returns the value as-is. - `string`: Creates a stream object that uses the given string as the contents. - `resource`: Creates a stream object that wraps the given PHP stream resource. - `Iterator`: If the provided value implements `Iterator`, then a read-only stream object will be created that wraps the given iterable. Each time the stream is read from, data from the iterator will fill a buffer and will be continuously called until the buffer is equal to the requested read size. Subsequent read calls will first read from the buffer and then call `next` on the underlying iterator until it is exhausted. - `object` with `__toString()`: If the object has the `__toString()` method, the object will be cast to a string and then a stream will be returned that uses the string value. - `NULL`: When `null` is passed, an empty stream object is returned. - `callable` When a callable is passed, a read-only stream object will be created that invokes the given callable. The callable is invoked with the number of suggested bytes to read. The callable can return any number of bytes, but MUST return `false` when there is no more data to return. The stream object that wraps the callable will invoke the callable until the number of requested bytes are available. Any additional bytes will be buffered and used in subsequent reads. ```php $stream = GuzzleHttp\Psr7\stream_for('foo'); $stream = GuzzleHttp\Psr7\stream_for(fopen('/path/to/file', 'r')); $generator = function ($bytes) { for ($i = 0; $i < $bytes; $i++) { yield ' '; } } $stream = GuzzleHttp\Psr7\stream_for($generator(100)); ``` ## `function parse_header` `function parse_header($header)` Parse an array of header values containing ";" separated data into an array of associative arrays representing the header key value pair data of the header. When a parameter does not contain a value, but just contains a key, this function will inject a key with a '' string value. ## `function normalize_header` `function normalize_header($header)` Converts an array of header values that may contain comma separated headers into an array of headers with no comma separated values. ## `function modify_request` `function modify_request(RequestInterface $request, array $changes)` Clone and modify a request with the given changes. This method is useful for reducing the number of clones needed to mutate a message. The changes can be one of: - method: (string) Changes the HTTP method. - set_headers: (array) Sets the given headers. - remove_headers: (array) Remove the given headers. - body: (mixed) Sets the given body. - uri: (UriInterface) Set the URI. - query: (string) Set the query string value of the URI. - version: (string) Set the protocol version. ## `function rewind_body` `function rewind_body(MessageInterface $message)` Attempts to rewind a message body and throws an exception on failure. The body of the message will only be rewound if a call to `tell()` returns a value other than `0`. ## `function try_fopen` `function try_fopen($filename, $mode)` Safely opens a PHP stream resource using a filename. When fopen fails, PHP normally raises a warning. This function adds an error handler that checks for errors and throws an exception instead. ## `function copy_to_string` `function copy_to_string(StreamInterface $stream, $maxLen = -1)` Copy the contents of a stream into a string until the given number of bytes have been read. ## `function copy_to_stream` `function copy_to_stream(StreamInterface $source, StreamInterface $dest, $maxLen = -1)` Copy the contents of a stream into another stream until the given number of bytes have been read. ## `function hash` `function hash(StreamInterface $stream, $algo, $rawOutput = false)` Calculate a hash of a Stream. This method reads the entire stream to calculate a rolling hash (based on PHP's hash_init functions). ## `function readline` `function readline(StreamInterface $stream, $maxLength = null)` Read a line from the stream up to the maximum allowed buffer length. ## `function parse_request` `function parse_request($message)` Parses a request message string into a request object. ## `function parse_response` `function parse_response($message)` Parses a response message string into a response object. ## `function parse_query` `function parse_query($str, $urlEncoding = true)` Parse a query string into an associative array. If multiple values are found for the same key, the value of that key value pair will become an array. This function does not parse nested PHP style arrays into an associative array (e.g., `foo[a]=1&foo[b]=2` will be parsed into `['foo[a]' => '1', 'foo[b]' => '2']`). ## `function build_query` `function build_query(array $params, $encoding = PHP_QUERY_RFC3986)` Build a query string from an array of key value pairs. This function can use the return value of parse_query() to build a query string. This function does not modify the provided keys when an array is encountered (like http_build_query would). ## `function mimetype_from_filename` `function mimetype_from_filename($filename)` Determines the mimetype of a file by looking at its extension. ## `function mimetype_from_extension` `function mimetype_from_extension($extension)` Maps a file extensions to a mimetype. # Additional URI Methods Aside from the standard `Psr\Http\Message\UriInterface` implementation in form of the `GuzzleHttp\Psr7\Uri` class, this library also provides additional functionality when working with URIs as static methods. ## URI Types An instance of `Psr\Http\Message\UriInterface` can either be an absolute URI or a relative reference. An absolute URI has a scheme. A relative reference is used to express a URI relative to another URI, the base URI. Relative references can be divided into several forms according to [RFC 3986 Section 4.2](https://tools.ietf.org/html/rfc3986#section-4.2): - network-path references, e.g. `//example.com/path` - absolute-path references, e.g. `/path` - relative-path references, e.g. `subpath` The following methods can be used to identify the type of the URI. ### `GuzzleHttp\Psr7\Uri::isAbsolute` `public static function isAbsolute(UriInterface $uri): bool` Whether the URI is absolute, i.e. it has a scheme. ### `GuzzleHttp\Psr7\Uri::isNetworkPathReference` `public static function isNetworkPathReference(UriInterface $uri): bool` Whether the URI is a network-path reference. A relative reference that begins with two slash characters is termed an network-path reference. ### `GuzzleHttp\Psr7\Uri::isAbsolutePathReference` `public static function isAbsolutePathReference(UriInterface $uri): bool` Whether the URI is a absolute-path reference. A relative reference that begins with a single slash character is termed an absolute-path reference. ### `GuzzleHttp\Psr7\Uri::isRelativePathReference` `public static function isRelativePathReference(UriInterface $uri): bool` Whether the URI is a relative-path reference. A relative reference that does not begin with a slash character is termed a relative-path reference. ### `GuzzleHttp\Psr7\Uri::isSameDocumentReference` `public static function isSameDocumentReference(UriInterface $uri, UriInterface $base = null): bool` Whether the URI is a same-document reference. A same-document reference refers to a URI that is, aside from its fragment component, identical to the base URI. When no base URI is given, only an empty URI reference (apart from its fragment) is considered a same-document reference. ## URI Components Additional methods to work with URI components. ### `GuzzleHttp\Psr7\Uri::isDefaultPort` `public static function isDefaultPort(UriInterface $uri): bool` Whether the URI has the default port of the current scheme. `Psr\Http\Message\UriInterface::getPort` may return null or the standard port. This method can be used independently of the implementation. ### `GuzzleHttp\Psr7\Uri::composeComponents` `public static function composeComponents($scheme, $authority, $path, $query, $fragment): string` Composes a URI reference string from its various components according to [RFC 3986 Section 5.3](https://tools.ietf.org/html/rfc3986#section-5.3). Usually this method does not need to be called manually but instead is used indirectly via `Psr\Http\Message\UriInterface::__toString`. ### `GuzzleHttp\Psr7\Uri::fromParts` `public static function fromParts(array $parts): UriInterface` Creates a URI from a hash of [`parse_url`](http://php.net/manual/en/function.parse-url.php) components. ### `GuzzleHttp\Psr7\Uri::withQueryValue` `public static function withQueryValue(UriInterface $uri, $key, $value): UriInterface` Creates a new URI with a specific query string value. Any existing query string values that exactly match the provided key are removed and replaced with the given key value pair. A value of null will set the query string key without a value, e.g. "key" instead of "key=value". ### `GuzzleHttp\Psr7\Uri::withQueryValues` `public static function withQueryValues(UriInterface $uri, array $keyValueArray): UriInterface` Creates a new URI with multiple query string values. It has the same behavior as `withQueryValue()` but for an associative array of key => value. ### `GuzzleHttp\Psr7\Uri::withoutQueryValue` `public static function withoutQueryValue(UriInterface $uri, $key): UriInterface` Creates a new URI with a specific query string value removed. Any existing query string values that exactly match the provided key are removed. ## Reference Resolution `GuzzleHttp\Psr7\UriResolver` provides methods to resolve a URI reference in the context of a base URI according to [RFC 3986 Section 5](https://tools.ietf.org/html/rfc3986#section-5). This is for example also what web browsers do when resolving a link in a website based on the current request URI. ### `GuzzleHttp\Psr7\UriResolver::resolve` `public static function resolve(UriInterface $base, UriInterface $rel): UriInterface` Converts the relative URI into a new URI that is resolved against the base URI. ### `GuzzleHttp\Psr7\UriResolver::removeDotSegments` `public static function removeDotSegments(string $path): string` Removes dot segments from a path and returns the new path according to [RFC 3986 Section 5.2.4](https://tools.ietf.org/html/rfc3986#section-5.2.4). ### `GuzzleHttp\Psr7\UriResolver::relativize` `public static function relativize(UriInterface $base, UriInterface $target): UriInterface` Returns the target URI as a relative reference from the base URI. This method is the counterpart to resolve(): ```php (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target)) ``` One use-case is to use the current request URI as base URI and then generate relative links in your documents to reduce the document size or offer self-contained downloadable document archives. ```php $base = new Uri('http://example.com/a/b/'); echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'. echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'. echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'. echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'. ``` ## Normalization and Comparison `GuzzleHttp\Psr7\UriNormalizer` provides methods to normalize and compare URIs according to [RFC 3986 Section 6](https://tools.ietf.org/html/rfc3986#section-6). ### `GuzzleHttp\Psr7\UriNormalizer::normalize` `public static function normalize(UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS): UriInterface` Returns a normalized URI. The scheme and host component are already normalized to lowercase per PSR-7 UriInterface. This methods adds additional normalizations that can be configured with the `$flags` parameter which is a bitmask of normalizations to apply. The following normalizations are available: - `UriNormalizer::PRESERVING_NORMALIZATIONS` Default normalizations which only include the ones that preserve semantics. - `UriNormalizer::CAPITALIZE_PERCENT_ENCODING` All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized. Example: `http://example.org/a%c2%b1b` → `http://example.org/a%C2%B1b` - `UriNormalizer::DECODE_UNRESERVED_CHARACTERS` Decodes percent-encoded octets of unreserved characters. For consistency, percent-encoded octets in the ranges of ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39), hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and, when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers. Example: `http://example.org/%7Eusern%61me/` → `http://example.org/~username/` - `UriNormalizer::CONVERT_EMPTY_PATH` Converts the empty path to "/" for http and https URIs. Example: `http://example.org` → `http://example.org/` - `UriNormalizer::REMOVE_DEFAULT_HOST` Removes the default host of the given URI scheme from the URI. Only the "file" scheme defines the default host "localhost". All of `file:/myfile`, `file:///myfile`, and `file://localhost/myfile` are equivalent according to RFC 3986. Example: `file://localhost/myfile` → `file:///myfile` - `UriNormalizer::REMOVE_DEFAULT_PORT` Removes the default port of the given URI scheme from the URI. Example: `http://example.org:80/` → `http://example.org/` - `UriNormalizer::REMOVE_DOT_SEGMENTS` Removes unnecessary dot-segments. Dot-segments in relative-path references are not removed as it would change the semantics of the URI reference. Example: `http://example.org/../a/b/../c/./d.html` → `http://example.org/a/c/d.html` - `UriNormalizer::REMOVE_DUPLICATE_SLASHES` Paths which include two or more adjacent slashes are converted to one. Webservers usually ignore duplicate slashes and treat those URIs equivalent. But in theory those URIs do not need to be equivalent. So this normalization may change the semantics. Encoded slashes (%2F) are not removed. Example: `http://example.org//foo///bar.html` → `http://example.org/foo/bar.html` - `UriNormalizer::SORT_QUERY_PARAMETERS` Sort query parameters with their values in alphabetical order. However, the order of parameters in a URI may be significant (this is not defined by the standard). So this normalization is not safe and may change the semantics of the URI. Example: `?lang=en&article=fred` → `?article=fred&lang=en` ### `GuzzleHttp\Psr7\UriNormalizer::isEquivalent` `public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, $normalizations = self::PRESERVING_NORMALIZATIONS): bool` Whether two URIs can be considered equivalent. Both URIs are normalized automatically before comparison with the given `$normalizations` bitmask. The method also accepts relative URI references and returns true when they are equivalent. This of course assumes they will be resolved against the same base URI. If this is not the case, determination of equivalence or difference of relative references does not mean anything. FROM composer:latest as setup RUN mkdir /guzzle WORKDIR /guzzle RUN set -xe \ && composer init --name=guzzlehttp/test --description="Simple project for testing Guzzle scripts" --author="Márk Sági-Kazár <mark.sagikazar@gmail.com>" --no-interaction \ && composer require guzzlehttp/guzzle FROM php:7.3 RUN mkdir /guzzle WORKDIR /guzzle COPY --from=setup /guzzle /guzzle <?php namespace GuzzleHttp; use GuzzleHttp\Handler\CurlHandler; use GuzzleHttp\Handler\CurlMultiHandler; use GuzzleHttp\Handler\Proxy; use GuzzleHttp\Handler\StreamHandler; /** * Expands a URI template * * @param string $template URI template * @param array $variables Template variables * * @return string */ function uri_template($template, array $variables) { if (extension_loaded('uri_template')) { // @codeCoverageIgnoreStart return \uri_template($template, $variables); // @codeCoverageIgnoreEnd } static $uriTemplate; if (!$uriTemplate) { $uriTemplate = new UriTemplate(); } return $uriTemplate->expand($template, $variables); } /** * Debug function used to describe the provided value type and class. * * @param mixed $input * * @return string Returns a string containing the type of the variable and * if a class is provided, the class name. */ function describe_type($input) { switch (gettype($input)) { case 'object': return 'object(' . get_class($input) . ')'; case 'array': return 'array(' . count($input) . ')'; default: ob_start(); var_dump($input); // normalize float vs double return str_replace('double(', 'float(', rtrim(ob_get_clean())); } } /** * Parses an array of header lines into an associative array of headers. * * @param iterable $lines Header lines array of strings in the following * format: "Name: Value" * @return array */ function headers_from_lines($lines) { $headers = []; foreach ($lines as $line) { $parts = explode(':', $line, 2); $headers[trim($parts[0])][] = isset($parts[1]) ? trim($parts[1]) : null; } return $headers; } /** * Returns a debug stream based on the provided variable. * * @param mixed $value Optional value * * @return resource */ function debug_resource($value = null) { if (is_resource($value)) { return $value; } elseif (defined('STDOUT')) { return STDOUT; } return fopen('php://output', 'w'); } /** * Chooses and creates a default handler to use based on the environment. * * The returned handler is not wrapped by any default middlewares. * * @throws \RuntimeException if no viable Handler is available. * @return callable Returns the best handler for the given system. */ function choose_handler() { $handler = null; if (function_exists('curl_multi_exec') && function_exists('curl_exec')) { $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler()); } elseif (function_exists('curl_exec')) { $handler = new CurlHandler(); } elseif (function_exists('curl_multi_exec')) { $handler = new CurlMultiHandler(); } if (ini_get('allow_url_fopen')) { $handler = $handler ? Proxy::wrapStreaming($handler, new StreamHandler()) : new StreamHandler(); } elseif (!$handler) { throw new \RuntimeException('GuzzleHttp requires cURL, the ' . 'allow_url_fopen ini setting, or a custom HTTP handler.'); } return $handler; } /** * Get the default User-Agent string to use with Guzzle * * @return string */ function default_user_agent() { static $defaultAgent = ''; if (!$defaultAgent) { $defaultAgent = 'GuzzleHttp/' . Client::VERSION; if (extension_loaded('curl') && function_exists('curl_version')) { $defaultAgent .= ' curl/' . \curl_version()['version']; } $defaultAgent .= ' PHP/' . PHP_VERSION; } return $defaultAgent; } /** * Returns the default cacert bundle for the current system. * * First, the openssl.cafile and curl.cainfo php.ini settings are checked. * If those settings are not configured, then the common locations for * bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X * and Windows are checked. If any of these file locations are found on * disk, they will be utilized. * * Note: the result of this function is cached for subsequent calls. * * @return string * @throws \RuntimeException if no bundle can be found. */ function default_ca_bundle() { static $cached = null; static $cafiles = [ // Red Hat, CentOS, Fedora (provided by the ca-certificates package) '/etc/pki/tls/certs/ca-bundle.crt', // Ubuntu, Debian (provided by the ca-certificates package) '/etc/ssl/certs/ca-certificates.crt', // FreeBSD (provided by the ca_root_nss package) '/usr/local/share/certs/ca-root-nss.crt', // SLES 12 (provided by the ca-certificates package) '/var/lib/ca-certificates/ca-bundle.pem', // OS X provided by homebrew (using the default path) '/usr/local/etc/openssl/cert.pem', // Google app engine '/etc/ca-certificates.crt', // Windows? 'C:\\windows\\system32\\curl-ca-bundle.crt', 'C:\\windows\\curl-ca-bundle.crt', ]; if ($cached) { return $cached; } if ($ca = ini_get('openssl.cafile')) { return $cached = $ca; } if ($ca = ini_get('curl.cainfo')) { return $cached = $ca; } foreach ($cafiles as $filename) { if (file_exists($filename)) { return $cached = $filename; } } throw new \RuntimeException( <<< EOT No system CA bundle could be found in any of the the common system locations. PHP versions earlier than 5.6 are not properly configured to use the system's CA bundle by default. In order to verify peer certificates, you will need to supply the path on disk to a certificate bundle to the 'verify' request option: http://docs.guzzlephp.org/en/latest/clients.html#verify. If you do not need a specific certificate bundle, then Mozilla provides a commonly used CA bundle which can be downloaded here (provided by the maintainer of cURL): https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt. Once you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP ini setting to point to the path to the file, allowing you to omit the 'verify' request option. See http://curl.haxx.se/docs/sslcerts.html for more information. EOT ); } /** * Creates an associative array of lowercase header names to the actual * header casing. * * @param array $headers * * @return array */ function normalize_header_keys(array $headers) { $result = []; foreach (array_keys($headers) as $key) { $result[strtolower($key)] = $key; } return $result; } /** * Returns true if the provided host matches any of the no proxy areas. * * This method will strip a port from the host if it is present. Each pattern * can be matched with an exact match (e.g., "foo.com" == "foo.com") or a * partial match: (e.g., "foo.com" == "baz.foo.com" and ".foo.com" == * "baz.foo.com", but ".foo.com" != "foo.com"). * * Areas are matched in the following cases: * 1. "*" (without quotes) always matches any hosts. * 2. An exact match. * 3. The area starts with "." and the area is the last part of the host. e.g. * '.mit.edu' will match any host that ends with '.mit.edu'. * * @param string $host Host to check against the patterns. * @param array $noProxyArray An array of host patterns. * * @return bool */ function is_host_in_noproxy($host, array $noProxyArray) { if (strlen($host) === 0) { throw new \InvalidArgumentException('Empty host provided'); } // Strip port if present. if (strpos($host, ':')) { $host = explode($host, ':', 2)[0]; } foreach ($noProxyArray as $area) { // Always match on wildcards. if ($area === '*') { return true; } elseif (empty($area)) { // Don't match on empty values. continue; } elseif ($area === $host) { // Exact matches. return true; } else { // Special match if the area when prefixed with ".". Remove any // existing leading "." and add a new leading ".". $area = '.' . ltrim($area, '.'); if (substr($host, -(strlen($area))) === $area) { return true; } } } return false; } /** * Wrapper for json_decode that throws when an error occurs. * * @param string $json JSON data to parse * @param bool $assoc When true, returned objects will be converted * into associative arrays. * @param int $depth User specified recursion depth. * @param int $options Bitmask of JSON decode options. * * @return mixed * @throws Exception\InvalidArgumentException if the JSON cannot be decoded. * @link http://www.php.net/manual/en/function.json-decode.php */ function json_decode($json, $assoc = false, $depth = 512, $options = 0) { $data = \json_decode($json, $assoc, $depth, $options); if (JSON_ERROR_NONE !== json_last_error()) { throw new Exception\InvalidArgumentException( 'json_decode error: ' . json_last_error_msg() ); } return $data; } /** * Wrapper for JSON encoding that throws when an error occurs. * * @param mixed $value The value being encoded * @param int $options JSON encode option bitmask * @param int $depth Set the maximum depth. Must be greater than zero. * * @return string * @throws Exception\InvalidArgumentException if the JSON cannot be encoded. * @link http://www.php.net/manual/en/function.json-encode.php */ function json_encode($value, $options = 0, $depth = 512) { $json = \json_encode($value, $options, $depth); if (JSON_ERROR_NONE !== json_last_error()) { throw new Exception\InvalidArgumentException( 'json_encode error: ' . json_last_error_msg() ); } return $json; } /** * Wrapper for the hrtime() or microtime() functions * (depending on the PHP version, one of the two is used) * * @return float|mixed UNIX timestamp * @internal */ function _current_time() { return function_exists('hrtime') ? hrtime(true) / 1e9 : microtime(true); } <?php namespace GuzzleHttp; use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\Promise\RejectedPromise; use GuzzleHttp\Psr7; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** * Middleware that retries requests based on the boolean result of * invoking the provided "decider" function. */ class RetryMiddleware { /** @var callable */ private $nextHandler; /** @var callable */ private $decider; /** @var callable */ private $delay; /** * @param callable $decider Function that accepts the number of retries, * a request, [response], and [exception] and * returns true if the request is to be * retried. * @param callable $nextHandler Next handler to invoke. * @param callable $delay Function that accepts the number of retries * and [response] and returns the number of * milliseconds to delay. */ public function __construct( callable $decider, callable $nextHandler, callable $delay = null ) { $this->decider = $decider; $this->nextHandler = $nextHandler; $this->delay = $delay ?: __CLASS__ . '::exponentialDelay'; } /** * Default exponential backoff delay function. * * @param int $retries * * @return int milliseconds. */ public static function exponentialDelay($retries) { return (int) pow(2, $retries - 1) * 1000; } /** * @param RequestInterface $request * @param array $options * * @return PromiseInterface */ public function __invoke(RequestInterface $request, array $options) { if (!isset($options['retries'])) { $options['retries'] = 0; } $fn = $this->nextHandler; return $fn($request, $options) ->then( $this->onFulfilled($request, $options), $this->onRejected($request, $options) ); } /** * Execute fulfilled closure * * @return mixed */ private function onFulfilled(RequestInterface $req, array $options) { return function ($value) use ($req, $options) { if (!call_user_func( $this->decider, $options['retries'], $req, $value, null )) { return $value; } return $this->doRetry($req, $options, $value); }; } /** * Execute rejected closure * * @return callable */ private function onRejected(RequestInterface $req, array $options) { return function ($reason) use ($req, $options) { if (!call_user_func( $this->decider, $options['retries'], $req, null, $reason )) { return \GuzzleHttp\Promise\rejection_for($reason); } return $this->doRetry($req, $options); }; } /** * @return self */ private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null) { $options['delay'] = call_user_func($this->delay, ++$options['retries'], $response); return $this($request, $options); } } <?php // Don't redefine the functions if included multiple times. if (!function_exists('GuzzleHttp\uri_template')) { require __DIR__ . '/functions.php'; } <?php namespace GuzzleHttp; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; /** * Represents data at the point after it was transferred either successfully * or after a network error. */ final class TransferStats { private $request; private $response; private $transferTime; private $handlerStats; private $handlerErrorData; /** * @param RequestInterface $request Request that was sent. * @param ResponseInterface|null $response Response received (if any) * @param float|null $transferTime Total handler transfer time. * @param mixed $handlerErrorData Handler error data. * @param array $handlerStats Handler specific stats. */ public function __construct( RequestInterface $request, ResponseInterface $response = null, $transferTime = null, $handlerErrorData = null, $handlerStats = [] ) { $this->request = $request; $this->response = $response; $this->transferTime = $transferTime; $this->handlerErrorData = $handlerErrorData; $this->handlerStats = $handlerStats; } /** * @return RequestInterface */ public function getRequest() { return $this->request; } /** * Returns the response that was received (if any). * * @return ResponseInterface|null */ public function getResponse() { return $this->response; } /** * Returns true if a response was received. * * @return bool */ public function hasResponse() { return $this->response !== null; } /** * Gets handler specific error data. * * This might be an exception, a integer representing an error code, or * anything else. Relying on this value assumes that you know what handler * you are using. * * @return mixed */ public function getHandlerErrorData() { return $this->handlerErrorData; } /** * Get the effective URI the request was sent to. * * @return UriInterface */ public function getEffectiveUri() { return $this->request->getUri(); } /** * Get the estimated time the request was being transferred by the handler. * * @return float|null Time in seconds. */ public function getTransferTime() { return $this->transferTime; } /** * Gets an array of all of the handler specific transfer data. * * @return array */ public function getHandlerStats() { return $this->handlerStats; } /** * Get a specific handler statistic from the handler by name. * * @param string $stat Handler specific transfer stat to retrieve. * * @return mixed|null */ public function getHandlerStat($stat) { return isset($this->handlerStats[$stat]) ? $this->handlerStats[$stat] : null; } } <?php namespace GuzzleHttp; /** * This class contains a list of built-in Guzzle request options. * * More documentation for each option can be found at http://guzzlephp.org/. * * @link http://docs.guzzlephp.org/en/v6/request-options.html */ final class RequestOptions { /** * allow_redirects: (bool|array) Controls redirect behavior. Pass false * to disable redirects, pass true to enable redirects, pass an * associative to provide custom redirect settings. Defaults to "false". * This option only works if your handler has the RedirectMiddleware. When * passing an associative array, you can provide the following key value * pairs: * * - max: (int, default=5) maximum number of allowed redirects. * - strict: (bool, default=false) Set to true to use strict redirects * meaning redirect POST requests with POST requests vs. doing what most * browsers do which is redirect POST requests with GET requests * - referer: (bool, default=false) Set to true to enable the Referer * header. * - protocols: (array, default=['http', 'https']) Allowed redirect * protocols. * - on_redirect: (callable) PHP callable that is invoked when a redirect * is encountered. The callable is invoked with the request, the redirect * response that was received, and the effective URI. Any return value * from the on_redirect function is ignored. */ const ALLOW_REDIRECTS = 'allow_redirects'; /** * auth: (array) Pass an array of HTTP authentication parameters to use * with the request. The array must contain the username in index [0], * the password in index [1], and you can optionally provide a built-in * authentication type in index [2]. Pass null to disable authentication * for a request. */ const AUTH = 'auth'; /** * body: (resource|string|null|int|float|StreamInterface|callable|\Iterator) * Body to send in the request. */ const BODY = 'body'; /** * cert: (string|array) Set to a string to specify the path to a file * containing a PEM formatted SSL client side certificate. If a password * is required, then set cert to an array containing the path to the PEM * file in the first array element followed by the certificate password * in the second array element. */ const CERT = 'cert'; /** * cookies: (bool|GuzzleHttp\Cookie\CookieJarInterface, default=false) * Specifies whether or not cookies are used in a request or what cookie * jar to use or what cookies to send. This option only works if your * handler has the `cookie` middleware. Valid values are `false` and * an instance of {@see GuzzleHttp\Cookie\CookieJarInterface}. */ const COOKIES = 'cookies'; /** * connect_timeout: (float, default=0) Float describing the number of * seconds to wait while trying to connect to a server. Use 0 to wait * indefinitely (the default behavior). */ const CONNECT_TIMEOUT = 'connect_timeout'; /** * debug: (bool|resource) Set to true or set to a PHP stream returned by * fopen() enable debug output with the HTTP handler used to send a * request. */ const DEBUG = 'debug'; /** * decode_content: (bool, default=true) Specify whether or not * Content-Encoding responses (gzip, deflate, etc.) are automatically * decoded. */ const DECODE_CONTENT = 'decode_content'; /** * delay: (int) The amount of time to delay before sending in milliseconds. */ const DELAY = 'delay'; /** * expect: (bool|integer) Controls the behavior of the * "Expect: 100-Continue" header. * * Set to `true` to enable the "Expect: 100-Continue" header for all * requests that sends a body. Set to `false` to disable the * "Expect: 100-Continue" header for all requests. Set to a number so that * the size of the payload must be greater than the number in order to send * the Expect header. Setting to a number will send the Expect header for * all requests in which the size of the payload cannot be determined or * where the body is not rewindable. * * By default, Guzzle will add the "Expect: 100-Continue" header when the * size of the body of a request is greater than 1 MB and a request is * using HTTP/1.1. */ const EXPECT = 'expect'; /** * form_params: (array) Associative array of form field names to values * where each value is a string or array of strings. Sets the Content-Type * header to application/x-www-form-urlencoded when no Content-Type header * is already present. */ const FORM_PARAMS = 'form_params'; /** * headers: (array) Associative array of HTTP headers. Each value MUST be * a string or array of strings. */ const HEADERS = 'headers'; /** * http_errors: (bool, default=true) Set to false to disable exceptions * when a non- successful HTTP response is received. By default, * exceptions will be thrown for 4xx and 5xx responses. This option only * works if your handler has the `httpErrors` middleware. */ const HTTP_ERRORS = 'http_errors'; /** * idn: (bool|int, default=true) A combination of IDNA_* constants for * idn_to_ascii() PHP's function (see "options" parameter). Set to false to * disable IDN support completely, or to true to use the default * configuration (IDNA_DEFAULT constant). */ const IDN_CONVERSION = 'idn_conversion'; /** * json: (mixed) Adds JSON data to a request. The provided value is JSON * encoded and a Content-Type header of application/json will be added to * the request if no Content-Type header is already present. */ const JSON = 'json'; /** * multipart: (array) Array of associative arrays, each containing a * required "name" key mapping to the form field, name, a required * "contents" key mapping to a StreamInterface|resource|string, an * optional "headers" associative array of custom headers, and an * optional "filename" key mapping to a string to send as the filename in * the part. If no "filename" key is present, then no "filename" attribute * will be added to the part. */ const MULTIPART = 'multipart'; /** * on_headers: (callable) A callable that is invoked when the HTTP headers * of the response have been received but the body has not yet begun to * download. */ const ON_HEADERS = 'on_headers'; /** * on_stats: (callable) allows you to get access to transfer statistics of * a request and access the lower level transfer details of the handler * associated with your client. ``on_stats`` is a callable that is invoked * when a handler has finished sending a request. The callback is invoked * with transfer statistics about the request, the response received, or * the error encountered. Included in the data is the total amount of time * taken to send the request. */ const ON_STATS = 'on_stats'; /** * progress: (callable) Defines a function to invoke when transfer * progress is made. The function accepts the following positional * arguments: the total number of bytes expected to be downloaded, the * number of bytes downloaded so far, the number of bytes expected to be * uploaded, the number of bytes uploaded so far. */ const PROGRESS = 'progress'; /** * proxy: (string|array) Pass a string to specify an HTTP proxy, or an * array to specify different proxies for different protocols (where the * key is the protocol and the value is a proxy string). */ const PROXY = 'proxy'; /** * query: (array|string) Associative array of query string values to add * to the request. This option uses PHP's http_build_query() to create * the string representation. Pass a string value if you need more * control than what this method provides */ const QUERY = 'query'; /** * sink: (resource|string|StreamInterface) Where the data of the * response is written to. Defaults to a PHP temp stream. Providing a * string will write data to a file by the given name. */ const SINK = 'sink'; /** * synchronous: (bool) Set to true to inform HTTP handlers that you intend * on waiting on the response. This can be useful for optimizations. Note * that a promise is still returned if you are using one of the async * client methods. */ const SYNCHRONOUS = 'synchronous'; /** * ssl_key: (array|string) Specify the path to a file containing a private * SSL key in PEM format. If a password is required, then set to an array * containing the path to the SSL key in the first array element followed * by the password required for the certificate in the second element. */ const SSL_KEY = 'ssl_key'; /** * stream: Set to true to attempt to stream a response rather than * download it all up-front. */ const STREAM = 'stream'; /** * verify: (bool|string, default=true) Describes the SSL certificate * verification behavior of a request. Set to true to enable SSL * certificate verification using the system CA bundle when available * (the default). Set to false to disable certificate verification (this * is insecure!). Set to a string to provide the path to a CA bundle on * disk to enable verification using a custom certificate. */ const VERIFY = 'verify'; /** * timeout: (float, default=0) Float describing the timeout of the * request in seconds. Use 0 to wait indefinitely (the default behavior). */ const TIMEOUT = 'timeout'; /** * read_timeout: (float, default=default_socket_timeout ini setting) Float describing * the body read timeout, for stream requests. */ const READ_TIMEOUT = 'read_timeout'; /** * version: (float) Specifies the HTTP protocol version to attempt to use. */ const VERSION = 'version'; /** * force_ip_resolve: (bool) Force client to use only ipv4 or ipv6 protocol */ const FORCE_IP_RESOLVE = 'force_ip_resolve'; } <?php namespace GuzzleHttp\Handler; use GuzzleHttp\Psr7\Response; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; /** * Represents a cURL easy handle and the data it populates. * * @internal */ final class EasyHandle { /** @var resource cURL resource */ public $handle; /** @var StreamInterface Where data is being written */ public $sink; /** @var array Received HTTP headers so far */ public $headers = []; /** @var ResponseInterface Received response (if any) */ public $response; /** @var RequestInterface Request being sent */ public $request; /** @var array Request options */ public $options = []; /** @var int cURL error number (if any) */ public $errno = 0; /** @var \Exception Exception during on_headers (if any) */ public $onHeadersException; /** * Attach a response to the easy handle based on the received headers. * * @throws \RuntimeException if no headers have been received. */ public function createResponse() { if (empty($this->headers)) { throw new \RuntimeException('No headers have been received'); } // HTTP-version SP status-code SP reason-phrase $startLine = explode(' ', array_shift($this->headers), 3); $headers = \GuzzleHttp\headers_from_lines($this->headers); $normalizedKeys = \GuzzleHttp\normalize_header_keys($headers); if (!empty($this->options['decode_content']) && isset($normalizedKeys['content-encoding']) ) { $headers['x-encoded-content-encoding'] = $headers[$normalizedKeys['content-encoding']]; unset($headers[$normalizedKeys['content-encoding']]); if (isset($normalizedKeys['content-length'])) { $headers['x-encoded-content-length'] = $headers[$normalizedKeys['content-length']]; $bodyLength = (int) $this->sink->getSize(); if ($bodyLength) { $headers[$normalizedKeys['content-length']] = $bodyLength; } else { unset($headers[$normalizedKeys['content-length']]); } } } // Attach a response to the easy handle with the parsed headers. $this->response = new Response( $startLine[1], $headers, $this->sink, substr($startLine[0], 5), isset($startLine[2]) ? (string) $startLine[2] : null ); } public function __get($name) { $msg = $name === 'handle' ? 'The EasyHandle has been released' : 'Invalid property: ' . $name; throw new \BadMethodCallException($msg); } } <?php namespace GuzzleHttp\Handler; use GuzzleHttp\RequestOptions; use Psr\Http\Message\RequestInterface; /** * Provides basic proxies for handlers. */ class Proxy { /** * Sends synchronous requests to a specific handler while sending all other * requests to another handler. * * @param callable $default Handler used for normal responses * @param callable $sync Handler used for synchronous responses. * * @return callable Returns the composed handler. */ public static function wrapSync( callable $default, callable $sync ) { return function (RequestInterface $request, array $options) use ($default, $sync) { return empty($options[RequestOptions::SYNCHRONOUS]) ? $default($request, $options) : $sync($request, $options); }; } /** * Sends streaming requests to a streaming compatible handler while sending * all other requests to a default handler. * * This, for example, could be useful for taking advantage of the * performance benefits of curl while still supporting true streaming * through the StreamHandler. * * @param callable $default Handler used for non-streaming responses * @param callable $streaming Handler used for streaming responses * * @return callable Returns the composed handler. */ public static function wrapStreaming( callable $default, callable $streaming ) { return function (RequestInterface $request, array $options) use ($default, $streaming) { return empty($options['stream']) ? $default($request, $options) : $streaming($request, $options); }; } } <?php namespace GuzzleHttp\Handler; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Promise\FulfilledPromise; use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\Psr7; use GuzzleHttp\TransferStats; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; /** * HTTP handler that uses PHP's HTTP stream wrapper. */ class StreamHandler { private $lastHeaders = []; /** * Sends an HTTP request. * * @param RequestInterface $request Request to send. * @param array $options Request transfer options. * * @return PromiseInterface */ public function __invoke(RequestInterface $request, array $options) { // Sleep if there is a delay specified. if (isset($options['delay'])) { usleep($options['delay'] * 1000); } $startTime = isset($options['on_stats']) ? \GuzzleHttp\_current_time() : null; try { // Does not support the expect header. $request = $request->withoutHeader('Expect'); // Append a content-length header if body size is zero to match // cURL's behavior. if (0 === $request->getBody()->getSize()) { $request = $request->withHeader('Content-Length', '0'); } return $this->createResponse( $request, $options, $this->createStream($request, $options), $startTime ); } catch (\InvalidArgumentException $e) { throw $e; } catch (\Exception $e) { // Determine if the error was a networking error. $message = $e->getMessage(); // This list can probably get more comprehensive. if (strpos($message, 'getaddrinfo') // DNS lookup failed || strpos($message, 'Connection refused') || strpos($message, "couldn't connect to host") // error on HHVM || strpos($message, "connection attempt failed") ) { $e = new ConnectException($e->getMessage(), $request, $e); } $e = RequestException::wrapException($request, $e); $this->invokeStats($options, $request, $startTime, null, $e); return \GuzzleHttp\Promise\rejection_for($e); } } private function invokeStats( array $options, RequestInterface $request, $startTime, ResponseInterface $response = null, $error = null ) { if (isset($options['on_stats'])) { $stats = new TransferStats( $request, $response, \GuzzleHttp\_current_time() - $startTime, $error, [] ); call_user_func($options['on_stats'], $stats); } } private function createResponse( RequestInterface $request, array $options, $stream, $startTime ) { $hdrs = $this->lastHeaders; $this->lastHeaders = []; $parts = explode(' ', array_shift($hdrs), 3); $ver = explode('/', $parts[0])[1]; $status = $parts[1]; $reason = isset($parts[2]) ? $parts[2] : null; $headers = \GuzzleHttp\headers_from_lines($hdrs); list($stream, $headers) = $this->checkDecode($options, $headers, $stream); $stream = Psr7\stream_for($stream); $sink = $stream; if (strcasecmp('HEAD', $request->getMethod())) { $sink = $this->createSink($stream, $options); } $response = new Psr7\Response($status, $headers, $sink, $ver, $reason); if (isset($options['on_headers'])) { try { $options['on_headers']($response); } catch (\Exception $e) { $msg = 'An error was encountered during the on_headers event'; $ex = new RequestException($msg, $request, $response, $e); return \GuzzleHttp\Promise\rejection_for($ex); } } // Do not drain when the request is a HEAD request because they have // no body. if ($sink !== $stream) { $this->drain( $stream, $sink, $response->getHeaderLine('Content-Length') ); } $this->invokeStats($options, $request, $startTime, $response, null); return new FulfilledPromise($response); } private function createSink(StreamInterface $stream, array $options) { if (!empty($options['stream'])) { return $stream; } $sink = isset($options['sink']) ? $options['sink'] : fopen('php://temp', 'r+'); return is_string($sink) ? new Psr7\LazyOpenStream($sink, 'w+') : Psr7\stream_for($sink); } private function checkDecode(array $options, array $headers, $stream) { // Automatically decode responses when instructed. if (!empty($options['decode_content'])) { $normalizedKeys = \GuzzleHttp\normalize_header_keys($headers); if (isset($normalizedKeys['content-encoding'])) { $encoding = $headers[$normalizedKeys['content-encoding']]; if ($encoding[0] === 'gzip' || $encoding[0] === 'deflate') { $stream = new Psr7\InflateStream( Psr7\stream_for($stream) ); $headers['x-encoded-content-encoding'] = $headers[$normalizedKeys['content-encoding']]; // Remove content-encoding header unset($headers[$normalizedKeys['content-encoding']]); // Fix content-length header if (isset($normalizedKeys['content-length'])) { $headers['x-encoded-content-length'] = $headers[$normalizedKeys['content-length']]; $length = (int) $stream->getSize(); if ($length === 0) { unset($headers[$normalizedKeys['content-length']]); } else { $headers[$normalizedKeys['content-length']] = [$length]; } } } } } return [$stream, $headers]; } /** * Drains the source stream into the "sink" client option. * * @param StreamInterface $source * @param StreamInterface $sink * @param string $contentLength Header specifying the amount of * data to read. * * @return StreamInterface * @throws \RuntimeException when the sink option is invalid. */ private function drain( StreamInterface $source, StreamInterface $sink, $contentLength ) { // If a content-length header is provided, then stop reading once // that number of bytes has been read. This can prevent infinitely // reading from a stream when dealing with servers that do not honor // Connection: Close headers. Psr7\copy_to_stream( $source, $sink, (strlen($contentLength) > 0 && (int) $contentLength > 0) ? (int) $contentLength : -1 ); $sink->seek(0); $source->close(); return $sink; } /** * Create a resource and check to ensure it was created successfully * * @param callable $callback Callable that returns stream resource * * @return resource * @throws \RuntimeException on error */ private function createResource(callable $callback) { $errors = null; set_error_handler(function ($_, $msg, $file, $line) use (&$errors) { $errors[] = [ 'message' => $msg, 'file' => $file, 'line' => $line ]; return true; }); $resource = $callback(); restore_error_handler(); if (!$resource) { $message = 'Error creating resource: '; foreach ($errors as $err) { foreach ($err as $key => $value) { $message .= "[$key] $value" . PHP_EOL; } } throw new \RuntimeException(trim($message)); } return $resource; } private function createStream(RequestInterface $request, array $options) { static $methods; if (!$methods) { $methods = array_flip(get_class_methods(__CLASS__)); } // HTTP/1.1 streams using the PHP stream wrapper require a // Connection: close header if ($request->getProtocolVersion() == '1.1' && !$request->hasHeader('Connection') ) { $request = $request->withHeader('Connection', 'close'); } // Ensure SSL is verified by default if (!isset($options['verify'])) { $options['verify'] = true; } $params = []; $context = $this->getDefaultContext($request); if (isset($options['on_headers']) && !is_callable($options['on_headers'])) { throw new \InvalidArgumentException('on_headers must be callable'); } if (!empty($options)) { foreach ($options as $key => $value) { $method = "add_{$key}"; if (isset($methods[$method])) { $this->{$method}($request, $context, $value, $params); } } } if (isset($options['stream_context'])) { if (!is_array($options['stream_context'])) { throw new \InvalidArgumentException('stream_context must be an array'); } $context = array_replace_recursive( $context, $options['stream_context'] ); } // Microsoft NTLM authentication only supported with curl handler if (isset($options['auth']) && is_array($options['auth']) && isset($options['auth'][2]) && 'ntlm' == $options['auth'][2] ) { throw new \InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler'); } $uri = $this->resolveHost($request, $options); $context = $this->createResource( function () use ($context, $params) { return stream_context_create($context, $params); } ); return $this->createResource( function () use ($uri, &$http_response_header, $context, $options) { $resource = fopen((string) $uri, 'r', null, $context); $this->lastHeaders = $http_response_header; if (isset($options['read_timeout'])) { $readTimeout = $options['read_timeout']; $sec = (int) $readTimeout; $usec = ($readTimeout - $sec) * 100000; stream_set_timeout($resource, $sec, $usec); } return $resource; } ); } private function resolveHost(RequestInterface $request, array $options) { $uri = $request->getUri(); if (isset($options['force_ip_resolve']) && !filter_var($uri->getHost(), FILTER_VALIDATE_IP)) { if ('v4' === $options['force_ip_resolve']) { $records = dns_get_record($uri->getHost(), DNS_A); if (!isset($records[0]['ip'])) { throw new ConnectException( sprintf( "Could not resolve IPv4 address for host '%s'", $uri->getHost() ), $request ); } $uri = $uri->withHost($records[0]['ip']); } elseif ('v6' === $options['force_ip_resolve']) { $records = dns_get_record($uri->getHost(), DNS_AAAA); if (!isset($records[0]['ipv6'])) { throw new ConnectException( sprintf( "Could not resolve IPv6 address for host '%s'", $uri->getHost() ), $request ); } $uri = $uri->withHost('[' . $records[0]['ipv6'] . ']'); } } return $uri; } private function getDefaultContext(RequestInterface $request) { $headers = ''; foreach ($request->getHeaders() as $name => $value) { foreach ($value as $val) { $headers .= "$name: $val\r\n"; } } $context = [ 'http' => [ 'method' => $request->getMethod(), 'header' => $headers, 'protocol_version' => $request->getProtocolVersion(), 'ignore_errors' => true, 'follow_location' => 0, ], ]; $body = (string) $request->getBody(); if (!empty($body)) { $context['http']['content'] = $body; // Prevent the HTTP handler from adding a Content-Type header. if (!$request->hasHeader('Content-Type')) { $context['http']['header'] .= "Content-Type:\r\n"; } } $context['http']['header'] = rtrim($context['http']['header']); return $context; } private function add_proxy(RequestInterface $request, &$options, $value, &$params) { if (!is_array($value)) { $options['http']['proxy'] = $value; } else { $scheme = $request->getUri()->getScheme(); if (isset($value[$scheme])) { if (!isset($value['no']) || !\GuzzleHttp\is_host_in_noproxy( $request->getUri()->getHost(), $value['no'] ) ) { $options['http']['proxy'] = $value[$scheme]; } } } } private function add_timeout(RequestInterface $request, &$options, $value, &$params) { if ($value > 0) { $options['http']['timeout'] = $value; } } private function add_verify(RequestInterface $request, &$options, $value, &$params) { if ($value === true) { // PHP 5.6 or greater will find the system cert by default. When // < 5.6, use the Guzzle bundled cacert. if (PHP_VERSION_ID < 50600) { $options['ssl']['cafile'] = \GuzzleHttp\default_ca_bundle(); } } elseif (is_string($value)) { $options['ssl']['cafile'] = $value; if (!file_exists($value)) { throw new \RuntimeException("SSL CA bundle not found: $value"); } } elseif ($value === false) { $options['ssl']['verify_peer'] = false; $options['ssl']['verify_peer_name'] = false; return; } else { throw new \InvalidArgumentException('Invalid verify request option'); } $options['ssl']['verify_peer'] = true; $options['ssl']['verify_peer_name'] = true; $options['ssl']['allow_self_signed'] = false; } private function add_cert(RequestInterface $request, &$options, $value, &$params) { if (is_array($value)) { $options['ssl']['passphrase'] = $value[1]; $value = $value[0]; } if (!file_exists($value)) { throw new \RuntimeException("SSL certificate not found: {$value}"); } $options['ssl']['local_cert'] = $value; } private function add_progress(RequestInterface $request, &$options, $value, &$params) { $this->addNotification( $params, function ($code, $a, $b, $c, $transferred, $total) use ($value) { if ($code == STREAM_NOTIFY_PROGRESS) { $value($total, $transferred, null, null); } } ); } private function add_debug(RequestInterface $request, &$options, $value, &$params) { if ($value === false) { return; } static $map = [ STREAM_NOTIFY_CONNECT => 'CONNECT', STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED', STREAM_NOTIFY_AUTH_RESULT => 'AUTH_RESULT', STREAM_NOTIFY_MIME_TYPE_IS => 'MIME_TYPE_IS', STREAM_NOTIFY_FILE_SIZE_IS => 'FILE_SIZE_IS', STREAM_NOTIFY_REDIRECTED => 'REDIRECTED', STREAM_NOTIFY_PROGRESS => 'PROGRESS', STREAM_NOTIFY_FAILURE => 'FAILURE', STREAM_NOTIFY_COMPLETED => 'COMPLETED', STREAM_NOTIFY_RESOLVE => 'RESOLVE', ]; static $args = ['severity', 'message', 'message_code', 'bytes_transferred', 'bytes_max']; $value = \GuzzleHttp\debug_resource($value); $ident = $request->getMethod() . ' ' . $request->getUri()->withFragment(''); $this->addNotification( $params, function () use ($ident, $value, $map, $args) { $passed = func_get_args(); $code = array_shift($passed); fprintf($value, '<%s> [%s] ', $ident, $map[$code]); foreach (array_filter($passed) as $i => $v) { fwrite($value, $args[$i] . ': "' . $v . '" '); } fwrite($value, "\n"); } ); } private function addNotification(array &$params, callable $notify) { // Wrap the existing function if needed. if (!isset($params['notification'])) { $params['notification'] = $notify; } else { $params['notification'] = $this->callArray([ $params['notification'], $notify ]); } } private function callArray(array $functions) { return function () use ($functions) { $args = func_get_args(); foreach ($functions as $fn) { call_user_func_array($fn, $args); } }; } } <?php namespace GuzzleHttp\Handler; use Psr\Http\Message\RequestInterface; interface CurlFactoryInterface { /** * Creates a cURL handle resource. * * @param RequestInterface $request Request * @param array $options Transfer options * * @return EasyHandle * @throws \RuntimeException when an option cannot be applied */ public function create(RequestInterface $request, array $options); /** * Release an easy handle, allowing it to be reused or closed. * * This function must call unset on the easy handle's "handle" property. * * @param EasyHandle $easy */ public function release(EasyHandle $easy); } <?php namespace GuzzleHttp\Handler; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\HandlerStack; use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\Promise\RejectedPromise; use GuzzleHttp\TransferStats; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** * Handler that returns responses or throw exceptions from a queue. */ class MockHandler implements \Countable { private $queue = []; private $lastRequest; private $lastOptions; private $onFulfilled; private $onRejected; /** * Creates a new MockHandler that uses the default handler stack list of * middlewares. * * @param array $queue Array of responses, callables, or exceptions. * @param callable $onFulfilled Callback to invoke when the return value is fulfilled. * @param callable $onRejected Callback to invoke when the return value is rejected. * * @return HandlerStack */ public static function createWithMiddleware( array $queue = null, callable $onFulfilled = null, callable $onRejected = null ) { return HandlerStack::create(new self($queue, $onFulfilled, $onRejected)); } /** * The passed in value must be an array of * {@see Psr7\Http\Message\ResponseInterface} objects, Exceptions, * callables, or Promises. * * @param array $queue * @param callable $onFulfilled Callback to invoke when the return value is fulfilled. * @param callable $onRejected Callback to invoke when the return value is rejected. */ public function __construct( array $queue = null, callable $onFulfilled = null, callable $onRejected = null ) { $this->onFulfilled = $onFulfilled; $this->onRejected = $onRejected; if ($queue) { call_user_func_array([$this, 'append'], $queue); } } public function __invoke(RequestInterface $request, array $options) { if (!$this->queue) { throw new \OutOfBoundsException('Mock queue is empty'); } if (isset($options['delay']) && is_numeric($options['delay'])) { usleep($options['delay'] * 1000); } $this->lastRequest = $request; $this->lastOptions = $options; $response = array_shift($this->queue); if (isset($options['on_headers'])) { if (!is_callable($options['on_headers'])) { throw new \InvalidArgumentException('on_headers must be callable'); } try { $options['on_headers']($response); } catch (\Exception $e) { $msg = 'An error was encountered during the on_headers event'; $response = new RequestException($msg, $request, $response, $e); } } if (is_callable($response)) { $response = call_user_func($response, $request, $options); } $response = $response instanceof \Exception ? \GuzzleHttp\Promise\rejection_for($response) : \GuzzleHttp\Promise\promise_for($response); return $response->then( function ($value) use ($request, $options) { $this->invokeStats($request, $options, $value); if ($this->onFulfilled) { call_user_func($this->onFulfilled, $value); } if (isset($options['sink'])) { $contents = (string) $value->getBody(); $sink = $options['sink']; if (is_resource($sink)) { fwrite($sink, $contents); } elseif (is_string($sink)) { file_put_contents($sink, $contents); } elseif ($sink instanceof \Psr\Http\Message\StreamInterface) { $sink->write($contents); } } return $value; }, function ($reason) use ($request, $options) { $this->invokeStats($request, $options, null, $reason); if ($this->onRejected) { call_user_func($this->onRejected, $reason); } return \GuzzleHttp\Promise\rejection_for($reason); } ); } /** * Adds one or more variadic requests, exceptions, callables, or promises * to the queue. */ public function append() { foreach (func_get_args() as $value) { if ($value instanceof ResponseInterface || $value instanceof \Exception || $value instanceof PromiseInterface || is_callable($value) ) { $this->queue[] = $value; } else { throw new \InvalidArgumentException('Expected a response or ' . 'exception. Found ' . \GuzzleHttp\describe_type($value)); } } } /** * Get the last received request. * * @return RequestInterface */ public function getLastRequest() { return $this->lastRequest; } /** * Get the last received request options. * * @return array */ public function getLastOptions() { return $this->lastOptions; } /** * Returns the number of remaining items in the queue. * * @return int */ public function count() { return count($this->queue); } public function reset() { $this->queue = []; } private function invokeStats( RequestInterface $request, array $options, ResponseInterface $response = null, $reason = null ) { if (isset($options['on_stats'])) { $transferTime = isset($options['transfer_time']) ? $options['transfer_time'] : 0; $stats = new TransferStats($request, $response, $transferTime, $reason); call_user_func($options['on_stats'], $stats); } } } <?php namespace GuzzleHttp\Handler; use GuzzleHttp\Exception\InvalidArgumentException; use GuzzleHttp\Promise as P; use GuzzleHttp\Promise\Promise; use Psr\Http\Message\RequestInterface; /** * Returns an asynchronous response using curl_multi_* functions. * * When using the CurlMultiHandler, custom curl options can be specified as an * associative array of curl option constants mapping to values in the * **curl** key of the provided request options. * * @property resource $_mh Internal use only. Lazy loaded multi-handle. */ class CurlMultiHandler { /** @var CurlFactoryInterface */ private $factory; private $selectTimeout; private $active; private $handles = []; private $delays = []; private $options = []; /** * This handler accepts the following options: * * - handle_factory: An optional factory used to create curl handles * - select_timeout: Optional timeout (in seconds) to block before timing * out while selecting curl handles. Defaults to 1 second. * - options: An associative array of CURLMOPT_* options and * corresponding values for curl_multi_setopt() * * @param array $options */ public function __construct(array $options = []) { $this->factory = isset($options['handle_factory']) ? $options['handle_factory'] : new CurlFactory(50); if (isset($options['select_timeout'])) { $this->selectTimeout = $options['select_timeout']; } elseif ($selectTimeout = getenv('GUZZLE_CURL_SELECT_TIMEOUT')) { $this->selectTimeout = $selectTimeout; } else { $this->selectTimeout = 1; } $this->options = isset($options['options']) ? $options['options'] : []; } public function __get($name) { if ($name === '_mh') { $this->_mh = curl_multi_init(); foreach ($this->options as $option => $value) { // A warning is raised in case of a wrong option. curl_multi_setopt($this->_mh, $option, $value); } // Further calls to _mh will return the value directly, without entering the // __get() method at all. return $this->_mh; } throw new \BadMethodCallException(); } public function __destruct() { if (isset($this->_mh)) { curl_multi_close($this->_mh); unset($this->_mh); } } public function __invoke(RequestInterface $request, array $options) { $easy = $this->factory->create($request, $options); $id = (int) $easy->handle; $promise = new Promise( [$this, 'execute'], function () use ($id) { return $this->cancel($id); } ); $this->addRequest(['easy' => $easy, 'deferred' => $promise]); return $promise; } /** * Ticks the curl event loop. */ public function tick() { // Add any delayed handles if needed. if ($this->delays) { $currentTime = \GuzzleHttp\_current_time(); foreach ($this->delays as $id => $delay) { if ($currentTime >= $delay) { unset($this->delays[$id]); curl_multi_add_handle( $this->_mh, $this->handles[$id]['easy']->handle ); } } } // Step through the task queue which may add additional requests. P\queue()->run(); if ($this->active && curl_multi_select($this->_mh, $this->selectTimeout) === -1 ) { // Perform a usleep if a select returns -1. // See: https://bugs.php.net/bug.php?id=61141 usleep(250); } while (curl_multi_exec($this->_mh, $this->active) === CURLM_CALL_MULTI_PERFORM); $this->processMessages(); } /** * Runs until all outstanding connections have completed. */ public function execute() { $queue = P\queue(); while ($this->handles || !$queue->isEmpty()) { // If there are no transfers, then sleep for the next delay if (!$this->active && $this->delays) { usleep($this->timeToNext()); } $this->tick(); } } private function addRequest(array $entry) { $easy = $entry['easy']; $id = (int) $easy->handle; $this->handles[$id] = $entry; if (empty($easy->options['delay'])) { curl_multi_add_handle($this->_mh, $easy->handle); } else { $this->delays[$id] = \GuzzleHttp\_current_time() + ($easy->options['delay'] / 1000); } } /** * Cancels a handle from sending and removes references to it. * * @param int $id Handle ID to cancel and remove. * * @return bool True on success, false on failure. */ private function cancel($id) { // Cannot cancel if it has been processed. if (!isset($this->handles[$id])) { return false; } $handle = $this->handles[$id]['easy']->handle; unset($this->delays[$id], $this->handles[$id]); curl_multi_remove_handle($this->_mh, $handle); curl_close($handle); return true; } private function processMessages() { while ($done = curl_multi_info_read($this->_mh)) { $id = (int) $done['handle']; curl_multi_remove_handle($this->_mh, $done['handle']); if (!isset($this->handles[$id])) { // Probably was cancelled. continue; } $entry = $this->handles[$id]; unset($this->handles[$id], $this->delays[$id]); $entry['easy']->errno = $done['result']; $entry['deferred']->resolve( CurlFactory::finish( $this, $entry['easy'], $this->factory ) ); } } private function timeToNext() { $currentTime = \GuzzleHttp\_current_time(); $nextTime = PHP_INT_MAX; foreach ($this->delays as $time) { if ($time < $nextTime) { $nextTime = $time; } } return max(0, $nextTime - $currentTime) * 1000000; } } <?php namespace GuzzleHttp\Handler; use GuzzleHttp\Psr7; use Psr\Http\Message\RequestInterface; /** * HTTP handler that uses cURL easy handles as a transport layer. * * When using the CurlHandler, custom curl options can be specified as an * associative array of curl option constants mapping to values in the * **curl** key of the "client" key of the request. */ class CurlHandler { /** @var CurlFactoryInterface */ private $factory; /** * Accepts an associative array of options: * * - factory: Optional curl factory used to create cURL handles. * * @param array $options Array of options to use with the handler */ public function __construct(array $options = []) { $this->factory = isset($options['handle_factory']) ? $options['handle_factory'] : new CurlFactory(3); } public function __invoke(RequestInterface $request, array $options) { if (isset($options['delay'])) { usleep($options['delay'] * 1000); } $easy = $this->factory->create($request, $options); curl_exec($easy->handle); $easy->errno = curl_errno($easy->handle); return CurlFactory::finish($this, $easy, $this->factory); } } <?php namespace GuzzleHttp\Handler; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Promise\FulfilledPromise; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\LazyOpenStream; use GuzzleHttp\TransferStats; use Psr\Http\Message\RequestInterface; /** * Creates curl resources from a request */ class CurlFactory implements CurlFactoryInterface { const CURL_VERSION_STR = 'curl_version'; const LOW_CURL_VERSION_NUMBER = '7.21.2'; /** @var array */ private $handles = []; /** @var int Total number of idle handles to keep in cache */ private $maxHandles; /** * @param int $maxHandles Maximum number of idle handles. */ public function __construct($maxHandles) { $this->maxHandles = $maxHandles; } public function create(RequestInterface $request, array $options) { if (isset($options['curl']['body_as_string'])) { $options['_body_as_string'] = $options['curl']['body_as_string']; unset($options['curl']['body_as_string']); } $easy = new EasyHandle; $easy->request = $request; $easy->options = $options; $conf = $this->getDefaultConf($easy); $this->applyMethod($easy, $conf); $this->applyHandlerOptions($easy, $conf); $this->applyHeaders($easy, $conf); unset($conf['_headers']); // Add handler options from the request configuration options if (isset($options['curl'])) { $conf = array_replace($conf, $options['curl']); } $conf[CURLOPT_HEADERFUNCTION] = $this->createHeaderFn($easy); $easy->handle = $this->handles ? array_pop($this->handles) : curl_init(); curl_setopt_array($easy->handle, $conf); return $easy; } public function release(EasyHandle $easy) { $resource = $easy->handle; unset($easy->handle); if (count($this->handles) >= $this->maxHandles) { curl_close($resource); } else { // Remove all callback functions as they can hold onto references // and are not cleaned up by curl_reset. Using curl_setopt_array // does not work for some reason, so removing each one // individually. curl_setopt($resource, CURLOPT_HEADERFUNCTION, null); curl_setopt($resource, CURLOPT_READFUNCTION, null); curl_setopt($resource, CURLOPT_WRITEFUNCTION, null); curl_setopt($resource, CURLOPT_PROGRESSFUNCTION, null); curl_reset($resource); $this->handles[] = $resource; } } /** * Completes a cURL transaction, either returning a response promise or a * rejected promise. * * @param callable $handler * @param EasyHandle $easy * @param CurlFactoryInterface $factory Dictates how the handle is released * * @return \GuzzleHttp\Promise\PromiseInterface */ public static function finish( callable $handler, EasyHandle $easy, CurlFactoryInterface $factory ) { if (isset($easy->options['on_stats'])) { self::invokeStats($easy); } if (!$easy->response || $easy->errno) { return self::finishError($handler, $easy, $factory); } // Return the response if it is present and there is no error. $factory->release($easy); // Rewind the body of the response if possible. $body = $easy->response->getBody(); if ($body->isSeekable()) { $body->rewind(); } return new FulfilledPromise($easy->response); } private static function invokeStats(EasyHandle $easy) { $curlStats = curl_getinfo($easy->handle); $curlStats['appconnect_time'] = curl_getinfo($easy->handle, CURLINFO_APPCONNECT_TIME); $stats = new TransferStats( $easy->request, $easy->response, $curlStats['total_time'], $easy->errno, $curlStats ); call_user_func($easy->options['on_stats'], $stats); } private static function finishError( callable $handler, EasyHandle $easy, CurlFactoryInterface $factory ) { // Get error information and release the handle to the factory. $ctx = [ 'errno' => $easy->errno, 'error' => curl_error($easy->handle), 'appconnect_time' => curl_getinfo($easy->handle, CURLINFO_APPCONNECT_TIME), ] + curl_getinfo($easy->handle); $ctx[self::CURL_VERSION_STR] = curl_version()['version']; $factory->release($easy); // Retry when nothing is present or when curl failed to rewind. if (empty($easy->options['_err_message']) && (!$easy->errno || $easy->errno == 65) ) { return self::retryFailedRewind($handler, $easy, $ctx); } return self::createRejection($easy, $ctx); } private static function createRejection(EasyHandle $easy, array $ctx) { static $connectionErrors = [ CURLE_OPERATION_TIMEOUTED => true, CURLE_COULDNT_RESOLVE_HOST => true, CURLE_COULDNT_CONNECT => true, CURLE_SSL_CONNECT_ERROR => true, CURLE_GOT_NOTHING => true, ]; // If an exception was encountered during the onHeaders event, then // return a rejected promise that wraps that exception. if ($easy->onHeadersException) { return \GuzzleHttp\Promise\rejection_for( new RequestException( 'An error was encountered during the on_headers event', $easy->request, $easy->response, $easy->onHeadersException, $ctx ) ); } if (version_compare($ctx[self::CURL_VERSION_STR], self::LOW_CURL_VERSION_NUMBER)) { $message = sprintf( 'cURL error %s: %s (%s)', $ctx['errno'], $ctx['error'], 'see https://curl.haxx.se/libcurl/c/libcurl-errors.html' ); } else { $message = sprintf( 'cURL error %s: %s (%s) for %s', $ctx['errno'], $ctx['error'], 'see https://curl.haxx.se/libcurl/c/libcurl-errors.html', $easy->request->getUri() ); } // Create a connection exception if it was a specific error code. $error = isset($connectionErrors[$easy->errno]) ? new ConnectException($message, $easy->request, null, $ctx) : new RequestException($message, $easy->request, $easy->response, null, $ctx); return \GuzzleHttp\Promise\rejection_for($error); } private function getDefaultConf(EasyHandle $easy) { $conf = [ '_headers' => $easy->request->getHeaders(), CURLOPT_CUSTOMREQUEST => $easy->request->getMethod(), CURLOPT_URL => (string) $easy->request->getUri()->withFragment(''), CURLOPT_RETURNTRANSFER => false, CURLOPT_HEADER => false, CURLOPT_CONNECTTIMEOUT => 150, ]; if (defined('CURLOPT_PROTOCOLS')) { $conf[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; } $version = $easy->request->getProtocolVersion(); if ($version == 1.1) { $conf[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; } elseif ($version == 2.0) { $conf[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_2_0; } else { $conf[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0; } return $conf; } private function applyMethod(EasyHandle $easy, array &$conf) { $body = $easy->request->getBody(); $size = $body->getSize(); if ($size === null || $size > 0) { $this->applyBody($easy->request, $easy->options, $conf); return; } $method = $easy->request->getMethod(); if ($method === 'PUT' || $method === 'POST') { // See http://tools.ietf.org/html/rfc7230#section-3.3.2 if (!$easy->request->hasHeader('Content-Length')) { $conf[CURLOPT_HTTPHEADER][] = 'Content-Length: 0'; } } elseif ($method === 'HEAD') { $conf[CURLOPT_NOBODY] = true; unset( $conf[CURLOPT_WRITEFUNCTION], $conf[CURLOPT_READFUNCTION], $conf[CURLOPT_FILE], $conf[CURLOPT_INFILE] ); } } private function applyBody(RequestInterface $request, array $options, array &$conf) { $size = $request->hasHeader('Content-Length') ? (int) $request->getHeaderLine('Content-Length') : null; // Send the body as a string if the size is less than 1MB OR if the // [curl][body_as_string] request value is set. if (($size !== null && $size < 1000000) || !empty($options['_body_as_string']) ) { $conf[CURLOPT_POSTFIELDS] = (string) $request->getBody(); // Don't duplicate the Content-Length header $this->removeHeader('Content-Length', $conf); $this->removeHeader('Transfer-Encoding', $conf); } else { $conf[CURLOPT_UPLOAD] = true; if ($size !== null) { $conf[CURLOPT_INFILESIZE] = $size; $this->removeHeader('Content-Length', $conf); } $body = $request->getBody(); if ($body->isSeekable()) { $body->rewind(); } $conf[CURLOPT_READFUNCTION] = function ($ch, $fd, $length) use ($body) { return $body->read($length); }; } // If the Expect header is not present, prevent curl from adding it if (!$request->hasHeader('Expect')) { $conf[CURLOPT_HTTPHEADER][] = 'Expect:'; } // cURL sometimes adds a content-type by default. Prevent this. if (!$request->hasHeader('Content-Type')) { $conf[CURLOPT_HTTPHEADER][] = 'Content-Type:'; } } private function applyHeaders(EasyHandle $easy, array &$conf) { foreach ($conf['_headers'] as $name => $values) { foreach ($values as $value) { $value = (string) $value; if ($value === '') { // cURL requires a special format for empty headers. // See https://github.com/guzzle/guzzle/issues/1882 for more details. $conf[CURLOPT_HTTPHEADER][] = "$name;"; } else { $conf[CURLOPT_HTTPHEADER][] = "$name: $value"; } } } // Remove the Accept header if one was not set if (!$easy->request->hasHeader('Accept')) { $conf[CURLOPT_HTTPHEADER][] = 'Accept:'; } } /** * Remove a header from the options array. * * @param string $name Case-insensitive header to remove * @param array $options Array of options to modify */ private function removeHeader($name, array &$options) { foreach (array_keys($options['_headers']) as $key) { if (!strcasecmp($key, $name)) { unset($options['_headers'][$key]); return; } } } private function applyHandlerOptions(EasyHandle $easy, array &$conf) { $options = $easy->options; if (isset($options['verify'])) { if ($options['verify'] === false) { unset($conf[CURLOPT_CAINFO]); $conf[CURLOPT_SSL_VERIFYHOST] = 0; $conf[CURLOPT_SSL_VERIFYPEER] = false; } else { $conf[CURLOPT_SSL_VERIFYHOST] = 2; $conf[CURLOPT_SSL_VERIFYPEER] = true; if (is_string($options['verify'])) { // Throw an error if the file/folder/link path is not valid or doesn't exist. if (!file_exists($options['verify'])) { throw new \InvalidArgumentException( "SSL CA bundle not found: {$options['verify']}" ); } // If it's a directory or a link to a directory use CURLOPT_CAPATH. // If not, it's probably a file, or a link to a file, so use CURLOPT_CAINFO. if (is_dir($options['verify']) || (is_link($options['verify']) && is_dir(readlink($options['verify'])))) { $conf[CURLOPT_CAPATH] = $options['verify']; } else { $conf[CURLOPT_CAINFO] = $options['verify']; } } } } if (!empty($options['decode_content'])) { $accept = $easy->request->getHeaderLine('Accept-Encoding'); if ($accept) { $conf[CURLOPT_ENCODING] = $accept; } else { $conf[CURLOPT_ENCODING] = ''; // Don't let curl send the header over the wire $conf[CURLOPT_HTTPHEADER][] = 'Accept-Encoding:'; } } if (isset($options['sink'])) { $sink = $options['sink']; if (!is_string($sink)) { $sink = \GuzzleHttp\Psr7\stream_for($sink); } elseif (!is_dir(dirname($sink))) { // Ensure that the directory exists before failing in curl. throw new \RuntimeException(sprintf( 'Directory %s does not exist for sink value of %s', dirname($sink), $sink )); } else { $sink = new LazyOpenStream($sink, 'w+'); } $easy->sink = $sink; $conf[CURLOPT_WRITEFUNCTION] = function ($ch, $write) use ($sink) { return $sink->write($write); }; } else { // Use a default temp stream if no sink was set. $conf[CURLOPT_FILE] = fopen('php://temp', 'w+'); $easy->sink = Psr7\stream_for($conf[CURLOPT_FILE]); } $timeoutRequiresNoSignal = false; if (isset($options['timeout'])) { $timeoutRequiresNoSignal |= $options['timeout'] < 1; $conf[CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000; } // CURL default value is CURL_IPRESOLVE_WHATEVER if (isset($options['force_ip_resolve'])) { if ('v4' === $options['force_ip_resolve']) { $conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4; } elseif ('v6' === $options['force_ip_resolve']) { $conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V6; } } if (isset($options['connect_timeout'])) { $timeoutRequiresNoSignal |= $options['connect_timeout'] < 1; $conf[CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000; } if ($timeoutRequiresNoSignal && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { $conf[CURLOPT_NOSIGNAL] = true; } if (isset($options['proxy'])) { if (!is_array($options['proxy'])) { $conf[CURLOPT_PROXY] = $options['proxy']; } else { $scheme = $easy->request->getUri()->getScheme(); if (isset($options['proxy'][$scheme])) { $host = $easy->request->getUri()->getHost(); if (!isset($options['proxy']['no']) || !\GuzzleHttp\is_host_in_noproxy($host, $options['proxy']['no']) ) { $conf[CURLOPT_PROXY] = $options['proxy'][$scheme]; } } } } if (isset($options['cert'])) { $cert = $options['cert']; if (is_array($cert)) { $conf[CURLOPT_SSLCERTPASSWD] = $cert[1]; $cert = $cert[0]; } if (!file_exists($cert)) { throw new \InvalidArgumentException( "SSL certificate not found: {$cert}" ); } $conf[CURLOPT_SSLCERT] = $cert; } if (isset($options['ssl_key'])) { if (is_array($options['ssl_key'])) { if (count($options['ssl_key']) === 2) { list($sslKey, $conf[CURLOPT_SSLKEYPASSWD]) = $options['ssl_key']; } else { list($sslKey) = $options['ssl_key']; } } $sslKey = isset($sslKey) ? $sslKey: $options['ssl_key']; if (!file_exists($sslKey)) { throw new \InvalidArgumentException( "SSL private key not found: {$sslKey}" ); } $conf[CURLOPT_SSLKEY] = $sslKey; } if (isset($options['progress'])) { $progress = $options['progress']; if (!is_callable($progress)) { throw new \InvalidArgumentException( 'progress client option must be callable' ); } $conf[CURLOPT_NOPROGRESS] = false; $conf[CURLOPT_PROGRESSFUNCTION] = function () use ($progress) { $args = func_get_args(); // PHP 5.5 pushed the handle onto the start of the args if (is_resource($args[0])) { array_shift($args); } call_user_func_array($progress, $args); }; } if (!empty($options['debug'])) { $conf[CURLOPT_STDERR] = \GuzzleHttp\debug_resource($options['debug']); $conf[CURLOPT_VERBOSE] = true; } } /** * This function ensures that a response was set on a transaction. If one * was not set, then the request is retried if possible. This error * typically means you are sending a payload, curl encountered a * "Connection died, retrying a fresh connect" error, tried to rewind the * stream, and then encountered a "necessary data rewind wasn't possible" * error, causing the request to be sent through curl_multi_info_read() * without an error status. */ private static function retryFailedRewind( callable $handler, EasyHandle $easy, array $ctx ) { try { // Only rewind if the body has been read from. $body = $easy->request->getBody(); if ($body->tell() > 0) { $body->rewind(); } } catch (\RuntimeException $e) { $ctx['error'] = 'The connection unexpectedly failed without ' . 'providing an error. The request would have been retried, ' . 'but attempting to rewind the request body failed. ' . 'Exception: ' . $e; return self::createRejection($easy, $ctx); } // Retry no more than 3 times before giving up. if (!isset($easy->options['_curl_retries'])) { $easy->options['_curl_retries'] = 1; } elseif ($easy->options['_curl_retries'] == 2) { $ctx['error'] = 'The cURL request was retried 3 times ' . 'and did not succeed. The most likely reason for the failure ' . 'is that cURL was unable to rewind the body of the request ' . 'and subsequent retries resulted in the same error. Turn on ' . 'the debug option to see what went wrong. See ' . 'https://bugs.php.net/bug.php?id=47204 for more information.'; return self::createRejection($easy, $ctx); } else { $easy->options['_curl_retries']++; } return $handler($easy->request, $easy->options); } private function createHeaderFn(EasyHandle $easy) { if (isset($easy->options['on_headers'])) { $onHeaders = $easy->options['on_headers']; if (!is_callable($onHeaders)) { throw new \InvalidArgumentException('on_headers must be callable'); } } else { $onHeaders = null; } return function ($ch, $h) use ( $onHeaders, $easy, &$startingResponse ) { $value = trim($h); if ($value === '') { $startingResponse = true; $easy->createResponse(); if ($onHeaders !== null) { try { $onHeaders($easy->response); } catch (\Exception $e) { // Associate the exception with the handle and trigger // a curl header write error by returning 0. $easy->onHeadersException = $e; return -1; } } } elseif ($startingResponse) { $startingResponse = false; $easy->headers = [$value]; } else { $easy->headers[] = $value; } return strlen($h); }; } } <?php namespace GuzzleHttp; /** * Expands URI templates. Userland implementation of PECL uri_template. * * @link http://tools.ietf.org/html/rfc6570 */ class UriTemplate { /** @var string URI template */ private $template; /** @var array Variables to use in the template expansion */ private $variables; /** @var array Hash for quick operator lookups */ private static $operatorHash = [ '' => ['prefix' => '', 'joiner' => ',', 'query' => false], '+' => ['prefix' => '', 'joiner' => ',', 'query' => false], '#' => ['prefix' => '#', 'joiner' => ',', 'query' => false], '.' => ['prefix' => '.', 'joiner' => '.', 'query' => false], '/' => ['prefix' => '/', 'joiner' => '/', 'query' => false], ';' => ['prefix' => ';', 'joiner' => ';', 'query' => true], '?' => ['prefix' => '?', 'joiner' => '&', 'query' => true], '&' => ['prefix' => '&', 'joiner' => '&', 'query' => true] ]; /** @var array Delimiters */ private static $delims = [':', '/', '?', '#', '[', ']', '@', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=']; /** @var array Percent encoded delimiters */ private static $delimsPct = ['%3A', '%2F', '%3F', '%23', '%5B', '%5D', '%40', '%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3D']; public function expand($template, array $variables) { if (false === strpos($template, '{')) { return $template; } $this->template = $template; $this->variables = $variables; return preg_replace_callback( '/\{([^\}]+)\}/', [$this, 'expandMatch'], $this->template ); } /** * Parse an expression into parts * * @param string $expression Expression to parse * * @return array Returns an associative array of parts */ private function parseExpression($expression) { $result = []; if (isset(self::$operatorHash[$expression[0]])) { $result['operator'] = $expression[0]; $expression = substr($expression, 1); } else { $result['operator'] = ''; } foreach (explode(',', $expression) as $value) { $value = trim($value); $varspec = []; if ($colonPos = strpos($value, ':')) { $varspec['value'] = substr($value, 0, $colonPos); $varspec['modifier'] = ':'; $varspec['position'] = (int) substr($value, $colonPos + 1); } elseif (substr($value, -1) === '*') { $varspec['modifier'] = '*'; $varspec['value'] = substr($value, 0, -1); } else { $varspec['value'] = (string) $value; $varspec['modifier'] = ''; } $result['values'][] = $varspec; } return $result; } /** * Process an expansion * * @param array $matches Matches met in the preg_replace_callback * * @return string Returns the replacement string */ private function expandMatch(array $matches) { static $rfc1738to3986 = ['+' => '%20', '%7e' => '~']; $replacements = []; $parsed = self::parseExpression($matches[1]); $prefix = self::$operatorHash[$parsed['operator']]['prefix']; $joiner = self::$operatorHash[$parsed['operator']]['joiner']; $useQuery = self::$operatorHash[$parsed['operator']]['query']; foreach ($parsed['values'] as $value) { if (!isset($this->variables[$value['value']])) { continue; } $variable = $this->variables[$value['value']]; $actuallyUseQuery = $useQuery; $expanded = ''; if (is_array($variable)) { $isAssoc = $this->isAssoc($variable); $kvp = []; foreach ($variable as $key => $var) { if ($isAssoc) { $key = rawurlencode($key); $isNestedArray = is_array($var); } else { $isNestedArray = false; } if (!$isNestedArray) { $var = rawurlencode($var); if ($parsed['operator'] === '+' || $parsed['operator'] === '#' ) { $var = $this->decodeReserved($var); } } if ($value['modifier'] === '*') { if ($isAssoc) { if ($isNestedArray) { // Nested arrays must allow for deeply nested // structures. $var = strtr( http_build_query([$key => $var]), $rfc1738to3986 ); } else { $var = $key . '=' . $var; } } elseif ($key > 0 && $actuallyUseQuery) { $var = $value['value'] . '=' . $var; } } $kvp[$key] = $var; } if (empty($variable)) { $actuallyUseQuery = false; } elseif ($value['modifier'] === '*') { $expanded = implode($joiner, $kvp); if ($isAssoc) { // Don't prepend the value name when using the explode // modifier with an associative array. $actuallyUseQuery = false; } } else { if ($isAssoc) { // When an associative array is encountered and the // explode modifier is not set, then the result must be // a comma separated list of keys followed by their // respective values. foreach ($kvp as $k => &$v) { $v = $k . ',' . $v; } } $expanded = implode(',', $kvp); } } else { if ($value['modifier'] === ':') { $variable = substr($variable, 0, $value['position']); } $expanded = rawurlencode($variable); if ($parsed['operator'] === '+' || $parsed['operator'] === '#') { $expanded = $this->decodeReserved($expanded); } } if ($actuallyUseQuery) { if (!$expanded && $joiner !== '&') { $expanded = $value['value']; } else { $expanded = $value['value'] . '=' . $expanded; } } $replacements[] = $expanded; } $ret = implode($joiner, $replacements); if ($ret && $prefix) { return $prefix . $ret; } return $ret; } /** * Determines if an array is associative. * * This makes the assumption that input arrays are sequences or hashes. * This assumption is a tradeoff for accuracy in favor of speed, but it * should work in almost every case where input is supplied for a URI * template. * * @param array $array Array to check * * @return bool */ private function isAssoc(array $array) { return $array && array_keys($array)[0] !== 0; } /** * Removes percent encoding on reserved characters (used with + and # * modifiers). * * @param string $string String to fix * * @return string */ private function decodeReserved($string) { return str_replace(self::$delimsPct, self::$delims, $string); } } <?php namespace GuzzleHttp\Cookie; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** * Stores HTTP cookies. * * It extracts cookies from HTTP requests, and returns them in HTTP responses. * CookieJarInterface instances automatically expire contained cookies when * necessary. Subclasses are also responsible for storing and retrieving * cookies from a file, database, etc. * * @link http://docs.python.org/2/library/cookielib.html Inspiration */ interface CookieJarInterface extends \Countable, \IteratorAggregate { /** * Create a request with added cookie headers. * * If no matching cookies are found in the cookie jar, then no Cookie * header is added to the request and the same request is returned. * * @param RequestInterface $request Request object to modify. * * @return RequestInterface returns the modified request. */ public function withCookieHeader(RequestInterface $request); /** * Extract cookies from an HTTP response and store them in the CookieJar. * * @param RequestInterface $request Request that was sent * @param ResponseInterface $response Response that was received */ public function extractCookies( RequestInterface $request, ResponseInterface $response ); /** * Sets a cookie in the cookie jar. * * @param SetCookie $cookie Cookie to set. * * @return bool Returns true on success or false on failure */ public function setCookie(SetCookie $cookie); /** * Remove cookies currently held in the cookie jar. * * Invoking this method without arguments will empty the whole cookie jar. * If given a $domain argument only cookies belonging to that domain will * be removed. If given a $domain and $path argument, cookies belonging to * the specified path within that domain are removed. If given all three * arguments, then the cookie with the specified name, path and domain is * removed. * * @param string|null $domain Clears cookies matching a domain * @param string|null $path Clears cookies matching a domain and path * @param string|null $name Clears cookies matching a domain, path, and name * * @return CookieJarInterface */ public function clear($domain = null, $path = null, $name = null); /** * Discard all sessions cookies. * * Removes cookies that don't have an expire field or a have a discard * field set to true. To be called when the user agent shuts down according * to RFC 2965. */ public function clearSessionCookies(); /** * Converts the cookie jar to an array. * * @return array */ public function toArray(); } <?php namespace GuzzleHttp\Cookie; /** * Persists cookies in the client session */ class SessionCookieJar extends CookieJar { /** @var string session key */ private $sessionKey; /** @var bool Control whether to persist session cookies or not. */ private $storeSessionCookies; /** * Create a new SessionCookieJar object * * @param string $sessionKey Session key name to store the cookie * data in session * @param bool $storeSessionCookies Set to true to store session cookies * in the cookie jar. */ public function __construct($sessionKey, $storeSessionCookies = false) { parent::__construct(); $this->sessionKey = $sessionKey; $this->storeSessionCookies = $storeSessionCookies; $this->load(); } /** * Saves cookies to session when shutting down */ public function __destruct() { $this->save(); } /** * Save cookies to the client session */ public function save() { $json = []; foreach ($this as $cookie) { /** @var SetCookie $cookie */ if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { $json[] = $cookie->toArray(); } } $_SESSION[$this->sessionKey] = json_encode($json); } /** * Load the contents of the client session into the data array */ protected function load() { if (!isset($_SESSION[$this->sessionKey])) { return; } $data = json_decode($_SESSION[$this->sessionKey], true); if (is_array($data)) { foreach ($data as $cookie) { $this->setCookie(new SetCookie($cookie)); } } elseif (strlen($data)) { throw new \RuntimeException("Invalid cookie data"); } } } <?php namespace GuzzleHttp\Cookie; /** * Set-Cookie object */ class SetCookie { /** @var array */ private static $defaults = [ 'Name' => null, 'Value' => null, 'Domain' => null, 'Path' => '/', 'Max-Age' => null, 'Expires' => null, 'Secure' => false, 'Discard' => false, 'HttpOnly' => false ]; /** @var array Cookie data */ private $data; /** * Create a new SetCookie object from a string * * @param string $cookie Set-Cookie header string * * @return self */ public static function fromString($cookie) { // Create the default return array $data = self::$defaults; // Explode the cookie string using a series of semicolons $pieces = array_filter(array_map('trim', explode(';', $cookie))); // The name of the cookie (first kvp) must exist and include an equal sign. if (empty($pieces[0]) || !strpos($pieces[0], '=')) { return new self($data); } // Add the cookie pieces into the parsed data array foreach ($pieces as $part) { $cookieParts = explode('=', $part, 2); $key = trim($cookieParts[0]); $value = isset($cookieParts[1]) ? trim($cookieParts[1], " \n\r\t\0\x0B") : true; // Only check for non-cookies when cookies have been found if (empty($data['Name'])) { $data['Name'] = $key; $data['Value'] = $value; } else { foreach (array_keys(self::$defaults) as $search) { if (!strcasecmp($search, $key)) { $data[$search] = $value; continue 2; } } $data[$key] = $value; } } return new self($data); } /** * @param array $data Array of cookie data provided by a Cookie parser */ public function __construct(array $data = []) { $this->data = array_replace(self::$defaults, $data); // Extract the Expires value and turn it into a UNIX timestamp if needed if (!$this->getExpires() && $this->getMaxAge()) { // Calculate the Expires date $this->setExpires(time() + $this->getMaxAge()); } elseif ($this->getExpires() && !is_numeric($this->getExpires())) { $this->setExpires($this->getExpires()); } } public function __toString() { $str = $this->data['Name'] . '=' . $this->data['Value'] . '; '; foreach ($this->data as $k => $v) { if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) { if ($k === 'Expires') { $str .= 'Expires=' . gmdate('D, d M Y H:i:s \G\M\T', $v) . '; '; } else { $str .= ($v === true ? $k : "{$k}={$v}") . '; '; } } } return rtrim($str, '; '); } public function toArray() { return $this->data; } /** * Get the cookie name * * @return string */ public function getName() { return $this->data['Name']; } /** * Set the cookie name * * @param string $name Cookie name */ public function setName($name) { $this->data['Name'] = $name; } /** * Get the cookie value * * @return string */ public function getValue() { return $this->data['Value']; } /** * Set the cookie value * * @param string $value Cookie value */ public function setValue($value) { $this->data['Value'] = $value; } /** * Get the domain * * @return string|null */ public function getDomain() { return $this->data['Domain']; } /** * Set the domain of the cookie * * @param string $domain */ public function setDomain($domain) { $this->data['Domain'] = $domain; } /** * Get the path * * @return string */ public function getPath() { return $this->data['Path']; } /** * Set the path of the cookie * * @param string $path Path of the cookie */ public function setPath($path) { $this->data['Path'] = $path; } /** * Maximum lifetime of the cookie in seconds * * @return int|null */ public function getMaxAge() { return $this->data['Max-Age']; } /** * Set the max-age of the cookie * * @param int $maxAge Max age of the cookie in seconds */ public function setMaxAge($maxAge) { $this->data['Max-Age'] = $maxAge; } /** * The UNIX timestamp when the cookie Expires * * @return mixed */ public function getExpires() { return $this->data['Expires']; } /** * Set the unix timestamp for which the cookie will expire * * @param int $timestamp Unix timestamp */ public function setExpires($timestamp) { $this->data['Expires'] = is_numeric($timestamp) ? (int) $timestamp : strtotime($timestamp); } /** * Get whether or not this is a secure cookie * * @return bool|null */ public function getSecure() { return $this->data['Secure']; } /** * Set whether or not the cookie is secure * * @param bool $secure Set to true or false if secure */ public function setSecure($secure) { $this->data['Secure'] = $secure; } /** * Get whether or not this is a session cookie * * @return bool|null */ public function getDiscard() { return $this->data['Discard']; } /** * Set whether or not this is a session cookie * * @param bool $discard Set to true or false if this is a session cookie */ public function setDiscard($discard) { $this->data['Discard'] = $discard; } /** * Get whether or not this is an HTTP only cookie * * @return bool */ public function getHttpOnly() { return $this->data['HttpOnly']; } /** * Set whether or not this is an HTTP only cookie * * @param bool $httpOnly Set to true or false if this is HTTP only */ public function setHttpOnly($httpOnly) { $this->data['HttpOnly'] = $httpOnly; } /** * Check if the cookie matches a path value. * * A request-path path-matches a given cookie-path if at least one of * the following conditions holds: * * - The cookie-path and the request-path are identical. * - The cookie-path is a prefix of the request-path, and the last * character of the cookie-path is %x2F ("/"). * - The cookie-path is a prefix of the request-path, and the first * character of the request-path that is not included in the cookie- * path is a %x2F ("/") character. * * @param string $requestPath Path to check against * * @return bool */ public function matchesPath($requestPath) { $cookiePath = $this->getPath(); // Match on exact matches or when path is the default empty "/" if ($cookiePath === '/' || $cookiePath == $requestPath) { return true; } // Ensure that the cookie-path is a prefix of the request path. if (0 !== strpos($requestPath, $cookiePath)) { return false; } // Match if the last character of the cookie-path is "/" if (substr($cookiePath, -1, 1) === '/') { return true; } // Match if the first character not included in cookie path is "/" return substr($requestPath, strlen($cookiePath), 1) === '/'; } /** * Check if the cookie matches a domain value * * @param string $domain Domain to check against * * @return bool */ public function matchesDomain($domain) { // Remove the leading '.' as per spec in RFC 6265. // http://tools.ietf.org/html/rfc6265#section-5.2.3 $cookieDomain = ltrim($this->getDomain(), '.'); // Domain not set or exact match. if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) { return true; } // Matching the subdomain according to RFC 6265. // http://tools.ietf.org/html/rfc6265#section-5.1.3 if (filter_var($domain, FILTER_VALIDATE_IP)) { return false; } return (bool) preg_match('/\.' . preg_quote($cookieDomain, '/') . '$/', $domain); } /** * Check if the cookie is expired * * @return bool */ public function isExpired() { return $this->getExpires() !== null && time() > $this->getExpires(); } /** * Check if the cookie is valid according to RFC 6265 * * @return bool|string Returns true if valid or an error message if invalid */ public function validate() { // Names must not be empty, but can be 0 $name = $this->getName(); if (empty($name) && !is_numeric($name)) { return 'The cookie name must not be empty'; } // Check if any of the invalid characters are present in the cookie name if (preg_match( '/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/', $name )) { return 'Cookie name must not contain invalid characters: ASCII ' . 'Control characters (0-31;127), space, tab and the ' . 'following characters: ()<>@,;:\"/?={}'; } // Value must not be empty, but can be 0 $value = $this->getValue(); if (empty($value) && !is_numeric($value)) { return 'The cookie value must not be empty'; } // Domains must not be empty, but can be 0 // A "0" is not a valid internet domain, but may be used as server name // in a private network. $domain = $this->getDomain(); if (empty($domain) && !is_numeric($domain)) { return 'The cookie domain must not be empty'; } return true; } } <?php namespace GuzzleHttp\Cookie; /** * Persists non-session cookies using a JSON formatted file */ class FileCookieJar extends CookieJar { /** @var string filename */ private $filename; /** @var bool Control whether to persist session cookies or not. */ private $storeSessionCookies; /** * Create a new FileCookieJar object * * @param string $cookieFile File to store the cookie data * @param bool $storeSessionCookies Set to true to store session cookies * in the cookie jar. * * @throws \RuntimeException if the file cannot be found or created */ public function __construct($cookieFile, $storeSessionCookies = false) { parent::__construct(); $this->filename = $cookieFile; $this->storeSessionCookies = $storeSessionCookies; if (file_exists($cookieFile)) { $this->load($cookieFile); } } /** * Saves the file when shutting down */ public function __destruct() { $this->save($this->filename); } /** * Saves the cookies to a file. * * @param string $filename File to save * @throws \RuntimeException if the file cannot be found or created */ public function save($filename) { $json = []; foreach ($this as $cookie) { /** @var SetCookie $cookie */ if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { $json[] = $cookie->toArray(); } } $jsonStr = \GuzzleHttp\json_encode($json); if (false === file_put_contents($filename, $jsonStr, LOCK_EX)) { throw new \RuntimeException("Unable to save file {$filename}"); } } /** * Load cookies from a JSON formatted file. * * Old cookies are kept unless overwritten by newly loaded ones. * * @param string $filename Cookie file to load. * @throws \RuntimeException if the file cannot be loaded. */ public function load($filename) { $json = file_get_contents($filename); if (false === $json) { throw new \RuntimeException("Unable to load file {$filename}"); } elseif ($json === '') { return; } $data = \GuzzleHttp\json_decode($json, true); if (is_array($data)) { foreach (json_decode($json, true) as $cookie) { $this->setCookie(new SetCookie($cookie)); } } elseif (strlen($data)) { throw new \RuntimeException("Invalid cookie file: {$filename}"); } } } <?php namespace GuzzleHttp\Cookie; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** * Cookie jar that stores cookies as an array */ class CookieJar implements CookieJarInterface { /** @var SetCookie[] Loaded cookie data */ private $cookies = []; /** @var bool */ private $strictMode; /** * @param bool $strictMode Set to true to throw exceptions when invalid * cookies are added to the cookie jar. * @param array $cookieArray Array of SetCookie objects or a hash of * arrays that can be used with the SetCookie * constructor */ public function __construct($strictMode = false, $cookieArray = []) { $this->strictMode = $strictMode; foreach ($cookieArray as $cookie) { if (!($cookie instanceof SetCookie)) { $cookie = new SetCookie($cookie); } $this->setCookie($cookie); } } /** * Create a new Cookie jar from an associative array and domain. * * @param array $cookies Cookies to create the jar from * @param string $domain Domain to set the cookies to * * @return self */ public static function fromArray(array $cookies, $domain) { $cookieJar = new self(); foreach ($cookies as $name => $value) { $cookieJar->setCookie(new SetCookie([ 'Domain' => $domain, 'Name' => $name, 'Value' => $value, 'Discard' => true ])); } return $cookieJar; } /** * @deprecated */ public static function getCookieValue($value) { return $value; } /** * Evaluate if this cookie should be persisted to storage * that survives between requests. * * @param SetCookie $cookie Being evaluated. * @param bool $allowSessionCookies If we should persist session cookies * @return bool */ public static function shouldPersist( SetCookie $cookie, $allowSessionCookies = false ) { if ($cookie->getExpires() || $allowSessionCookies) { if (!$cookie->getDiscard()) { return true; } } return false; } /** * Finds and returns the cookie based on the name * * @param string $name cookie name to search for * @return SetCookie|null cookie that was found or null if not found */ public function getCookieByName($name) { // don't allow a non string name if ($name === null || !is_scalar($name)) { return null; } foreach ($this->cookies as $cookie) { if ($cookie->getName() !== null && strcasecmp($cookie->getName(), $name) === 0) { return $cookie; } } return null; } public function toArray() { return array_map(function (SetCookie $cookie) { return $cookie->toArray(); }, $this->getIterator()->getArrayCopy()); } public function clear($domain = null, $path = null, $name = null) { if (!$domain) { $this->cookies = []; return; } elseif (!$path) { $this->cookies = array_filter( $this->cookies, function (SetCookie $cookie) use ($domain) { return !$cookie->matchesDomain($domain); } ); } elseif (!$name) { $this->cookies = array_filter( $this->cookies, function (SetCookie $cookie) use ($path, $domain) { return !($cookie->matchesPath($path) && $cookie->matchesDomain($domain)); } ); } else { $this->cookies = array_filter( $this->cookies, function (SetCookie $cookie) use ($path, $domain, $name) { return !($cookie->getName() == $name && $cookie->matchesPath($path) && $cookie->matchesDomain($domain)); } ); } } public function clearSessionCookies() { $this->cookies = array_filter( $this->cookies, function (SetCookie $cookie) { return !$cookie->getDiscard() && $cookie->getExpires(); } ); } public function setCookie(SetCookie $cookie) { // If the name string is empty (but not 0), ignore the set-cookie // string entirely. $name = $cookie->getName(); if (!$name && $name !== '0') { return false; } // Only allow cookies with set and valid domain, name, value $result = $cookie->validate(); if ($result !== true) { if ($this->strictMode) { throw new \RuntimeException('Invalid cookie: ' . $result); } else { $this->removeCookieIfEmpty($cookie); return false; } } // Resolve conflicts with previously set cookies foreach ($this->cookies as $i => $c) { // Two cookies are identical, when their path, and domain are // identical. if ($c->getPath() != $cookie->getPath() || $c->getDomain() != $cookie->getDomain() || $c->getName() != $cookie->getName() ) { continue; } // The previously set cookie is a discard cookie and this one is // not so allow the new cookie to be set if (!$cookie->getDiscard() && $c->getDiscard()) { unset($this->cookies[$i]); continue; } // If the new cookie's expiration is further into the future, then // replace the old cookie if ($cookie->getExpires() > $c->getExpires()) { unset($this->cookies[$i]); continue; } // If the value has changed, we better change it if ($cookie->getValue() !== $c->getValue()) { unset($this->cookies[$i]); continue; } // The cookie exists, so no need to continue return false; } $this->cookies[] = $cookie; return true; } public function count() { return count($this->cookies); } public function getIterator() { return new \ArrayIterator(array_values($this->cookies)); } public function extractCookies( RequestInterface $request, ResponseInterface $response ) { if ($cookieHeader = $response->getHeader('Set-Cookie')) { foreach ($cookieHeader as $cookie) { $sc = SetCookie::fromString($cookie); if (!$sc->getDomain()) { $sc->setDomain($request->getUri()->getHost()); } if (0 !== strpos($sc->getPath(), '/')) { $sc->setPath($this->getCookiePathFromRequest($request)); } $this->setCookie($sc); } } } /** * Computes cookie path following RFC 6265 section 5.1.4 * * @link https://tools.ietf.org/html/rfc6265#section-5.1.4 * * @param RequestInterface $request * @return string */ private function getCookiePathFromRequest(RequestInterface $request) { $uriPath = $request->getUri()->getPath(); if ('' === $uriPath) { return '/'; } if (0 !== strpos($uriPath, '/')) { return '/'; } if ('/' === $uriPath) { return '/'; } if (0 === $lastSlashPos = strrpos($uriPath, '/')) { return '/'; } return substr($uriPath, 0, $lastSlashPos); } public function withCookieHeader(RequestInterface $request) { $values = []; $uri = $request->getUri(); $scheme = $uri->getScheme(); $host = $uri->getHost(); $path = $uri->getPath() ?: '/'; foreach ($this->cookies as $cookie) { if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme === 'https') ) { $values[] = $cookie->getName() . '=' . $cookie->getValue(); } } return $values ? $request->withHeader('Cookie', implode('; ', $values)) : $request; } /** * If a cookie already exists and the server asks to set it again with a * null value, the cookie must be deleted. * * @param SetCookie $cookie */ private function removeCookieIfEmpty(SetCookie $cookie) { $cookieValue = $cookie->getValue(); if ($cookieValue === null || $cookieValue === '') { $this->clear( $cookie->getDomain(), $cookie->getPath(), $cookie->getName() ); } } } <?php namespace GuzzleHttp; use GuzzleHttp\Promise\PromiseInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** * Creates a composed Guzzle handler function by stacking middlewares on top of * an HTTP handler function. */ class HandlerStack { /** @var callable|null */ private $handler; /** @var array */ private $stack = []; /** @var callable|null */ private $cached; /** * Creates a default handler stack that can be used by clients. * * The returned handler will wrap the provided handler or use the most * appropriate default handler for your system. The returned HandlerStack has * support for cookies, redirects, HTTP error exceptions, and preparing a body * before sending. * * The returned handler stack can be passed to a client in the "handler" * option. * * @param callable $handler HTTP handler function to use with the stack. If no * handler is provided, the best handler for your * system will be utilized. * * @return HandlerStack */ public static function create(callable $handler = null) { $stack = new self($handler ?: choose_handler()); $stack->push(Middleware::httpErrors(), 'http_errors'); $stack->push(Middleware::redirect(), 'allow_redirects'); $stack->push(Middleware::cookies(), 'cookies'); $stack->push(Middleware::prepareBody(), 'prepare_body'); return $stack; } /** * @param callable $handler Underlying HTTP handler. */ public function __construct(callable $handler = null) { $this->handler = $handler; } /** * Invokes the handler stack as a composed handler * * @param RequestInterface $request * @param array $options * * @return ResponseInterface|PromiseInterface */ public function __invoke(RequestInterface $request, array $options) { $handler = $this->resolve(); return $handler($request, $options); } /** * Dumps a string representation of the stack. * * @return string */ public function __toString() { $depth = 0; $stack = []; if ($this->handler) { $stack[] = "0) Handler: " . $this->debugCallable($this->handler); } $result = ''; foreach (array_reverse($this->stack) as $tuple) { $depth++; $str = "{$depth}) Name: '{$tuple[1]}', "; $str .= "Function: " . $this->debugCallable($tuple[0]); $result = "> {$str}\n{$result}"; $stack[] = $str; } foreach (array_keys($stack) as $k) { $result .= "< {$stack[$k]}\n"; } return $result; } /** * Set the HTTP handler that actually returns a promise. * * @param callable $handler Accepts a request and array of options and * returns a Promise. */ public function setHandler(callable $handler) { $this->handler = $handler; $this->cached = null; } /** * Returns true if the builder has a handler. * * @return bool */ public function hasHandler() { return (bool) $this->handler; } /** * Unshift a middleware to the bottom of the stack. * * @param callable $middleware Middleware function * @param string $name Name to register for this middleware. */ public function unshift(callable $middleware, $name = null) { array_unshift($this->stack, [$middleware, $name]); $this->cached = null; } /** * Push a middleware to the top of the stack. * * @param callable $middleware Middleware function * @param string $name Name to register for this middleware. */ public function push(callable $middleware, $name = '') { $this->stack[] = [$middleware, $name]; $this->cached = null; } /** * Add a middleware before another middleware by name. * * @param string $findName Middleware to find * @param callable $middleware Middleware function * @param string $withName Name to register for this middleware. */ public function before($findName, callable $middleware, $withName = '') { $this->splice($findName, $withName, $middleware, true); } /** * Add a middleware after another middleware by name. * * @param string $findName Middleware to find * @param callable $middleware Middleware function * @param string $withName Name to register for this middleware. */ public function after($findName, callable $middleware, $withName = '') { $this->splice($findName, $withName, $middleware, false); } /** * Remove a middleware by instance or name from the stack. * * @param callable|string $remove Middleware to remove by instance or name. */ public function remove($remove) { $this->cached = null; $idx = is_callable($remove) ? 0 : 1; $this->stack = array_values(array_filter( $this->stack, function ($tuple) use ($idx, $remove) { return $tuple[$idx] !== $remove; } )); } /** * Compose the middleware and handler into a single callable function. * * @return callable */ public function resolve() { if (!$this->cached) { if (!($prev = $this->handler)) { throw new \LogicException('No handler has been specified'); } foreach (array_reverse($this->stack) as $fn) { $prev = $fn[0]($prev); } $this->cached = $prev; } return $this->cached; } /** * @param string $name * @return int */ private function findByName($name) { foreach ($this->stack as $k => $v) { if ($v[1] === $name) { return $k; } } throw new \InvalidArgumentException("Middleware not found: $name"); } /** * Splices a function into the middleware list at a specific position. * * @param string $findName * @param string $withName * @param callable $middleware * @param bool $before */ private function splice($findName, $withName, callable $middleware, $before) { $this->cached = null; $idx = $this->findByName($findName); $tuple = [$middleware, $withName]; if ($before) { if ($idx === 0) { array_unshift($this->stack, $tuple); } else { $replacement = [$tuple, $this->stack[$idx]]; array_splice($this->stack, $idx, 1, $replacement); } } elseif ($idx === count($this->stack) - 1) { $this->stack[] = $tuple; } else { $replacement = [$this->stack[$idx], $tuple]; array_splice($this->stack, $idx, 1, $replacement); } } /** * Provides a debug string for a given callable. * * @param array|callable $fn Function to write as a string. * * @return string */ private function debugCallable($fn) { if (is_string($fn)) { return "callable({$fn})"; } if (is_array($fn)) { return is_string($fn[0]) ? "callable({$fn[0]}::{$fn[1]})" : "callable(['" . get_class($fn[0]) . "', '{$fn[1]}'])"; } return 'callable(' . spl_object_hash($fn) . ')'; } } <?php namespace GuzzleHttp; use GuzzleHttp\Cookie\CookieJarInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Promise\RejectedPromise; use GuzzleHttp\Psr7; use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerInterface; /** * Functions used to create and wrap handlers with handler middleware. */ final class Middleware { /** * Middleware that adds cookies to requests. * * The options array must be set to a CookieJarInterface in order to use * cookies. This is typically handled for you by a client. * * @return callable Returns a function that accepts the next handler. */ public static function cookies() { return function (callable $handler) { return function ($request, array $options) use ($handler) { if (empty($options['cookies'])) { return $handler($request, $options); } elseif (!($options['cookies'] instanceof CookieJarInterface)) { throw new \InvalidArgumentException('cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface'); } $cookieJar = $options['cookies']; $request = $cookieJar->withCookieHeader($request); return $handler($request, $options) ->then( function ($response) use ($cookieJar, $request) { $cookieJar->extractCookies($request, $response); return $response; } ); }; }; } /** * Middleware that throws exceptions for 4xx or 5xx responses when the * "http_error" request option is set to true. * * @return callable Returns a function that accepts the next handler. */ public static function httpErrors() { return function (callable $handler) { return function ($request, array $options) use ($handler) { if (empty($options['http_errors'])) { return $handler($request, $options); } return $handler($request, $options)->then( function (ResponseInterface $response) use ($request) { $code = $response->getStatusCode(); if ($code < 400) { return $response; } throw RequestException::create($request, $response); } ); }; }; } /** * Middleware that pushes history data to an ArrayAccess container. * * @param array|\ArrayAccess $container Container to hold the history (by reference). * * @return callable Returns a function that accepts the next handler. * @throws \InvalidArgumentException if container is not an array or ArrayAccess. */ public static function history(&$container) { if (!is_array($container) && !$container instanceof \ArrayAccess) { throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess'); } return function (callable $handler) use (&$container) { return function ($request, array $options) use ($handler, &$container) { return $handler($request, $options)->then( function ($value) use ($request, &$container, $options) { $container[] = [ 'request' => $request, 'response' => $value, 'error' => null, 'options' => $options ]; return $value; }, function ($reason) use ($request, &$container, $options) { $container[] = [ 'request' => $request, 'response' => null, 'error' => $reason, 'options' => $options ]; return \GuzzleHttp\Promise\rejection_for($reason); } ); }; }; } /** * Middleware that invokes a callback before and after sending a request. * * The provided listener cannot modify or alter the response. It simply * "taps" into the chain to be notified before returning the promise. The * before listener accepts a request and options array, and the after * listener accepts a request, options array, and response promise. * * @param callable $before Function to invoke before forwarding the request. * @param callable $after Function invoked after forwarding. * * @return callable Returns a function that accepts the next handler. */ public static function tap(callable $before = null, callable $after = null) { return function (callable $handler) use ($before, $after) { return function ($request, array $options) use ($handler, $before, $after) { if ($before) { $before($request, $options); } $response = $handler($request, $options); if ($after) { $after($request, $options, $response); } return $response; }; }; } /** * Middleware that handles request redirects. * * @return callable Returns a function that accepts the next handler. */ public static function redirect() { return function (callable $handler) { return new RedirectMiddleware($handler); }; } /** * Middleware that retries requests based on the boolean result of * invoking the provided "decider" function. * * If no delay function is provided, a simple implementation of exponential * backoff will be utilized. * * @param callable $decider Function that accepts the number of retries, * a request, [response], and [exception] and * returns true if the request is to be retried. * @param callable $delay Function that accepts the number of retries and * returns the number of milliseconds to delay. * * @return callable Returns a function that accepts the next handler. */ public static function retry(callable $decider, callable $delay = null) { return function (callable $handler) use ($decider, $delay) { return new RetryMiddleware($decider, $handler, $delay); }; } /** * Middleware that logs requests, responses, and errors using a message * formatter. * * @param LoggerInterface $logger Logs messages. * @param MessageFormatter $formatter Formatter used to create message strings. * @param string $logLevel Level at which to log requests. * * @return callable Returns a function that accepts the next handler. */ public static function log(LoggerInterface $logger, MessageFormatter $formatter, $logLevel = 'info' /* \Psr\Log\LogLevel::INFO */) { return function (callable $handler) use ($logger, $formatter, $logLevel) { return function ($request, array $options) use ($handler, $logger, $formatter, $logLevel) { return $handler($request, $options)->then( function ($response) use ($logger, $request, $formatter, $logLevel) { $message = $formatter->format($request, $response); $logger->log($logLevel, $message); return $response; }, function ($reason) use ($logger, $request, $formatter) { $response = $reason instanceof RequestException ? $reason->getResponse() : null; $message = $formatter->format($request, $response, $reason); $logger->notice($message); return \GuzzleHttp\Promise\rejection_for($reason); } ); }; }; } /** * This middleware adds a default content-type if possible, a default * content-length or transfer-encoding header, and the expect header. * * @return callable */ public static function prepareBody() { return function (callable $handler) { return new PrepareBodyMiddleware($handler); }; } /** * Middleware that applies a map function to the request before passing to * the next handler. * * @param callable $fn Function that accepts a RequestInterface and returns * a RequestInterface. * @return callable */ public static function mapRequest(callable $fn) { return function (callable $handler) use ($fn) { return function ($request, array $options) use ($handler, $fn) { return $handler($fn($request), $options); }; }; } /** * Middleware that applies a map function to the resolved promise's * response. * * @param callable $fn Function that accepts a ResponseInterface and * returns a ResponseInterface. * @return callable */ public static function mapResponse(callable $fn) { return function (callable $handler) use ($fn) { return function ($request, array $options) use ($handler, $fn) { return $handler($request, $options)->then($fn); }; }; } } <?php namespace GuzzleHttp; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Promise\PromiseInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; /** * Client interface for sending HTTP requests. */ interface ClientInterface { /** * @deprecated Will be removed in Guzzle 7.0.0 */ const VERSION = '6.5.0'; /** * Send an HTTP request. * * @param RequestInterface $request Request to send * @param array $options Request options to apply to the given * request and to the transfer. * * @return ResponseInterface * @throws GuzzleException */ public function send(RequestInterface $request, array $options = []); /** * Asynchronously send an HTTP request. * * @param RequestInterface $request Request to send * @param array $options Request options to apply to the given * request and to the transfer. * * @return PromiseInterface */ public function sendAsync(RequestInterface $request, array $options = []); /** * Create and send an HTTP request. * * Use an absolute path to override the base path of the client, or a * relative path to append to the base path of the client. The URL can * contain the query string as well. * * @param string $method HTTP method. * @param string|UriInterface $uri URI object or string. * @param array $options Request options to apply. * * @return ResponseInterface * @throws GuzzleException */ public function request($method, $uri, array $options = []); /** * Create and send an asynchronous HTTP request. * * Use an absolute path to override the base path of the client, or a * relative path to append to the base path of the client. The URL can * contain the query string as well. Use an array to provide a URL * template and additional variables to use in the URL template expansion. * * @param string $method HTTP method * @param string|UriInterface $uri URI object or string. * @param array $options Request options to apply. * * @return PromiseInterface */ public function requestAsync($method, $uri, array $options = []); /** * Get a client configuration option. * * These options include default request options of the client, a "handler" * (if utilized by the concrete client), and a "base_uri" if utilized by * the concrete client. * * @param string|null $option The config option to retrieve. * * @return mixed */ public function getConfig($option = null); } <?php namespace GuzzleHttp; use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Exception\InvalidArgumentException; use GuzzleHttp\Promise; use GuzzleHttp\Psr7; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; /** * @method ResponseInterface get(string|UriInterface $uri, array $options = []) * @method ResponseInterface head(string|UriInterface $uri, array $options = []) * @method ResponseInterface put(string|UriInterface $uri, array $options = []) * @method ResponseInterface post(string|UriInterface $uri, array $options = []) * @method ResponseInterface patch(string|UriInterface $uri, array $options = []) * @method ResponseInterface delete(string|UriInterface $uri, array $options = []) * @method Promise\PromiseInterface getAsync(string|UriInterface $uri, array $options = []) * @method Promise\PromiseInterface headAsync(string|UriInterface $uri, array $options = []) * @method Promise\PromiseInterface putAsync(string|UriInterface $uri, array $options = []) * @method Promise\PromiseInterface postAsync(string|UriInterface $uri, array $options = []) * @method Promise\PromiseInterface patchAsync(string|UriInterface $uri, array $options = []) * @method Promise\PromiseInterface deleteAsync(string|UriInterface $uri, array $options = []) */ class Client implements ClientInterface { /** @var array Default request options */ private $config; /** * Clients accept an array of constructor parameters. * * Here's an example of creating a client using a base_uri and an array of * default request options to apply to each request: * * $client = new Client([ * 'base_uri' => 'http://www.foo.com/1.0/', * 'timeout' => 0, * 'allow_redirects' => false, * 'proxy' => '192.168.16.1:10' * ]); * * Client configuration settings include the following options: * * - handler: (callable) Function that transfers HTTP requests over the * wire. The function is called with a Psr7\Http\Message\RequestInterface * and array of transfer options, and must return a * GuzzleHttp\Promise\PromiseInterface that is fulfilled with a * Psr7\Http\Message\ResponseInterface on success. "handler" is a * constructor only option that cannot be overridden in per/request * options. If no handler is provided, a default handler will be created * that enables all of the request options below by attaching all of the * default middleware to the handler. * - base_uri: (string|UriInterface) Base URI of the client that is merged * into relative URIs. Can be a string or instance of UriInterface. * - **: any request option * * @param array $config Client configuration settings. * * @see \GuzzleHttp\RequestOptions for a list of available request options. */ public function __construct(array $config = []) { if (!isset($config['handler'])) { $config['handler'] = HandlerStack::create(); } elseif (!is_callable($config['handler'])) { throw new \InvalidArgumentException('handler must be a callable'); } // Convert the base_uri to a UriInterface if (isset($config['base_uri'])) { $config['base_uri'] = Psr7\uri_for($config['base_uri']); } $this->configureDefaults($config); } /** * @param string $method * @param array $args * * @return Promise\PromiseInterface */ public function __call($method, $args) { if (count($args) < 1) { throw new \InvalidArgumentException('Magic request methods require a URI and optional options array'); } $uri = $args[0]; $opts = isset($args[1]) ? $args[1] : []; return substr($method, -5) === 'Async' ? $this->requestAsync(substr($method, 0, -5), $uri, $opts) : $this->request($method, $uri, $opts); } /** * Asynchronously send an HTTP request. * * @param array $options Request options to apply to the given * request and to the transfer. See \GuzzleHttp\RequestOptions. * * @return PromiseInterface */ public function sendAsync(RequestInterface $request, array $options = []) { // Merge the base URI into the request URI if needed. $options = $this->prepareDefaults($options); return $this->transfer( $request->withUri($this->buildUri($request->getUri(), $options), $request->hasHeader('Host')), $options ); } /** * Send an HTTP request. * * @param array $options Request options to apply to the given * request and to the transfer. See \GuzzleHttp\RequestOptions. * * @return ResponseInterface * @throws GuzzleException */ public function send(RequestInterface $request, array $options = []) { $options[RequestOptions::SYNCHRONOUS] = true; return $this->sendAsync($request, $options)->wait(); } /** * Create and send an asynchronous HTTP request. * * Use an absolute path to override the base path of the client, or a * relative path to append to the base path of the client. The URL can * contain the query string as well. Use an array to provide a URL * template and additional variables to use in the URL template expansion. * * @param string $method HTTP method * @param string|UriInterface $uri URI object or string. * @param array $options Request options to apply. See \GuzzleHttp\RequestOptions. * * @return PromiseInterface */ public function requestAsync($method, $uri = '', array $options = []) { $options = $this->prepareDefaults($options); // Remove request modifying parameter because it can be done up-front. $headers = isset($options['headers']) ? $options['headers'] : []; $body = isset($options['body']) ? $options['body'] : null; $version = isset($options['version']) ? $options['version'] : '1.1'; // Merge the URI into the base URI. $uri = $this->buildUri($uri, $options); if (is_array($body)) { $this->invalidBody(); } $request = new Psr7\Request($method, $uri, $headers, $body, $version); // Remove the option so that they are not doubly-applied. unset($options['headers'], $options['body'], $options['version']); return $this->transfer($request, $options); } /** * Create and send an HTTP request. * * Use an absolute path to override the base path of the client, or a * relative path to append to the base path of the client. The URL can * contain the query string as well. * * @param string $method HTTP method. * @param string|UriInterface $uri URI object or string. * @param array $options Request options to apply. See \GuzzleHttp\RequestOptions. * * @return ResponseInterface * @throws GuzzleException */ public function request($method, $uri = '', array $options = []) { $options[RequestOptions::SYNCHRONOUS] = true; return $this->requestAsync($method, $uri, $options)->wait(); } /** * Get a client configuration option. * * These options include default request options of the client, a "handler" * (if utilized by the concrete client), and a "base_uri" if utilized by * the concrete client. * * @param string|null $option The config option to retrieve. * * @return mixed */ public function getConfig($option = null) { return $option === null ? $this->config : (isset($this->config[$option]) ? $this->config[$option] : null); } /** * @param string|null $uri * * @return UriInterface */ private function buildUri($uri, array $config) { // for BC we accept null which would otherwise fail in uri_for $uri = Psr7\uri_for($uri === null ? '' : $uri); if (isset($config['base_uri'])) { $uri = Psr7\UriResolver::resolve(Psr7\uri_for($config['base_uri']), $uri); } if ($uri->getHost() && isset($config['idn_conversion']) && ($config['idn_conversion'] !== false)) { $idnOptions = ($config['idn_conversion'] === true) ? IDNA_DEFAULT : $config['idn_conversion']; $asciiHost = idn_to_ascii($uri->getHost(), $idnOptions, INTL_IDNA_VARIANT_UTS46, $info); if ($asciiHost === false) { $errorBitSet = isset($info['errors']) ? $info['errors'] : 0; $errorConstants = array_filter(array_keys(get_defined_constants()), function ($name) { return substr($name, 0, 11) === 'IDNA_ERROR_'; }); $errors = []; foreach ($errorConstants as $errorConstant) { if ($errorBitSet & constant($errorConstant)) { $errors[] = $errorConstant; } } $errorMessage = 'IDN conversion failed'; if ($errors) { $errorMessage .= ' (errors: ' . implode(', ', $errors) . ')'; } throw new InvalidArgumentException($errorMessage); } else { if ($uri->getHost() !== $asciiHost) { // Replace URI only if the ASCII version is different $uri = $uri->withHost($asciiHost); } } } return $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri; } /** * Configures the default options for a client. * * @param array $config * @return void */ private function configureDefaults(array $config) { $defaults = [ 'allow_redirects' => RedirectMiddleware::$defaultSettings, 'http_errors' => true, 'decode_content' => true, 'verify' => true, 'cookies' => false ]; // idn_to_ascii() is a part of ext-intl and might be not available $defaults['idn_conversion'] = function_exists('idn_to_ascii'); // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set. // We can only trust the HTTP_PROXY environment variable in a CLI // process due to the fact that PHP has no reliable mechanism to // get environment variables that start with "HTTP_". if (php_sapi_name() === 'cli' && getenv('HTTP_PROXY')) { $defaults['proxy']['http'] = getenv('HTTP_PROXY'); } if ($proxy = getenv('HTTPS_PROXY')) { $defaults['proxy']['https'] = $proxy; } if ($noProxy = getenv('NO_PROXY')) { $cleanedNoProxy = str_replace(' ', '', $noProxy); $defaults['proxy']['no'] = explode(',', $cleanedNoProxy); } $this->config = $config + $defaults; if (!empty($config['cookies']) && $config['cookies'] === true) { $this->config['cookies'] = new CookieJar(); } // Add the default user-agent header. if (!isset($this->config['headers'])) { $this->config['headers'] = ['User-Agent' => default_user_agent()]; } else { // Add the User-Agent header if one was not already set. foreach (array_keys($this->config['headers']) as $name) { if (strtolower($name) === 'user-agent') { return; } } $this->config['headers']['User-Agent'] = default_user_agent(); } } /** * Merges default options into the array. * * @param array $options Options to modify by reference * * @return array */ private function prepareDefaults(array $options) { $defaults = $this->config; if (!empty($defaults['headers'])) { // Default headers are only added if they are not present. $defaults['_conditional'] = $defaults['headers']; unset($defaults['headers']); } // Special handling for headers is required as they are added as // conditional headers and as headers passed to a request ctor. if (array_key_exists('headers', $options)) { // Allows default headers to be unset. if ($options['headers'] === null) { $defaults['_conditional'] = []; unset($options['headers']); } elseif (!is_array($options['headers'])) { throw new \InvalidArgumentException('headers must be an array'); } } // Shallow merge defaults underneath options. $result = $options + $defaults; // Remove null values. foreach ($result as $k => $v) { if ($v === null) { unset($result[$k]); } } return $result; } /** * Transfers the given request and applies request options. * * The URI of the request is not modified and the request options are used * as-is without merging in default options. * * @param array $options See \GuzzleHttp\RequestOptions. * * @return Promise\PromiseInterface */ private function transfer(RequestInterface $request, array $options) { // save_to -> sink if (isset($options['save_to'])) { $options['sink'] = $options['save_to']; unset($options['save_to']); } // exceptions -> http_errors if (isset($options['exceptions'])) { $options['http_errors'] = $options['exceptions']; unset($options['exceptions']); } $request = $this->applyOptions($request, $options); /** @var HandlerStack $handler */ $handler = $options['handler']; try { return Promise\promise_for($handler($request, $options)); } catch (\Exception $e) { return Promise\rejection_for($e); } } /** * Applies the array of request options to a request. * * @param RequestInterface $request * @param array $options * * @return RequestInterface */ private function applyOptions(RequestInterface $request, array &$options) { $modify = [ 'set_headers' => [], ]; if (isset($options['headers'])) { $modify['set_headers'] = $options['headers']; unset($options['headers']); } if (isset($options['form_params'])) { if (isset($options['multipart'])) { throw new \InvalidArgumentException('You cannot use ' . 'form_params and multipart at the same time. Use the ' . 'form_params option if you want to send application/' . 'x-www-form-urlencoded requests, and the multipart ' . 'option to send multipart/form-data requests.'); } $options['body'] = http_build_query($options['form_params'], '', '&'); unset($options['form_params']); // Ensure that we don't have the header in different case and set the new value. $options['_conditional'] = Psr7\_caseless_remove(['Content-Type'], $options['_conditional']); $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded'; } if (isset($options['multipart'])) { $options['body'] = new Psr7\MultipartStream($options['multipart']); unset($options['multipart']); } if (isset($options['json'])) { $options['body'] = \GuzzleHttp\json_encode($options['json']); unset($options['json']); // Ensure that we don't have the header in different case and set the new value. $options['_conditional'] = Psr7\_caseless_remove(['Content-Type'], $options['_conditional']); $options['_conditional']['Content-Type'] = 'application/json'; } if (!empty($options['decode_content']) && $options['decode_content'] !== true ) { // Ensure that we don't have the header in different case and set the new value. $options['_conditional'] = Psr7\_caseless_remove(['Accept-Encoding'], $options['_conditional']); $modify['set_headers']['Accept-Encoding'] = $options['decode_content']; } if (isset($options['body'])) { if (is_array($options['body'])) { $this->invalidBody(); } $modify['body'] = Psr7\stream_for($options['body']); unset($options['body']); } if (!empty($options['auth']) && is_array($options['auth'])) { $value = $options['auth']; $type = isset($value[2]) ? strtolower($value[2]) : 'basic'; switch ($type) { case 'basic': // Ensure that we don't have the header in different case and set the new value. $modify['set_headers'] = Psr7\_caseless_remove(['Authorization'], $modify['set_headers']); $modify['set_headers']['Authorization'] = 'Basic ' . base64_encode("$value[0]:$value[1]"); break; case 'digest': // @todo: Do not rely on curl $options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST; $options['curl'][CURLOPT_USERPWD] = "$value[0]:$value[1]"; break; case 'ntlm': $options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_NTLM; $options['curl'][CURLOPT_USERPWD] = "$value[0]:$value[1]"; break; } } if (isset($options['query'])) { $value = $options['query']; if (is_array($value)) { $value = http_build_query($value, null, '&', PHP_QUERY_RFC3986); } if (!is_string($value)) { throw new \InvalidArgumentException('query must be a string or array'); } $modify['query'] = $value; unset($options['query']); } // Ensure that sink is not an invalid value. if (isset($options['sink'])) { // TODO: Add more sink validation? if (is_bool($options['sink'])) { throw new \InvalidArgumentException('sink must not be a boolean'); } } $request = Psr7\modify_request($request, $modify); if ($request->getBody() instanceof Psr7\MultipartStream) { // Use a multipart/form-data POST if a Content-Type is not set. // Ensure that we don't have the header in different case and set the new value. $options['_conditional'] = Psr7\_caseless_remove(['Content-Type'], $options['_conditional']); $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' . $request->getBody()->getBoundary(); } // Merge in conditional headers if they are not present. if (isset($options['_conditional'])) { // Build up the changes so it's in a single clone of the message. $modify = []; foreach ($options['_conditional'] as $k => $v) { if (!$request->hasHeader($k)) { $modify['set_headers'][$k] = $v; } } $request = Psr7\modify_request($request, $modify); // Don't pass this internal value along to middleware/handlers. unset($options['_conditional']); } return $request; } /** * Throw Exception with pre-set message. * @return void * @throws InvalidArgumentException Invalid body. */ private function invalidBody() { throw new \InvalidArgumentException('Passing in the "body" request ' . 'option as an array to send a POST request has been deprecated. ' . 'Please use the "form_params" request option to send a ' . 'application/x-www-form-urlencoded request, or the "multipart" ' . 'request option to send a multipart/form-data request.'); } } <?php namespace GuzzleHttp; use GuzzleHttp\Promise\EachPromise; use GuzzleHttp\Promise\PromisorInterface; use Psr\Http\Message\RequestInterface; /** * Sends an iterator of requests concurrently using a capped pool size. * * The pool will read from an iterator until it is cancelled or until the * iterator is consumed. When a request is yielded, the request is sent after * applying the "request_options" request options (if provided in the ctor). * * When a function is yielded by the iterator, the function is provided the * "request_options" array that should be merged on top of any existing * options, and the function MUST then return a wait-able promise. */ class Pool implements PromisorInterface { /** @var EachPromise */ private $each; /** * @param ClientInterface $client Client used to send the requests. * @param array|\Iterator $requests Requests or functions that return * requests to send concurrently. * @param array $config Associative array of options * - concurrency: (int) Maximum number of requests to send concurrently * - options: Array of request options to apply to each request. * - fulfilled: (callable) Function to invoke when a request completes. * - rejected: (callable) Function to invoke when a request is rejected. */ public function __construct( ClientInterface $client, $requests, array $config = [] ) { // Backwards compatibility. if (isset($config['pool_size'])) { $config['concurrency'] = $config['pool_size']; } elseif (!isset($config['concurrency'])) { $config['concurrency'] = 25; } if (isset($config['options'])) { $opts = $config['options']; unset($config['options']); } else { $opts = []; } $iterable = \GuzzleHttp\Promise\iter_for($requests); $requests = function () use ($iterable, $client, $opts) { foreach ($iterable as $key => $rfn) { if ($rfn instanceof RequestInterface) { yield $key => $client->sendAsync($rfn, $opts); } elseif (is_callable($rfn)) { yield $key => $rfn($opts); } else { throw new \InvalidArgumentException('Each value yielded by ' . 'the iterator must be a Psr7\Http\Message\RequestInterface ' . 'or a callable that returns a promise that fulfills ' . 'with a Psr7\Message\Http\ResponseInterface object.'); } } }; $this->each = new EachPromise($requests(), $config); } /** * Get promise * @return GuzzleHttp\Promise\Promise */ public function promise() { return $this->each->promise(); } /** * Sends multiple requests concurrently and returns an array of responses * and exceptions that uses the same ordering as the provided requests. * * IMPORTANT: This method keeps every request and response in memory, and * as such, is NOT recommended when sending a large number or an * indeterminate number of requests concurrently. * * @param ClientInterface $client Client used to send the requests * @param array|\Iterator $requests Requests to send concurrently. * @param array $options Passes through the options available in * {@see GuzzleHttp\Pool::__construct} * * @return array Returns an array containing the response or an exception * in the same order that the requests were sent. * @throws \InvalidArgumentException if the event format is incorrect. */ public static function batch( ClientInterface $client, $requests, array $options = [] ) { $res = []; self::cmpCallback($options, 'fulfilled', $res); self::cmpCallback($options, 'rejected', $res); $pool = new static($client, $requests, $options); $pool->promise()->wait(); ksort($res); return $res; } /** * Execute callback(s) * * @return void */ private static function cmpCallback(array &$options, $name, array &$results) { if (!isset($options[$name])) { $options[$name] = function ($v, $k) use (&$results) { $results[$k] = $v; }; } else { $currentFn = $options[$name]; $options[$name] = function ($v, $k) use (&$results, $currentFn) { $currentFn($v, $k); $results[$k] = $v; }; } } } <?php namespace GuzzleHttp; use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\Psr7; use Psr\Http\Message\RequestInterface; /** * Prepares requests that contain a body, adding the Content-Length, * Content-Type, and Expect headers. */ class PrepareBodyMiddleware { /** @var callable */ private $nextHandler; /** * @param callable $nextHandler Next handler to invoke. */ public function __construct(callable $nextHandler) { $this->nextHandler = $nextHandler; } /** * @param RequestInterface $request * @param array $options * * @return PromiseInterface */ public function __invoke(RequestInterface $request, array $options) { $fn = $this->nextHandler; // Don't do anything if the request has no body. if ($request->getBody()->getSize() === 0) { return $fn($request, $options); } $modify = []; // Add a default content-type if possible. if (!$request->hasHeader('Content-Type')) { if ($uri = $request->getBody()->getMetadata('uri')) { if ($type = Psr7\mimetype_from_filename($uri)) { $modify['set_headers']['Content-Type'] = $type; } } } // Add a default content-length or transfer-encoding header. if (!$request->hasHeader('Content-Length') && !$request->hasHeader('Transfer-Encoding') ) { $size = $request->getBody()->getSize(); if ($size !== null) { $modify['set_headers']['Content-Length'] = $size; } else { $modify['set_headers']['Transfer-Encoding'] = 'chunked'; } } // Add the expect header if needed. $this->addExpectHeader($request, $options, $modify); return $fn(Psr7\modify_request($request, $modify), $options); } /** * Add expect header * * @return void */ private function addExpectHeader( RequestInterface $request, array $options, array &$modify ) { // Determine if the Expect header should be used if ($request->hasHeader('Expect')) { return; } $expect = isset($options['expect']) ? $options['expect'] : null; // Return if disabled or if you're not using HTTP/1.1 or HTTP/2.0 if ($expect === false || $request->getProtocolVersion() < 1.1) { return; } // The expect header is unconditionally enabled if ($expect === true) { $modify['set_headers']['Expect'] = '100-Continue'; return; } // By default, send the expect header when the payload is > 1mb if ($expect === null) { $expect = 1048576; } // Always add if the body cannot be rewound, the size cannot be // determined, or the size is greater than the cutoff threshold $body = $request->getBody(); $size = $body->getSize(); if ($size === null || $size >= (int) $expect || !$body->isSeekable()) { $modify['set_headers']['Expect'] = '100-Continue'; } } } <?php namespace GuzzleHttp\Exception; use Psr\Http\Message\RequestInterface; /** * Exception thrown when a connection cannot be established. * * Note that no response is present for a ConnectException */ class ConnectException extends RequestException { public function __construct( $message, RequestInterface $request, \Exception $previous = null, array $handlerContext = [] ) { parent::__construct($message, $request, null, $previous, $handlerContext); } /** * @return null */ public function getResponse() { return null; } /** * @return bool */ public function hasResponse() { return false; } } <?php namespace GuzzleHttp\Exception; final class InvalidArgumentException extends \InvalidArgumentException implements GuzzleException { } <?php namespace GuzzleHttp\Exception; class TransferException extends \RuntimeException implements GuzzleException { } <?php namespace GuzzleHttp\Exception; /** * Exception when a client error is encountered (4xx codes) */ class ClientException extends BadResponseException { } <?php namespace GuzzleHttp\Exception; class TooManyRedirectsException extends RequestException { } <?php namespace GuzzleHttp\Exception; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** * Exception when an HTTP error occurs (4xx or 5xx error) */ class BadResponseException extends RequestException { public function __construct( $message, RequestInterface $request, ResponseInterface $response = null, \Exception $previous = null, array $handlerContext = [] ) { if (null === $response) { @trigger_error( 'Instantiating the ' . __CLASS__ . ' class without a Response is deprecated since version 6.3 and will be removed in 7.0.', E_USER_DEPRECATED ); } parent::__construct($message, $request, $response, $previous, $handlerContext); } } <?php namespace GuzzleHttp\Exception; /** * Exception when a server error is encountered (5xx codes) */ class ServerException extends BadResponseException { } <?php namespace GuzzleHttp\Exception; use Psr\Http\Message\StreamInterface; /** * Exception thrown when a seek fails on a stream. */ class SeekException extends \RuntimeException implements GuzzleException { private $stream; public function __construct(StreamInterface $stream, $pos = 0, $msg = '') { $this->stream = $stream; $msg = $msg ?: 'Could not seek the stream to position ' . $pos; parent::__construct($msg); } /** * @return StreamInterface */ public function getStream() { return $this->stream; } } <?php namespace GuzzleHttp\Exception; use GuzzleHttp\Promise\PromiseInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; /** * HTTP Request exception */ class RequestException extends TransferException { /** @var RequestInterface */ private $request; /** @var ResponseInterface|null */ private $response; /** @var array */ private $handlerContext; public function __construct( $message, RequestInterface $request, ResponseInterface $response = null, \Exception $previous = null, array $handlerContext = [] ) { // Set the code of the exception if the response is set and not future. $code = $response && !($response instanceof PromiseInterface) ? $response->getStatusCode() : 0; parent::__construct($message, $code, $previous); $this->request = $request; $this->response = $response; $this->handlerContext = $handlerContext; } /** * Wrap non-RequestExceptions with a RequestException * * @param RequestInterface $request * @param \Exception $e * * @return RequestException */ public static function wrapException(RequestInterface $request, \Exception $e) { return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); } /** * Factory method to create a new exception with a normalized error message * * @param RequestInterface $request Request * @param ResponseInterface $response Response received * @param \Exception $previous Previous exception * @param array $ctx Optional handler context. * * @return self */ public static function create( RequestInterface $request, ResponseInterface $response = null, \Exception $previous = null, array $ctx = [] ) { if (!$response) { return new self( 'Error completing request', $request, null, $previous, $ctx ); } $level = (int) floor($response->getStatusCode() / 100); if ($level === 4) { $label = 'Client error'; $className = ClientException::class; } elseif ($level === 5) { $label = 'Server error'; $className = ServerException::class; } else { $label = 'Unsuccessful request'; $className = __CLASS__; } $uri = $request->getUri(); $uri = static::obfuscateUri($uri); // Client Error: `GET /` resulted in a `404 Not Found` response: // <html> ... (truncated) $message = sprintf( '%s: `%s %s` resulted in a `%s %s` response', $label, $request->getMethod(), $uri, $response->getStatusCode(), $response->getReasonPhrase() ); $summary = static::getResponseBodySummary($response); if ($summary !== null) { $message .= ":\n{$summary}\n"; } return new $className($message, $request, $response, $previous, $ctx); } /** * Get a short summary of the response * * Will return `null` if the response is not printable. * * @param ResponseInterface $response * * @return string|null */ public static function getResponseBodySummary(ResponseInterface $response) { return \GuzzleHttp\Psr7\get_message_body_summary($response); } /** * Obfuscates URI if there is a username and a password present * * @param UriInterface $uri * * @return UriInterface */ private static function obfuscateUri(UriInterface $uri) { $userInfo = $uri->getUserInfo(); if (false !== ($pos = strpos($userInfo, ':'))) { return $uri->withUserInfo(substr($userInfo, 0, $pos), '***'); } return $uri; } /** * Get the request that caused the exception * * @return RequestInterface */ public function getRequest() { return $this->request; } /** * Get the associated response * * @return ResponseInterface|null */ public function getResponse() { return $this->response; } /** * Check if a response was received * * @return bool */ public function hasResponse() { return $this->response !== null; } /** * Get contextual information about the error from the underlying handler. * * The contents of this array will vary depending on which handler you are * using. It may also be just an empty array. Relying on this data will * couple you to a specific handler, but can give more debug information * when needed. * * @return array */ public function getHandlerContext() { return $this->handlerContext; } } <?php namespace GuzzleHttp\Exception; use Throwable; if (interface_exists(Throwable::class)) { interface GuzzleException extends Throwable { } } else { /** * @method string getMessage() * @method \Throwable|null getPrevious() * @method mixed getCode() * @method string getFile() * @method int getLine() * @method array getTrace() * @method string getTraceAsString() */ interface GuzzleException { } } <?php namespace GuzzleHttp; use Psr\Http\Message\MessageInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** * Formats log messages using variable substitutions for requests, responses, * and other transactional data. * * The following variable substitutions are supported: * * - {request}: Full HTTP request message * - {response}: Full HTTP response message * - {ts}: ISO 8601 date in GMT * - {date_iso_8601} ISO 8601 date in GMT * - {date_common_log} Apache common log date using the configured timezone. * - {host}: Host of the request * - {method}: Method of the request * - {uri}: URI of the request * - {version}: Protocol version * - {target}: Request target of the request (path + query + fragment) * - {hostname}: Hostname of the machine that sent the request * - {code}: Status code of the response (if available) * - {phrase}: Reason phrase of the response (if available) * - {error}: Any error messages (if available) * - {req_header_*}: Replace `*` with the lowercased name of a request header to add to the message * - {res_header_*}: Replace `*` with the lowercased name of a response header to add to the message * - {req_headers}: Request headers * - {res_headers}: Response headers * - {req_body}: Request body * - {res_body}: Response body */ class MessageFormatter { /** * Apache Common Log Format. * @link http://httpd.apache.org/docs/2.4/logs.html#common * @var string */ const CLF = "{hostname} {req_header_User-Agent} - [{date_common_log}] \"{method} {target} HTTP/{version}\" {code} {res_header_Content-Length}"; const DEBUG = ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}"; const SHORT = '[{ts}] "{method} {target} HTTP/{version}" {code}'; /** @var string Template used to format log messages */ private $template; /** * @param string $template Log message template */ public function __construct($template = self::CLF) { $this->template = $template ?: self::CLF; } /** * Returns a formatted message string. * * @param RequestInterface $request Request that was sent * @param ResponseInterface $response Response that was received * @param \Exception $error Exception that was received * * @return string */ public function format( RequestInterface $request, ResponseInterface $response = null, \Exception $error = null ) { $cache = []; return preg_replace_callback( '/{\s*([A-Za-z_\-\.0-9]+)\s*}/', function (array $matches) use ($request, $response, $error, &$cache) { if (isset($cache[$matches[1]])) { return $cache[$matches[1]]; } $result = ''; switch ($matches[1]) { case 'request': $result = Psr7\str($request); break; case 'response': $result = $response ? Psr7\str($response) : ''; break; case 'req_headers': $result = trim($request->getMethod() . ' ' . $request->getRequestTarget()) . ' HTTP/' . $request->getProtocolVersion() . "\r\n" . $this->headers($request); break; case 'res_headers': $result = $response ? sprintf( 'HTTP/%s %d %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase() ) . "\r\n" . $this->headers($response) : 'NULL'; break; case 'req_body': $result = $request->getBody(); break; case 'res_body': $result = $response ? $response->getBody() : 'NULL'; break; case 'ts': case 'date_iso_8601': $result = gmdate('c'); break; case 'date_common_log': $result = date('d/M/Y:H:i:s O'); break; case 'method': $result = $request->getMethod(); break; case 'version': $result = $request->getProtocolVersion(); break; case 'uri': case 'url': $result = $request->getUri(); break; case 'target': $result = $request->getRequestTarget(); break; case 'req_version': $result = $request->getProtocolVersion(); break; case 'res_version': $result = $response ? $response->getProtocolVersion() : 'NULL'; break; case 'host': $result = $request->getHeaderLine('Host'); break; case 'hostname': $result = gethostname(); break; case 'code': $result = $response ? $response->getStatusCode() : 'NULL'; break; case 'phrase': $result = $response ? $response->getReasonPhrase() : 'NULL'; break; case 'error': $result = $error ? $error->getMessage() : 'NULL'; break; default: // handle prefixed dynamic headers if (strpos($matches[1], 'req_header_') === 0) { $result = $request->getHeaderLine(substr($matches[1], 11)); } elseif (strpos($matches[1], 'res_header_') === 0) { $result = $response ? $response->getHeaderLine(substr($matches[1], 11)) : 'NULL'; } } $cache[$matches[1]] = $result; return $result; }, $this->template ); } /** * Get headers from message as string * * @return string */ private function headers(MessageInterface $message) { $result = ''; foreach ($message->getHeaders() as $name => $values) { $result .= $name . ': ' . implode(', ', $values) . "\r\n"; } return trim($result); } } <?php namespace GuzzleHttp; use GuzzleHttp\Exception\BadResponseException; use GuzzleHttp\Exception\TooManyRedirectsException; use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\Psr7; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; /** * Request redirect middleware. * * Apply this middleware like other middleware using * {@see GuzzleHttp\Middleware::redirect()}. */ class RedirectMiddleware { const HISTORY_HEADER = 'X-Guzzle-Redirect-History'; const STATUS_HISTORY_HEADER = 'X-Guzzle-Redirect-Status-History'; public static $defaultSettings = [ 'max' => 5, 'protocols' => ['http', 'https'], 'strict' => false, 'referer' => false, 'track_redirects' => false, ]; /** @var callable */ private $nextHandler; /** * @param callable $nextHandler Next handler to invoke. */ public function __construct(callable $nextHandler) { $this->nextHandler = $nextHandler; } /** * @param RequestInterface $request * @param array $options * * @return PromiseInterface */ public function __invoke(RequestInterface $request, array $options) { $fn = $this->nextHandler; if (empty($options['allow_redirects'])) { return $fn($request, $options); } if ($options['allow_redirects'] === true) { $options['allow_redirects'] = self::$defaultSettings; } elseif (!is_array($options['allow_redirects'])) { throw new \InvalidArgumentException('allow_redirects must be true, false, or array'); } else { // Merge the default settings with the provided settings $options['allow_redirects'] += self::$defaultSettings; } if (empty($options['allow_redirects']['max'])) { return $fn($request, $options); } return $fn($request, $options) ->then(function (ResponseInterface $response) use ($request, $options) { return $this->checkRedirect($request, $options, $response); }); } /** * @param RequestInterface $request * @param array $options * @param ResponseInterface $response * * @return ResponseInterface|PromiseInterface */ public function checkRedirect( RequestInterface $request, array $options, ResponseInterface $response ) { if (substr($response->getStatusCode(), 0, 1) != '3' || !$response->hasHeader('Location') ) { return $response; } $this->guardMax($request, $options); $nextRequest = $this->modifyRequest($request, $options, $response); if (isset($options['allow_redirects']['on_redirect'])) { call_user_func( $options['allow_redirects']['on_redirect'], $request, $response, $nextRequest->getUri() ); } /** @var PromiseInterface|ResponseInterface $promise */ $promise = $this($nextRequest, $options); // Add headers to be able to track history of redirects. if (!empty($options['allow_redirects']['track_redirects'])) { return $this->withTracking( $promise, (string) $nextRequest->getUri(), $response->getStatusCode() ); } return $promise; } /** * Enable tracking on promise. * * @return PromiseInterface */ private function withTracking(PromiseInterface $promise, $uri, $statusCode) { return $promise->then( function (ResponseInterface $response) use ($uri, $statusCode) { // Note that we are pushing to the front of the list as this // would be an earlier response than what is currently present // in the history header. $historyHeader = $response->getHeader(self::HISTORY_HEADER); $statusHeader = $response->getHeader(self::STATUS_HISTORY_HEADER); array_unshift($historyHeader, $uri); array_unshift($statusHeader, $statusCode); return $response->withHeader(self::HISTORY_HEADER, $historyHeader) ->withHeader(self::STATUS_HISTORY_HEADER, $statusHeader); } ); } /** * Check for too many redirects * * @return void * * @throws TooManyRedirectsException Too many redirects. */ private function guardMax(RequestInterface $request, array &$options) { $current = isset($options['__redirect_count']) ? $options['__redirect_count'] : 0; $options['__redirect_count'] = $current + 1; $max = $options['allow_redirects']['max']; if ($options['__redirect_count'] > $max) { throw new TooManyRedirectsException( "Will not follow more than {$max} redirects", $request ); } } /** * @param RequestInterface $request * @param array $options * @param ResponseInterface $response * * @return RequestInterface */ public function modifyRequest( RequestInterface $request, array $options, ResponseInterface $response ) { // Request modifications to apply. $modify = []; $protocols = $options['allow_redirects']['protocols']; // Use a GET request if this is an entity enclosing request and we are // not forcing RFC compliance, but rather emulating what all browsers // would do. $statusCode = $response->getStatusCode(); if ($statusCode == 303 || ($statusCode <= 302 && !$options['allow_redirects']['strict']) ) { $modify['method'] = 'GET'; $modify['body'] = ''; } $modify['uri'] = $this->redirectUri($request, $response, $protocols); Psr7\rewind_body($request); // Add the Referer header if it is told to do so and only // add the header if we are not redirecting from https to http. if ($options['allow_redirects']['referer'] && $modify['uri']->getScheme() === $request->getUri()->getScheme() ) { $uri = $request->getUri()->withUserInfo(''); $modify['set_headers']['Referer'] = (string) $uri; } else { $modify['remove_headers'][] = 'Referer'; } // Remove Authorization header if host is different. if ($request->getUri()->getHost() !== $modify['uri']->getHost()) { $modify['remove_headers'][] = 'Authorization'; } return Psr7\modify_request($request, $modify); } /** * Set the appropriate URL on the request based on the location header * * @param RequestInterface $request * @param ResponseInterface $response * @param array $protocols * * @return UriInterface */ private function redirectUri( RequestInterface $request, ResponseInterface $response, array $protocols ) { $location = Psr7\UriResolver::resolve( $request->getUri(), new Psr7\Uri($response->getHeaderLine('Location')) ); // Ensure that the redirect URI is allowed based on the protocols. if (!in_array($location->getScheme(), $protocols)) { throw new BadResponseException( sprintf( 'Redirect URI, %s, does not use one of the allowed redirect protocols: %s', $location, implode(', ', $protocols) ), $request, $response ); } return $location; } } # Change Log ## 6.5.0 - 2019-11-07 * Improvement: Added support for reset internal queue in MockHandler. [#2143](https://github.com/guzzle/guzzle/pull/2143) * Improvement: Added support to pass arbitrary options to `curl_multi_init`. [#2287](https://github.com/guzzle/guzzle/pull/2287) * Fix: Gracefully handle passing `null` to the `header` option. [#2132](https://github.com/guzzle/guzzle/pull/2132) * Fix: `RetryMiddleware` did not do exponential delay between retires due unit mismatch. [#2132](https://github.com/guzzle/guzzle/pull/2132) * Fix: Prevent undefined offset when using array for ssl_key options. [#2348](https://github.com/guzzle/guzzle/pull/2348) * Deprecated `ClientInterface::VERSION` ## 6.4.1 - 2019-10-23 * No `guzzle.phar` was created in 6.4.0 due expired API token. This release will fix that * Added `parent::__construct()` to `FileCookieJar` and `SessionCookieJar` ## 6.4.0 - 2019-10-23 * Improvement: Improved error messages when using curl < 7.21.2 [#2108](https://github.com/guzzle/guzzle/pull/2108) * Fix: Test if response is readable before returning a summary in `RequestException::getResponseBodySummary()` [#2081](https://github.com/guzzle/guzzle/pull/2081) * Fix: Add support for GUZZLE_CURL_SELECT_TIMEOUT environment variable [#2161](https://github.com/guzzle/guzzle/pull/2161) * Improvement: Added `GuzzleHttp\Exception\InvalidArgumentException` [#2163](https://github.com/guzzle/guzzle/pull/2163) * Improvement: Added `GuzzleHttp\_current_time()` to use `hrtime()` if that function exists. [#2242](https://github.com/guzzle/guzzle/pull/2242) * Improvement: Added curl's `appconnect_time` in `TransferStats` [#2284](https://github.com/guzzle/guzzle/pull/2284) * Improvement: Make GuzzleException extend Throwable wherever it's available [#2273](https://github.com/guzzle/guzzle/pull/2273) * Fix: Prevent concurrent writes to file when saving `CookieJar` [#2335](https://github.com/guzzle/guzzle/pull/2335) * Improvement: Update `MockHandler` so we can test transfer time [#2362](https://github.com/guzzle/guzzle/pull/2362) ## 6.3.3 - 2018-04-22 * Fix: Default headers when decode_content is specified ## 6.3.2 - 2018-03-26 * Fix: Release process ## 6.3.1 - 2018-03-26 * Bug fix: Parsing 0 epoch expiry times in cookies [#2014](https://github.com/guzzle/guzzle/pull/2014) * Improvement: Better ConnectException detection [#2012](https://github.com/guzzle/guzzle/pull/2012) * Bug fix: Malformed domain that contains a "/" [#1999](https://github.com/guzzle/guzzle/pull/1999) * Bug fix: Undefined offset when a cookie has no first key-value pair [#1998](https://github.com/guzzle/guzzle/pull/1998) * Improvement: Support PHPUnit 6 [#1953](https://github.com/guzzle/guzzle/pull/1953) * Bug fix: Support empty headers [#1915](https://github.com/guzzle/guzzle/pull/1915) * Bug fix: Ignore case during header modifications [#1916](https://github.com/guzzle/guzzle/pull/1916) + Minor code cleanups, documentation fixes and clarifications. ## 6.3.0 - 2017-06-22 * Feature: force IP resolution (ipv4 or ipv6) [#1608](https://github.com/guzzle/guzzle/pull/1608), [#1659](https://github.com/guzzle/guzzle/pull/1659) * Improvement: Don't include summary in exception message when body is empty [#1621](https://github.com/guzzle/guzzle/pull/1621) * Improvement: Handle `on_headers` option in MockHandler [#1580](https://github.com/guzzle/guzzle/pull/1580) * Improvement: Added SUSE Linux CA path [#1609](https://github.com/guzzle/guzzle/issues/1609) * Improvement: Use class reference for getting the name of the class instead of using hardcoded strings [#1641](https://github.com/guzzle/guzzle/pull/1641) * Feature: Added `read_timeout` option [#1611](https://github.com/guzzle/guzzle/pull/1611) * Bug fix: PHP 7.x fixes [#1685](https://github.com/guzzle/guzzle/pull/1685), [#1686](https://github.com/guzzle/guzzle/pull/1686), [#1811](https://github.com/guzzle/guzzle/pull/1811) * Deprecation: BadResponseException instantiation without a response [#1642](https://github.com/guzzle/guzzle/pull/1642) * Feature: Added NTLM auth [#1569](https://github.com/guzzle/guzzle/pull/1569) * Feature: Track redirect HTTP status codes [#1711](https://github.com/guzzle/guzzle/pull/1711) * Improvement: Check handler type during construction [#1745](https://github.com/guzzle/guzzle/pull/1745) * Improvement: Always include the Content-Length if there's a body [#1721](https://github.com/guzzle/guzzle/pull/1721) * Feature: Added convenience method to access a cookie by name [#1318](https://github.com/guzzle/guzzle/pull/1318) * Bug fix: Fill `CURLOPT_CAPATH` and `CURLOPT_CAINFO` properly [#1684](https://github.com/guzzle/guzzle/pull/1684) * Improvement: Use `\GuzzleHttp\Promise\rejection_for` function instead of object init [#1827](https://github.com/guzzle/guzzle/pull/1827) + Minor code cleanups, documentation fixes and clarifications. ## 6.2.3 - 2017-02-28 * Fix deprecations with guzzle/psr7 version 1.4 ## 6.2.2 - 2016-10-08 * Allow to pass nullable Response to delay callable * Only add scheme when host is present * Fix drain case where content-length is the literal string zero * Obfuscate in-URL credentials in exceptions ## 6.2.1 - 2016-07-18 * Address HTTP_PROXY security vulnerability, CVE-2016-5385: https://httpoxy.org/ * Fixing timeout bug with StreamHandler: https://github.com/guzzle/guzzle/pull/1488 * Only read up to `Content-Length` in PHP StreamHandler to avoid timeouts when a server does not honor `Connection: close`. * Ignore URI fragment when sending requests. ## 6.2.0 - 2016-03-21 * Feature: added `GuzzleHttp\json_encode` and `GuzzleHttp\json_decode`. https://github.com/guzzle/guzzle/pull/1389 * Bug fix: Fix sleep calculation when waiting for delayed requests. https://github.com/guzzle/guzzle/pull/1324 * Feature: More flexible history containers. https://github.com/guzzle/guzzle/pull/1373 * Bug fix: defer sink stream opening in StreamHandler. https://github.com/guzzle/guzzle/pull/1377 * Bug fix: do not attempt to escape cookie values. https://github.com/guzzle/guzzle/pull/1406 * Feature: report original content encoding and length on decoded responses. https://github.com/guzzle/guzzle/pull/1409 * Bug fix: rewind seekable request bodies before dispatching to cURL. https://github.com/guzzle/guzzle/pull/1422 * Bug fix: provide an empty string to `http_build_query` for HHVM workaround. https://github.com/guzzle/guzzle/pull/1367 ## 6.1.1 - 2015-11-22 * Bug fix: Proxy::wrapSync() now correctly proxies to the appropriate handler https://github.com/guzzle/guzzle/commit/911bcbc8b434adce64e223a6d1d14e9a8f63e4e4 * Feature: HandlerStack is now more generic. https://github.com/guzzle/guzzle/commit/f2102941331cda544745eedd97fc8fd46e1ee33e * Bug fix: setting verify to false in the StreamHandler now disables peer verification. https://github.com/guzzle/guzzle/issues/1256 * Feature: Middleware now uses an exception factory, including more error context. https://github.com/guzzle/guzzle/pull/1282 * Feature: better support for disabled functions. https://github.com/guzzle/guzzle/pull/1287 * Bug fix: fixed regression where MockHandler was not using `sink`. https://github.com/guzzle/guzzle/pull/1292 ## 6.1.0 - 2015-09-08 * Feature: Added the `on_stats` request option to provide access to transfer statistics for requests. https://github.com/guzzle/guzzle/pull/1202 * Feature: Added the ability to persist session cookies in CookieJars. https://github.com/guzzle/guzzle/pull/1195 * Feature: Some compatibility updates for Google APP Engine https://github.com/guzzle/guzzle/pull/1216 * Feature: Added support for NO_PROXY to prevent the use of a proxy based on a simple set of rules. https://github.com/guzzle/guzzle/pull/1197 * Feature: Cookies can now contain square brackets. https://github.com/guzzle/guzzle/pull/1237 * Bug fix: Now correctly parsing `=` inside of quotes in Cookies. https://github.com/guzzle/guzzle/pull/1232 * Bug fix: Cusotm cURL options now correctly override curl options of the same name. https://github.com/guzzle/guzzle/pull/1221 * Bug fix: Content-Type header is now added when using an explicitly provided multipart body. https://github.com/guzzle/guzzle/pull/1218 * Bug fix: Now ignoring Set-Cookie headers that have no name. * Bug fix: Reason phrase is no longer cast to an int in some cases in the cURL handler. https://github.com/guzzle/guzzle/pull/1187 * Bug fix: Remove the Authorization header when redirecting if the Host header changes. https://github.com/guzzle/guzzle/pull/1207 * Bug fix: Cookie path matching fixes https://github.com/guzzle/guzzle/issues/1129 * Bug fix: Fixing the cURL `body_as_string` setting https://github.com/guzzle/guzzle/pull/1201 * Bug fix: quotes are no longer stripped when parsing cookies. https://github.com/guzzle/guzzle/issues/1172 * Bug fix: `form_params` and `query` now always uses the `&` separator. https://github.com/guzzle/guzzle/pull/1163 * Bug fix: Adding a Content-Length to PHP stream wrapper requests if not set. https://github.com/guzzle/guzzle/pull/1189 ## 6.0.2 - 2015-07-04 * Fixed a memory leak in the curl handlers in which references to callbacks were not being removed by `curl_reset`. * Cookies are now extracted properly before redirects. * Cookies now allow more character ranges. * Decoded Content-Encoding responses are now modified to correctly reflect their state if the encoding was automatically removed by a handler. This means that the `Content-Encoding` header may be removed an the `Content-Length` modified to reflect the message size after removing the encoding. * Added a more explicit error message when trying to use `form_params` and `multipart` in the same request. * Several fixes for HHVM support. * Functions are now conditionally required using an additional level of indirection to help with global Composer installations. ## 6.0.1 - 2015-05-27 * Fixed a bug with serializing the `query` request option where the `&` separator was missing. * Added a better error message for when `body` is provided as an array. Please use `form_params` or `multipart` instead. * Various doc fixes. ## 6.0.0 - 2015-05-26 * See the UPGRADING.md document for more information. * Added `multipart` and `form_params` request options. * Added `synchronous` request option. * Added the `on_headers` request option. * Fixed `expect` handling. * No longer adding default middlewares in the client ctor. These need to be present on the provided handler in order to work. * Requests are no longer initiated when sending async requests with the CurlMultiHandler. This prevents unexpected recursion from requests completing while ticking the cURL loop. * Removed the semantics of setting `default` to `true`. This is no longer required now that the cURL loop is not ticked for async requests. * Added request and response logging middleware. * No longer allowing self signed certificates when using the StreamHandler. * Ensuring that `sink` is valid if saving to a file. * Request exceptions now include a "handler context" which provides handler specific contextual information. * Added `GuzzleHttp\RequestOptions` to allow request options to be applied using constants. * `$maxHandles` has been removed from CurlMultiHandler. * `MultipartPostBody` is now part of the `guzzlehttp/psr7` package. ## 5.3.0 - 2015-05-19 * Mock now supports `save_to` * Marked `AbstractRequestEvent::getTransaction()` as public. * Fixed a bug in which multiple headers using different casing would overwrite previous headers in the associative array. * Added `Utils::getDefaultHandler()` * Marked `GuzzleHttp\Client::getDefaultUserAgent` as deprecated. * URL scheme is now always lowercased. ## 6.0.0-beta.1 * Requires PHP >= 5.5 * Updated to use PSR-7 * Requires immutable messages, which basically means an event based system owned by a request instance is no longer possible. * Utilizing the [Guzzle PSR-7 package](https://github.com/guzzle/psr7). * Removed the dependency on `guzzlehttp/streams`. These stream abstractions are available in the `guzzlehttp/psr7` package under the `GuzzleHttp\Psr7` namespace. * Added middleware and handler system * Replaced the Guzzle event and subscriber system with a middleware system. * No longer depends on RingPHP, but rather places the HTTP handlers directly in Guzzle, operating on PSR-7 messages. * Retry logic is now encapsulated in `GuzzleHttp\Middleware::retry`, which means the `guzzlehttp/retry-subscriber` is now obsolete. * Mocking responses is now handled using `GuzzleHttp\Handler\MockHandler`. * Asynchronous responses * No longer supports the `future` request option to send an async request. Instead, use one of the `*Async` methods of a client (e.g., `requestAsync`, `getAsync`, etc.). * Utilizing `GuzzleHttp\Promise` instead of React's promise library to avoid recursion required by chaining and forwarding react promises. See https://github.com/guzzle/promises * Added `requestAsync` and `sendAsync` to send request asynchronously. * Added magic methods for `getAsync()`, `postAsync()`, etc. to send requests asynchronously. * Request options * POST and form updates * Added the `form_fields` and `form_files` request options. * Removed the `GuzzleHttp\Post` namespace. * The `body` request option no longer accepts an array for POST requests. * The `exceptions` request option has been deprecated in favor of the `http_errors` request options. * The `save_to` request option has been deprecated in favor of `sink` request option. * Clients no longer accept an array of URI template string and variables for URI variables. You will need to expand URI templates before passing them into a client constructor or request method. * Client methods `get()`, `post()`, `put()`, `patch()`, `options()`, etc. are now magic methods that will send synchronous requests. * Replaced `Utils.php` with plain functions in `functions.php`. * Removed `GuzzleHttp\Collection`. * Removed `GuzzleHttp\BatchResults`. Batched pool results are now returned as an array. * Removed `GuzzleHttp\Query`. Query string handling is now handled using an associative array passed into the `query` request option. The query string is serialized using PHP's `http_build_query`. If you need more control, you can pass the query string in as a string. * `GuzzleHttp\QueryParser` has been replaced with the `GuzzleHttp\Psr7\parse_query`. ## 5.2.0 - 2015-01-27 * Added `AppliesHeadersInterface` to make applying headers to a request based on the body more generic and not specific to `PostBodyInterface`. * Reduced the number of stack frames needed to send requests. * Nested futures are now resolved in the client rather than the RequestFsm * Finishing state transitions is now handled in the RequestFsm rather than the RingBridge. * Added a guard in the Pool class to not use recursion for request retries. ## 5.1.0 - 2014-12-19 * Pool class no longer uses recursion when a request is intercepted. * The size of a Pool can now be dynamically adjusted using a callback. See https://github.com/guzzle/guzzle/pull/943. * Setting a request option to `null` when creating a request with a client will ensure that the option is not set. This allows you to overwrite default request options on a per-request basis. See https://github.com/guzzle/guzzle/pull/937. * Added the ability to limit which protocols are allowed for redirects by specifying a `protocols` array in the `allow_redirects` request option. * Nested futures due to retries are now resolved when waiting for synchronous responses. See https://github.com/guzzle/guzzle/pull/947. * `"0"` is now an allowed URI path. See https://github.com/guzzle/guzzle/pull/935. * `Query` no longer typehints on the `$query` argument in the constructor, allowing for strings and arrays. * Exceptions thrown in the `end` event are now correctly wrapped with Guzzle specific exceptions if necessary. ## 5.0.3 - 2014-11-03 This change updates query strings so that they are treated as un-encoded values by default where the value represents an un-encoded value to send over the wire. A Query object then encodes the value before sending over the wire. This means that even value query string values (e.g., ":") are url encoded. This makes the Query class match PHP's http_build_query function. However, if you want to send requests over the wire using valid query string characters that do not need to be encoded, then you can provide a string to Url::setQuery() and pass true as the second argument to specify that the query string is a raw string that should not be parsed or encoded (unless a call to getQuery() is subsequently made, forcing the query-string to be converted into a Query object). ## 5.0.2 - 2014-10-30 * Added a trailing `\r\n` to multipart/form-data payloads. See https://github.com/guzzle/guzzle/pull/871 * Added a `GuzzleHttp\Pool::send()` convenience method to match the docs. * Status codes are now returned as integers. See https://github.com/guzzle/guzzle/issues/881 * No longer overwriting an existing `application/x-www-form-urlencoded` header when sending POST requests, allowing for customized headers. See https://github.com/guzzle/guzzle/issues/877 * Improved path URL serialization. * No longer double percent-encoding characters in the path or query string if they are already encoded. * Now properly encoding the supplied path to a URL object, instead of only encoding ' ' and '?'. * Note: This has been changed in 5.0.3 to now encode query string values by default unless the `rawString` argument is provided when setting the query string on a URL: Now allowing many more characters to be present in the query string without being percent encoded. See http://tools.ietf.org/html/rfc3986#appendix-A ## 5.0.1 - 2014-10-16 Bugfix release. * Fixed an issue where connection errors still returned response object in error and end events event though the response is unusable. This has been corrected so that a response is not returned in the `getResponse` method of these events if the response did not complete. https://github.com/guzzle/guzzle/issues/867 * Fixed an issue where transfer statistics were not being populated in the RingBridge. https://github.com/guzzle/guzzle/issues/866 ## 5.0.0 - 2014-10-12 Adding support for non-blocking responses and some minor API cleanup. ### New Features * Added support for non-blocking responses based on `guzzlehttp/guzzle-ring`. * Added a public API for creating a default HTTP adapter. * Updated the redirect plugin to be non-blocking so that redirects are sent concurrently. Other plugins like this can now be updated to be non-blocking. * Added a "progress" event so that you can get upload and download progress events. * Added `GuzzleHttp\Pool` which implements FutureInterface and transfers requests concurrently using a capped pool size as efficiently as possible. * Added `hasListeners()` to EmitterInterface. * Removed `GuzzleHttp\ClientInterface::sendAll` and marked `GuzzleHttp\Client::sendAll` as deprecated (it's still there, just not the recommended way). ### Breaking changes The breaking changes in this release are relatively minor. The biggest thing to look out for is that request and response objects no longer implement fluent interfaces. * Removed the fluent interfaces (i.e., `return $this`) from requests, responses, `GuzzleHttp\Collection`, `GuzzleHttp\Url`, `GuzzleHttp\Query`, `GuzzleHttp\Post\PostBody`, and `GuzzleHttp\Cookie\SetCookie`. This blog post provides a good outline of why I did this: http://ocramius.github.io/blog/fluent-interfaces-are-evil/. This also makes the Guzzle message interfaces compatible with the current PSR-7 message proposal. * Removed "functions.php", so that Guzzle is truly PSR-4 compliant. Except for the HTTP request functions from function.php, these functions are now implemented in `GuzzleHttp\Utils` using camelCase. `GuzzleHttp\json_decode` moved to `GuzzleHttp\Utils::jsonDecode`. `GuzzleHttp\get_path` moved to `GuzzleHttp\Utils::getPath`. `GuzzleHttp\set_path` moved to `GuzzleHttp\Utils::setPath`. `GuzzleHttp\batch` should now be `GuzzleHttp\Pool::batch`, which returns an `objectStorage`. Using functions.php caused problems for many users: they aren't PSR-4 compliant, require an explicit include, and needed an if-guard to ensure that the functions are not declared multiple times. * Rewrote adapter layer. * Removing all classes from `GuzzleHttp\Adapter`, these are now implemented as callables that are stored in `GuzzleHttp\Ring\Client`. * Removed the concept of "parallel adapters". Sending requests serially or concurrently is now handled using a single adapter. * Moved `GuzzleHttp\Adapter\Transaction` to `GuzzleHttp\Transaction`. The Transaction object now exposes the request, response, and client as public properties. The getters and setters have been removed. * Removed the "headers" event. This event was only useful for changing the body a response once the headers of the response were known. You can implement a similar behavior in a number of ways. One example might be to use a FnStream that has access to the transaction being sent. For example, when the first byte is written, you could check if the response headers match your expectations, and if so, change the actual stream body that is being written to. * Removed the `asArray` parameter from `GuzzleHttp\Message\MessageInterface::getHeader`. If you want to get a header value as an array, then use the newly added `getHeaderAsArray()` method of `MessageInterface`. This change makes the Guzzle interfaces compatible with the PSR-7 interfaces. * `GuzzleHttp\Message\MessageFactory` no longer allows subclasses to add custom request options using double-dispatch (this was an implementation detail). Instead, you should now provide an associative array to the constructor which is a mapping of the request option name mapping to a function that applies the option value to a request. * Removed the concept of "throwImmediately" from exceptions and error events. This control mechanism was used to stop a transfer of concurrent requests from completing. This can now be handled by throwing the exception or by cancelling a pool of requests or each outstanding future request individually. * Updated to "GuzzleHttp\Streams" 3.0. * `GuzzleHttp\Stream\StreamInterface::getContents()` no longer accepts a `maxLen` parameter. This update makes the Guzzle streams project compatible with the current PSR-7 proposal. * `GuzzleHttp\Stream\Stream::__construct`, `GuzzleHttp\Stream\Stream::factory`, and `GuzzleHttp\Stream\Utils::create` no longer accept a size in the second argument. They now accept an associative array of options, including the "size" key and "metadata" key which can be used to provide custom metadata. ## 4.2.2 - 2014-09-08 * Fixed a memory leak in the CurlAdapter when reusing cURL handles. * No longer using `request_fulluri` in stream adapter proxies. * Relative redirects are now based on the last response, not the first response. ## 4.2.1 - 2014-08-19 * Ensuring that the StreamAdapter does not always add a Content-Type header * Adding automated github releases with a phar and zip ## 4.2.0 - 2014-08-17 * Now merging in default options using a case-insensitive comparison. Closes https://github.com/guzzle/guzzle/issues/767 * Added the ability to automatically decode `Content-Encoding` response bodies using the `decode_content` request option. This is set to `true` by default to decode the response body if it comes over the wire with a `Content-Encoding`. Set this value to `false` to disable decoding the response content, and pass a string to provide a request `Accept-Encoding` header and turn on automatic response decoding. This feature now allows you to pass an `Accept-Encoding` header in the headers of a request but still disable automatic response decoding. Closes https://github.com/guzzle/guzzle/issues/764 * Added the ability to throw an exception immediately when transferring requests in parallel. Closes https://github.com/guzzle/guzzle/issues/760 * Updating guzzlehttp/streams dependency to ~2.1 * No longer utilizing the now deprecated namespaced methods from the stream package. ## 4.1.8 - 2014-08-14 * Fixed an issue in the CurlFactory that caused setting the `stream=false` request option to throw an exception. See: https://github.com/guzzle/guzzle/issues/769 * TransactionIterator now calls rewind on the inner iterator. See: https://github.com/guzzle/guzzle/pull/765 * You can now set the `Content-Type` header to `multipart/form-data` when creating POST requests to force multipart bodies. See https://github.com/guzzle/guzzle/issues/768 ## 4.1.7 - 2014-08-07 * Fixed an error in the HistoryPlugin that caused the same request and response to be logged multiple times when an HTTP protocol error occurs. * Ensuring that cURL does not add a default Content-Type when no Content-Type has been supplied by the user. This prevents the adapter layer from modifying the request that is sent over the wire after any listeners may have already put the request in a desired state (e.g., signed the request). * Throwing an exception when you attempt to send requests that have the "stream" set to true in parallel using the MultiAdapter. * Only calling curl_multi_select when there are active cURL handles. This was previously changed and caused performance problems on some systems due to PHP always selecting until the maximum select timeout. * Fixed a bug where multipart/form-data POST fields were not correctly aggregated (e.g., values with "&"). ## 4.1.6 - 2014-08-03 * Added helper methods to make it easier to represent messages as strings, including getting the start line and getting headers as a string. ## 4.1.5 - 2014-08-02 * Automatically retrying cURL "Connection died, retrying a fresh connect" errors when possible. * cURL implementation cleanup * Allowing multiple event subscriber listeners to be registered per event by passing an array of arrays of listener configuration. ## 4.1.4 - 2014-07-22 * Fixed a bug that caused multi-part POST requests with more than one field to serialize incorrectly. * Paths can now be set to "0" * `ResponseInterface::xml` now accepts a `libxml_options` option and added a missing default argument that was required when parsing XML response bodies. * A `save_to` stream is now created lazily, which means that files are not created on disk unless a request succeeds. ## 4.1.3 - 2014-07-15 * Various fixes to multipart/form-data POST uploads * Wrapping function.php in an if-statement to ensure Guzzle can be used globally and in a Composer install * Fixed an issue with generating and merging in events to an event array * POST headers are only applied before sending a request to allow you to change the query aggregator used before uploading * Added much more robust query string parsing * Fixed various parsing and normalization issues with URLs * Fixing an issue where multi-valued headers were not being utilized correctly in the StreamAdapter ## 4.1.2 - 2014-06-18 * Added support for sending payloads with GET requests ## 4.1.1 - 2014-06-08 * Fixed an issue related to using custom message factory options in subclasses * Fixed an issue with nested form fields in a multi-part POST * Fixed an issue with using the `json` request option for POST requests * Added `ToArrayInterface` to `GuzzleHttp\Cookie\CookieJar` ## 4.1.0 - 2014-05-27 * Added a `json` request option to easily serialize JSON payloads. * Added a `GuzzleHttp\json_decode()` wrapper to safely parse JSON. * Added `setPort()` and `getPort()` to `GuzzleHttp\Message\RequestInterface`. * Added the ability to provide an emitter to a client in the client constructor. * Added the ability to persist a cookie session using $_SESSION. * Added a trait that can be used to add event listeners to an iterator. * Removed request method constants from RequestInterface. * Fixed warning when invalid request start-lines are received. * Updated MessageFactory to work with custom request option methods. * Updated cacert bundle to latest build. 4.0.2 (2014-04-16) ------------------ * Proxy requests using the StreamAdapter now properly use request_fulluri (#632) * Added the ability to set scalars as POST fields (#628) ## 4.0.1 - 2014-04-04 * The HTTP status code of a response is now set as the exception code of RequestException objects. * 303 redirects will now correctly switch from POST to GET requests. * The default parallel adapter of a client now correctly uses the MultiAdapter. * HasDataTrait now initializes the internal data array as an empty array so that the toArray() method always returns an array. ## 4.0.0 - 2014-03-29 * For more information on the 4.0 transition, see: http://mtdowling.com/blog/2014/03/15/guzzle-4-rc/ * For information on changes and upgrading, see: https://github.com/guzzle/guzzle/blob/master/UPGRADING.md#3x-to-40 * Added `GuzzleHttp\batch()` as a convenience function for sending requests in parallel without needing to write asynchronous code. * Restructured how events are added to `GuzzleHttp\ClientInterface::sendAll()`. You can now pass a callable or an array of associative arrays where each associative array contains the "fn", "priority", and "once" keys. ## 4.0.0.rc-2 - 2014-03-25 * Removed `getConfig()` and `setConfig()` from clients to avoid confusion around whether things like base_url, message_factory, etc. should be able to be retrieved or modified. * Added `getDefaultOption()` and `setDefaultOption()` to ClientInterface * functions.php functions were renamed using snake_case to match PHP idioms * Added support for `HTTP_PROXY`, `HTTPS_PROXY`, and `GUZZLE_CURL_SELECT_TIMEOUT` environment variables * Added the ability to specify custom `sendAll()` event priorities * Added the ability to specify custom stream context options to the stream adapter. * Added a functions.php function for `get_path()` and `set_path()` * CurlAdapter and MultiAdapter now use a callable to generate curl resources * MockAdapter now properly reads a body and emits a `headers` event * Updated Url class to check if a scheme and host are set before adding ":" and "//". This allows empty Url (e.g., "") to be serialized as "". * Parsing invalid XML no longer emits warnings * Curl classes now properly throw AdapterExceptions * Various performance optimizations * Streams are created with the faster `Stream\create()` function * Marked deprecation_proxy() as internal * Test server is now a collection of static methods on a class ## 4.0.0-rc.1 - 2014-03-15 * See https://github.com/guzzle/guzzle/blob/master/UPGRADING.md#3x-to-40 ## 3.8.1 - 2014-01-28 * Bug: Always using GET requests when redirecting from a 303 response * Bug: CURLOPT_SSL_VERIFYHOST is now correctly set to false when setting `$certificateAuthority` to false in `Guzzle\Http\ClientInterface::setSslVerification()` * Bug: RedirectPlugin now uses strict RFC 3986 compliance when combining a base URL with a relative URL * Bug: The body of a request can now be set to `"0"` * Sending PHP stream requests no longer forces `HTTP/1.0` * Adding more information to ExceptionCollection exceptions so that users have more context, including a stack trace of each sub-exception * Updated the `$ref` attribute in service descriptions to merge over any existing parameters of a schema (rather than clobbering everything). * Merging URLs will now use the query string object from the relative URL (thus allowing custom query aggregators) * Query strings are now parsed in a way that they do no convert empty keys with no value to have a dangling `=`. For example `foo&bar=baz` is now correctly parsed and recognized as `foo&bar=baz` rather than `foo=&bar=baz`. * Now properly escaping the regular expression delimiter when matching Cookie domains. * Network access is now disabled when loading XML documents ## 3.8.0 - 2013-12-05 * Added the ability to define a POST name for a file * JSON response parsing now properly walks additionalProperties * cURL error code 18 is now retried automatically in the BackoffPlugin * Fixed a cURL error when URLs contain fragments * Fixed an issue in the BackoffPlugin retry event where it was trying to access all exceptions as if they were CurlExceptions * CURLOPT_PROGRESS function fix for PHP 5.5 (69fcc1e) * Added the ability for Guzzle to work with older versions of cURL that do not support `CURLOPT_TIMEOUT_MS` * Fixed a bug that was encountered when parsing empty header parameters * UriTemplate now has a `setRegex()` method to match the docs * The `debug` request parameter now checks if it is truthy rather than if it exists * Setting the `debug` request parameter to true shows verbose cURL output instead of using the LogPlugin * Added the ability to combine URLs using strict RFC 3986 compliance * Command objects can now return the validation errors encountered by the command * Various fixes to cache revalidation (#437 and 29797e5) * Various fixes to the AsyncPlugin * Cleaned up build scripts ## 3.7.4 - 2013-10-02 * Bug fix: 0 is now an allowed value in a description parameter that has a default value (#430) * Bug fix: SchemaFormatter now returns an integer when formatting to a Unix timestamp (see https://github.com/aws/aws-sdk-php/issues/147) * Bug fix: Cleaned up and fixed URL dot segment removal to properly resolve internal dots * Minimum PHP version is now properly specified as 5.3.3 (up from 5.3.2) (#420) * Updated the bundled cacert.pem (#419) * OauthPlugin now supports adding authentication to headers or query string (#425) ## 3.7.3 - 2013-09-08 * Added the ability to get the exception associated with a request/command when using `MultiTransferException` and `CommandTransferException`. * Setting `additionalParameters` of a response to false is now honored when parsing responses with a service description * Schemas are only injected into response models when explicitly configured. * No longer guessing Content-Type based on the path of a request. Content-Type is now only guessed based on the path of an EntityBody. * Bug fix: ChunkedIterator can now properly chunk a \Traversable as well as an \Iterator. * Bug fix: FilterIterator now relies on `\Iterator` instead of `\Traversable`. * Bug fix: Gracefully handling malformed responses in RequestMediator::writeResponseBody() * Bug fix: Replaced call to canCache with canCacheRequest in the CallbackCanCacheStrategy of the CachePlugin * Bug fix: Visiting XML attributes first before visiting XML children when serializing requests * Bug fix: Properly parsing headers that contain commas contained in quotes * Bug fix: mimetype guessing based on a filename is now case-insensitive ## 3.7.2 - 2013-08-02 * Bug fix: Properly URL encoding paths when using the PHP-only version of the UriTemplate expander See https://github.com/guzzle/guzzle/issues/371 * Bug fix: Cookie domains are now matched correctly according to RFC 6265 See https://github.com/guzzle/guzzle/issues/377 * Bug fix: GET parameters are now used when calculating an OAuth signature * Bug fix: Fixed an issue with cache revalidation where the If-None-Match header was being double quoted * `Guzzle\Common\AbstractHasDispatcher::dispatch()` now returns the event that was dispatched * `Guzzle\Http\QueryString::factory()` now guesses the most appropriate query aggregator to used based on the input. See https://github.com/guzzle/guzzle/issues/379 * Added a way to add custom domain objects to service description parsing using the `operation.parse_class` event. See https://github.com/guzzle/guzzle/pull/380 * cURL multi cleanup and optimizations ## 3.7.1 - 2013-07-05 * Bug fix: Setting default options on a client now works * Bug fix: Setting options on HEAD requests now works. See #352 * Bug fix: Moving stream factory before send event to before building the stream. See #353 * Bug fix: Cookies no longer match on IP addresses per RFC 6265 * Bug fix: Correctly parsing header parameters that are in `<>` and quotes * Added `cert` and `ssl_key` as request options * `Host` header can now diverge from the host part of a URL if the header is set manually * `Guzzle\Service\Command\LocationVisitor\Request\XmlVisitor` was rewritten to change from using SimpleXML to XMLWriter * OAuth parameters are only added via the plugin if they aren't already set * Exceptions are now thrown when a URL cannot be parsed * Returning `false` if `Guzzle\Http\EntityBody::getContentMd5()` fails * Not setting a `Content-MD5` on a command if calculating the Content-MD5 fails via the CommandContentMd5Plugin ## 3.7.0 - 2013-06-10 * See UPGRADING.md for more information on how to upgrade. * Requests now support the ability to specify an array of $options when creating a request to more easily modify a request. You can pass a 'request.options' configuration setting to a client to apply default request options to every request created by a client (e.g. default query string variables, headers, curl options, etc.). * Added a static facade class that allows you to use Guzzle with static methods and mount the class to `\Guzzle`. See `Guzzle\Http\StaticClient::mount`. * Added `command.request_options` to `Guzzle\Service\Command\AbstractCommand` to pass request options to requests created by a command (e.g. custom headers, query string variables, timeout settings, etc.). * Stream size in `Guzzle\Stream\PhpStreamRequestFactory` will now be set if Content-Length is returned in the headers of a response * Added `Guzzle\Common\Collection::setPath($path, $value)` to set a value into an array using a nested key (e.g. `$collection->setPath('foo/baz/bar', 'test'); echo $collection['foo']['bar']['bar'];`) * ServiceBuilders now support storing and retrieving arbitrary data * CachePlugin can now purge all resources for a given URI * CachePlugin can automatically purge matching cached items when a non-idempotent request is sent to a resource * CachePlugin now uses the Vary header to determine if a resource is a cache hit * `Guzzle\Http\Message\Response` now implements `\Serializable` * Added `Guzzle\Cache\CacheAdapterFactory::fromCache()` to more easily create cache adapters * `Guzzle\Service\ClientInterface::execute()` now accepts an array, single command, or Traversable * Fixed a bug in `Guzzle\Http\Message\Header\Link::addLink()` * Better handling of calculating the size of a stream in `Guzzle\Stream\Stream` using fstat() and caching the size * `Guzzle\Common\Exception\ExceptionCollection` now creates a more readable exception message * Fixing BC break: Added back the MonologLogAdapter implementation rather than extending from PsrLog so that older Symfony users can still use the old version of Monolog. * Fixing BC break: Added the implementation back in for `Guzzle\Http\Message\AbstractMessage::getTokenizedHeader()`. Now triggering an E_USER_DEPRECATED warning when used. Use `$message->getHeader()->parseParams()`. * Several performance improvements to `Guzzle\Common\Collection` * Added an `$options` argument to the end of the following methods of `Guzzle\Http\ClientInterface`: createRequest, head, delete, put, patch, post, options, prepareRequest * Added an `$options` argument to the end of `Guzzle\Http\Message\Request\RequestFactoryInterface::createRequest()` * Added an `applyOptions()` method to `Guzzle\Http\Message\Request\RequestFactoryInterface` * Changed `Guzzle\Http\ClientInterface::get($uri = null, $headers = null, $body = null)` to `Guzzle\Http\ClientInterface::get($uri = null, $headers = null, $options = array())`. You can still pass in a resource, string, or EntityBody into the $options parameter to specify the download location of the response. * Changed `Guzzle\Common\Collection::__construct($data)` to no longer accepts a null value for `$data` but a default `array()` * Added `Guzzle\Stream\StreamInterface::isRepeatable` * Removed `Guzzle\Http\ClientInterface::setDefaultHeaders(). Use $client->getConfig()->setPath('request.options/headers/{header_name}', 'value')`. or $client->getConfig()->setPath('request.options/headers', array('header_name' => 'value'))`. * Removed `Guzzle\Http\ClientInterface::getDefaultHeaders(). Use $client->getConfig()->getPath('request.options/headers')`. * Removed `Guzzle\Http\ClientInterface::expandTemplate()` * Removed `Guzzle\Http\ClientInterface::setRequestFactory()` * Removed `Guzzle\Http\ClientInterface::getCurlMulti()` * Removed `Guzzle\Http\Message\RequestInterface::canCache` * Removed `Guzzle\Http\Message\RequestInterface::setIsRedirect` * Removed `Guzzle\Http\Message\RequestInterface::isRedirect` * Made `Guzzle\Http\Client::expandTemplate` and `getUriTemplate` protected methods. * You can now enable E_USER_DEPRECATED warnings to see if you are using a deprecated method by setting `Guzzle\Common\Version::$emitWarnings` to true. * Marked `Guzzle\Http\Message\Request::isResponseBodyRepeatable()` as deprecated. Use `$request->getResponseBody()->isRepeatable()` instead. * Marked `Guzzle\Http\Message\Request::canCache()` as deprecated. Use `Guzzle\Plugin\Cache\DefaultCanCacheStrategy->canCacheRequest()` instead. * Marked `Guzzle\Http\Message\Request::canCache()` as deprecated. Use `Guzzle\Plugin\Cache\DefaultCanCacheStrategy->canCacheRequest()` instead. * Marked `Guzzle\Http\Message\Request::setIsRedirect()` as deprecated. Use the HistoryPlugin instead. * Marked `Guzzle\Http\Message\Request::isRedirect()` as deprecated. Use the HistoryPlugin instead. * Marked `Guzzle\Cache\CacheAdapterFactory::factory()` as deprecated * Marked 'command.headers', 'command.response_body' and 'command.on_complete' as deprecated for AbstractCommand. These will work through Guzzle 4.0 * Marked 'request.params' for `Guzzle\Http\Client` as deprecated. Use [request.options][params]. * Marked `Guzzle\Service\Client::enableMagicMethods()` as deprecated. Magic methods can no longer be disabled on a Guzzle\Service\Client. * Marked `Guzzle\Service\Client::getDefaultHeaders()` as deprecated. Use $client->getConfig()->getPath('request.options/headers')`. * Marked `Guzzle\Service\Client::setDefaultHeaders()` as deprecated. Use $client->getConfig()->setPath('request.options/headers/{header_name}', 'value')`. * Marked `Guzzle\Parser\Url\UrlParser` as deprecated. Just use PHP's `parse_url()` and percent encode your UTF-8. * Marked `Guzzle\Common\Collection::inject()` as deprecated. * Marked `Guzzle\Plugin\CurlAuth\CurlAuthPlugin` as deprecated. Use `$client->getConfig()->setPath('request.options/auth', array('user', 'pass', 'Basic|Digest');` * CacheKeyProviderInterface and DefaultCacheKeyProvider are no longer used. All of this logic is handled in a CacheStorageInterface. These two objects and interface will be removed in a future version. * Always setting X-cache headers on cached responses * Default cache TTLs are now handled by the CacheStorageInterface of a CachePlugin * `CacheStorageInterface::cache($key, Response $response, $ttl = null)` has changed to `cache(RequestInterface $request, Response $response);` * `CacheStorageInterface::fetch($key)` has changed to `fetch(RequestInterface $request);` * `CacheStorageInterface::delete($key)` has changed to `delete(RequestInterface $request);` * Added `CacheStorageInterface::purge($url)` * `DefaultRevalidation::__construct(CacheKeyProviderInterface $cacheKey, CacheStorageInterface $cache, CachePlugin $plugin)` has changed to `DefaultRevalidation::__construct(CacheStorageInterface $cache, CanCacheStrategyInterface $canCache = null)` * Added `RevalidationInterface::shouldRevalidate(RequestInterface $request, Response $response)` ## 3.6.0 - 2013-05-29 * ServiceDescription now implements ToArrayInterface * Added command.hidden_params to blacklist certain headers from being treated as additionalParameters * Guzzle can now correctly parse incomplete URLs * Mixed casing of headers are now forced to be a single consistent casing across all values for that header. * Messages internally use a HeaderCollection object to delegate handling case-insensitive header resolution * Removed the whole changedHeader() function system of messages because all header changes now go through addHeader(). * Specific header implementations can be created for complex headers. When a message creates a header, it uses a HeaderFactory which can map specific headers to specific header classes. There is now a Link header and CacheControl header implementation. * Removed from interface: Guzzle\Http\ClientInterface::setUriTemplate * Removed from interface: Guzzle\Http\ClientInterface::setCurlMulti() * Removed Guzzle\Http\Message\Request::receivedRequestHeader() and implemented this functionality in Guzzle\Http\Curl\RequestMediator * Removed the optional $asString parameter from MessageInterface::getHeader(). Just cast the header to a string. * Removed the optional $tryChunkedTransfer option from Guzzle\Http\Message\EntityEnclosingRequestInterface * Removed the $asObjects argument from Guzzle\Http\Message\MessageInterface::getHeaders() * Removed Guzzle\Parser\ParserRegister::get(). Use getParser() * Removed Guzzle\Parser\ParserRegister::set(). Use registerParser(). * All response header helper functions return a string rather than mixing Header objects and strings inconsistently * Removed cURL blacklist support. This is no longer necessary now that Expect, Accept, etc. are managed by Guzzle directly via interfaces * Removed the injecting of a request object onto a response object. The methods to get and set a request still exist but are a no-op until removed. * Most classes that used to require a `Guzzle\Service\Command\CommandInterface` typehint now request a `Guzzle\Service\Command\ArrayCommandInterface`. * Added `Guzzle\Http\Message\RequestInterface::startResponse()` to the RequestInterface to handle injecting a response on a request while the request is still being transferred * The ability to case-insensitively search for header values * Guzzle\Http\Message\Header::hasExactHeader * Guzzle\Http\Message\Header::raw. Use getAll() * Deprecated cache control specific methods on Guzzle\Http\Message\AbstractMessage. Use the CacheControl header object instead. * `Guzzle\Service\Command\CommandInterface` now extends from ToArrayInterface and ArrayAccess * Added the ability to cast Model objects to a string to view debug information. ## 3.5.0 - 2013-05-13 * Bug: Fixed a regression so that request responses are parsed only once per oncomplete event rather than multiple times * Bug: Better cleanup of one-time events across the board (when an event is meant to fire once, it will now remove itself from the EventDispatcher) * Bug: `Guzzle\Log\MessageFormatter` now properly writes "total_time" and "connect_time" values * Bug: Cloning an EntityEnclosingRequest now clones the EntityBody too * Bug: Fixed an undefined index error when parsing nested JSON responses with a sentAs parameter that reference a non-existent key * Bug: All __call() method arguments are now required (helps with mocking frameworks) * Deprecating Response::getRequest() and now using a shallow clone of a request object to remove a circular reference to help with refcount based garbage collection of resources created by sending a request * Deprecating ZF1 cache and log adapters. These will be removed in the next major version. * Deprecating `Response::getPreviousResponse()` (method signature still exists, but it's deprecated). Use the HistoryPlugin for a history. * Added a `responseBody` alias for the `response_body` location * Refactored internals to no longer rely on Response::getRequest() * HistoryPlugin can now be cast to a string * HistoryPlugin now logs transactions rather than requests and responses to more accurately keep track of the requests and responses that are sent over the wire * Added `getEffectiveUrl()` and `getRedirectCount()` to Response objects ## 3.4.3 - 2013-04-30 * Bug fix: Fixing bug introduced in 3.4.2 where redirect responses are duplicated on the final redirected response * Added a check to re-extract the temp cacert bundle from the phar before sending each request ## 3.4.2 - 2013-04-29 * Bug fix: Stream objects now work correctly with "a" and "a+" modes * Bug fix: Removing `Transfer-Encoding: chunked` header when a Content-Length is present * Bug fix: AsyncPlugin no longer forces HEAD requests * Bug fix: DateTime timezones are now properly handled when using the service description schema formatter * Bug fix: CachePlugin now properly handles stale-if-error directives when a request to the origin server fails * Setting a response on a request will write to the custom request body from the response body if one is specified * LogPlugin now writes to php://output when STDERR is undefined * Added the ability to set multiple POST files for the same key in a single call * application/x-www-form-urlencoded POSTs now use the utf-8 charset by default * Added the ability to queue CurlExceptions to the MockPlugin * Cleaned up how manual responses are queued on requests (removed "queued_response" and now using request.before_send) * Configuration loading now allows remote files ## 3.4.1 - 2013-04-16 * Large refactoring to how CurlMulti handles work. There is now a proxy that sits in front of a pool of CurlMulti handles. This greatly simplifies the implementation, fixes a couple bugs, and provides a small performance boost. * Exceptions are now properly grouped when sending requests in parallel * Redirects are now properly aggregated when a multi transaction fails * Redirects now set the response on the original object even in the event of a failure * Bug fix: Model names are now properly set even when using $refs * Added support for PHP 5.5's CurlFile to prevent warnings with the deprecated @ syntax * Added support for oauth_callback in OAuth signatures * Added support for oauth_verifier in OAuth signatures * Added support to attempt to retrieve a command first literally, then ucfirst, the with inflection ## 3.4.0 - 2013-04-11 * Bug fix: URLs are now resolved correctly based on http://tools.ietf.org/html/rfc3986#section-5.2. #289 * Bug fix: Absolute URLs with a path in a service description will now properly override the base URL. #289 * Bug fix: Parsing a query string with a single PHP array value will now result in an array. #263 * Bug fix: Better normalization of the User-Agent header to prevent duplicate headers. #264. * Bug fix: Added `number` type to service descriptions. * Bug fix: empty parameters are removed from an OAuth signature * Bug fix: Revalidating a cache entry prefers the Last-Modified over the Date header * Bug fix: Fixed "array to string" error when validating a union of types in a service description * Bug fix: Removed code that attempted to determine the size of a stream when data is written to the stream * Bug fix: Not including an `oauth_token` if the value is null in the OauthPlugin. * Bug fix: Now correctly aggregating successful requests and failed requests in CurlMulti when a redirect occurs. * The new default CURLOPT_TIMEOUT setting has been increased to 150 seconds so that Guzzle works on poor connections. * Added a feature to EntityEnclosingRequest::setBody() that will automatically set the Content-Type of the request if the Content-Type can be determined based on the entity body or the path of the request. * Added the ability to overwrite configuration settings in a client when grabbing a throwaway client from a builder. * Added support for a PSR-3 LogAdapter. * Added a `command.after_prepare` event * Added `oauth_callback` parameter to the OauthPlugin * Added the ability to create a custom stream class when using a stream factory * Added a CachingEntityBody decorator * Added support for `additionalParameters` in service descriptions to define how custom parameters are serialized. * The bundled SSL certificate is now provided in the phar file and extracted when running Guzzle from a phar. * You can now send any EntityEnclosingRequest with POST fields or POST files and cURL will handle creating bodies * POST requests using a custom entity body are now treated exactly like PUT requests but with a custom cURL method. This means that the redirect behavior of POST requests with custom bodies will not be the same as POST requests that use POST fields or files (the latter is only used when emulating a form POST in the browser). * Lots of cleanup to CurlHandle::factory and RequestFactory::createRequest ## 3.3.1 - 2013-03-10 * Added the ability to create PHP streaming responses from HTTP requests * Bug fix: Running any filters when parsing response headers with service descriptions * Bug fix: OauthPlugin fixes to allow for multi-dimensional array signing, and sorting parameters before signing * Bug fix: Removed the adding of default empty arrays and false Booleans to responses in order to be consistent across response location visitors. * Bug fix: Removed the possibility of creating configuration files with circular dependencies * RequestFactory::create() now uses the key of a POST file when setting the POST file name * Added xmlAllowEmpty to serialize an XML body even if no XML specific parameters are set ## 3.3.0 - 2013-03-03 * A large number of performance optimizations have been made * Bug fix: Added 'wb' as a valid write mode for streams * Bug fix: `Guzzle\Http\Message\Response::json()` now allows scalar values to be returned * Bug fix: Fixed bug in `Guzzle\Http\Message\Response` where wrapping quotes were stripped from `getEtag()` * BC: Removed `Guzzle\Http\Utils` class * BC: Setting a service description on a client will no longer modify the client's command factories. * BC: Emitting IO events from a RequestMediator is now a parameter that must be set in a request's curl options using the 'emit_io' key. This was previously set under a request's parameters using 'curl.emit_io' * BC: `Guzzle\Stream\Stream::getWrapper()` and `Guzzle\Stream\Stream::getSteamType()` are no longer converted to lowercase * Operation parameter objects are now lazy loaded internally * Added ErrorResponsePlugin that can throw errors for responses defined in service description operations' errorResponses * Added support for instantiating responseType=class responseClass classes. Classes must implement `Guzzle\Service\Command\ResponseClassInterface` * Added support for additionalProperties for top-level parameters in responseType=model responseClasses. These additional properties also support locations and can be used to parse JSON responses where the outermost part of the JSON is an array * Added support for nested renaming of JSON models (rename sentAs to name) * CachePlugin * Added support for stale-if-error so that the CachePlugin can now serve stale content from the cache on error * Debug headers can now added to cached response in the CachePlugin ## 3.2.0 - 2013-02-14 * CurlMulti is no longer reused globally. A new multi object is created per-client. This helps to isolate clients. * URLs with no path no longer contain a "/" by default * Guzzle\Http\QueryString does no longer manages the leading "?". This is now handled in Guzzle\Http\Url. * BadResponseException no longer includes the full request and response message * Adding setData() to Guzzle\Service\Description\ServiceDescriptionInterface * Adding getResponseBody() to Guzzle\Http\Message\RequestInterface * Various updates to classes to use ServiceDescriptionInterface type hints rather than ServiceDescription * Header values can now be normalized into distinct values when multiple headers are combined with a comma separated list * xmlEncoding can now be customized for the XML declaration of a XML service description operation * Guzzle\Http\QueryString now uses Guzzle\Http\QueryAggregator\QueryAggregatorInterface objects to add custom value aggregation and no longer uses callbacks * The URL encoding implementation of Guzzle\Http\QueryString can now be customized * Bug fix: Filters were not always invoked for array service description parameters * Bug fix: Redirects now use a target response body rather than a temporary response body * Bug fix: The default exponential backoff BackoffPlugin was not giving when the request threshold was exceeded * Bug fix: Guzzle now takes the first found value when grabbing Cache-Control directives ## 3.1.2 - 2013-01-27 * Refactored how operation responses are parsed. Visitors now include a before() method responsible for parsing the response body. For example, the XmlVisitor now parses the XML response into an array in the before() method. * Fixed an issue where cURL would not automatically decompress responses when the Accept-Encoding header was sent * CURLOPT_SSL_VERIFYHOST is never set to 1 because it is deprecated (see 5e0ff2ef20f839e19d1eeb298f90ba3598784444) * Fixed a bug where redirect responses were not chained correctly using getPreviousResponse() * Setting default headers on a client after setting the user-agent will not erase the user-agent setting ## 3.1.1 - 2013-01-20 * Adding wildcard support to Guzzle\Common\Collection::getPath() * Adding alias support to ServiceBuilder configs * Adding Guzzle\Service\Resource\CompositeResourceIteratorFactory and cleaning up factory interface ## 3.1.0 - 2013-01-12 * BC: CurlException now extends from RequestException rather than BadResponseException * BC: Renamed Guzzle\Plugin\Cache\CanCacheStrategyInterface::canCache() to canCacheRequest() and added CanCacheResponse() * Added getData to ServiceDescriptionInterface * Added context array to RequestInterface::setState() * Bug: Removing hard dependency on the BackoffPlugin from Guzzle\Http * Bug: Adding required content-type when JSON request visitor adds JSON to a command * Bug: Fixing the serialization of a service description with custom data * Made it easier to deal with exceptions thrown when transferring commands or requests in parallel by providing an array of successful and failed responses * Moved getPath from Guzzle\Service\Resource\Model to Guzzle\Common\Collection * Added Guzzle\Http\IoEmittingEntityBody * Moved command filtration from validators to location visitors * Added `extends` attributes to service description parameters * Added getModels to ServiceDescriptionInterface ## 3.0.7 - 2012-12-19 * Fixing phar detection when forcing a cacert to system if null or true * Allowing filename to be passed to `Guzzle\Http\Message\Request::setResponseBody()` * Cleaning up `Guzzle\Common\Collection::inject` method * Adding a response_body location to service descriptions ## 3.0.6 - 2012-12-09 * CurlMulti performance improvements * Adding setErrorResponses() to Operation * composer.json tweaks ## 3.0.5 - 2012-11-18 * Bug: Fixing an infinite recursion bug caused from revalidating with the CachePlugin * Bug: Response body can now be a string containing "0" * Bug: Using Guzzle inside of a phar uses system by default but now allows for a custom cacert * Bug: QueryString::fromString now properly parses query string parameters that contain equal signs * Added support for XML attributes in service description responses * DefaultRequestSerializer now supports array URI parameter values for URI template expansion * Added better mimetype guessing to requests and post files ## 3.0.4 - 2012-11-11 * Bug: Fixed a bug when adding multiple cookies to a request to use the correct glue value * Bug: Cookies can now be added that have a name, domain, or value set to "0" * Bug: Using the system cacert bundle when using the Phar * Added json and xml methods to Response to make it easier to parse JSON and XML response data into data structures * Enhanced cookie jar de-duplication * Added the ability to enable strict cookie jars that throw exceptions when invalid cookies are added * Added setStream to StreamInterface to actually make it possible to implement custom rewind behavior for entity bodies * Added the ability to create any sort of hash for a stream rather than just an MD5 hash ## 3.0.3 - 2012-11-04 * Implementing redirects in PHP rather than cURL * Added PECL URI template extension and using as default parser if available * Bug: Fixed Content-Length parsing of Response factory * Adding rewind() method to entity bodies and streams. Allows for custom rewinding of non-repeatable streams. * Adding ToArrayInterface throughout library * Fixing OauthPlugin to create unique nonce values per request ## 3.0.2 - 2012-10-25 * Magic methods are enabled by default on clients * Magic methods return the result of a command * Service clients no longer require a base_url option in the factory * Bug: Fixed an issue with URI templates where null template variables were being expanded ## 3.0.1 - 2012-10-22 * Models can now be used like regular collection objects by calling filter, map, etc. * Models no longer require a Parameter structure or initial data in the constructor * Added a custom AppendIterator to get around a PHP bug with the `\AppendIterator` ## 3.0.0 - 2012-10-15 * Rewrote service description format to be based on Swagger * Now based on JSON schema * Added nested input structures and nested response models * Support for JSON and XML input and output models * Renamed `commands` to `operations` * Removed dot class notation * Removed custom types * Broke the project into smaller top-level namespaces to be more component friendly * Removed support for XML configs and descriptions. Use arrays or JSON files. * Removed the Validation component and Inspector * Moved all cookie code to Guzzle\Plugin\Cookie * Magic methods on a Guzzle\Service\Client now return the command un-executed. * Calling getResult() or getResponse() on a command will lazily execute the command if needed. * Now shipping with cURL's CA certs and using it by default * Added previousResponse() method to response objects * No longer sending Accept and Accept-Encoding headers on every request * Only sending an Expect header by default when a payload is greater than 1MB * Added/moved client options: * curl.blacklist to curl.option.blacklist * Added ssl.certificate_authority * Added a Guzzle\Iterator component * Moved plugins from Guzzle\Http\Plugin to Guzzle\Plugin * Added a more robust backoff retry strategy (replaced the ExponentialBackoffPlugin) * Added a more robust caching plugin * Added setBody to response objects * Updating LogPlugin to use a more flexible MessageFormatter * Added a completely revamped build process * Cleaning up Collection class and removing default values from the get method * Fixed ZF2 cache adapters ## 2.8.8 - 2012-10-15 * Bug: Fixed a cookie issue that caused dot prefixed domains to not match where popular browsers did ## 2.8.7 - 2012-09-30 * Bug: Fixed config file aliases for JSON includes * Bug: Fixed cookie bug on a request object by using CookieParser to parse cookies on requests * Bug: Removing the path to a file when sending a Content-Disposition header on a POST upload * Bug: Hardening request and response parsing to account for missing parts * Bug: Fixed PEAR packaging * Bug: Fixed Request::getInfo * Bug: Fixed cases where CURLM_CALL_MULTI_PERFORM return codes were causing curl transactions to fail * Adding the ability for the namespace Iterator factory to look in multiple directories * Added more getters/setters/removers from service descriptions * Added the ability to remove POST fields from OAuth signatures * OAuth plugin now supports 2-legged OAuth ## 2.8.6 - 2012-09-05 * Added the ability to modify and build service descriptions * Added the use of visitors to apply parameters to locations in service descriptions using the dynamic command * Added a `json` parameter location * Now allowing dot notation for classes in the CacheAdapterFactory * Using the union of two arrays rather than an array_merge when extending service builder services and service params * Ensuring that a service is a string before doing strpos() checks on it when substituting services for references in service builder config files. * Services defined in two different config files that include one another will by default replace the previously defined service, but you can now create services that extend themselves and merge their settings over the previous * The JsonLoader now supports aliasing filenames with different filenames. This allows you to alias something like '_default' with a default JSON configuration file. ## 2.8.5 - 2012-08-29 * Bug: Suppressed empty arrays from URI templates * Bug: Added the missing $options argument from ServiceDescription::factory to enable caching * Added support for HTTP responses that do not contain a reason phrase in the start-line * AbstractCommand commands are now invokable * Added a way to get the data used when signing an Oauth request before a request is sent ## 2.8.4 - 2012-08-15 * Bug: Custom delay time calculations are no longer ignored in the ExponentialBackoffPlugin * Added the ability to transfer entity bodies as a string rather than streamed. This gets around curl error 65. Set `body_as_string` in a request's curl options to enable. * Added a StreamInterface, EntityBodyInterface, and added ftell() to Guzzle\Common\Stream * Added an AbstractEntityBodyDecorator and a ReadLimitEntityBody decorator to transfer only a subset of a decorated stream * Stream and EntityBody objects will now return the file position to the previous position after a read required operation (e.g. getContentMd5()) * Added additional response status codes * Removed SSL information from the default User-Agent header * DELETE requests can now send an entity body * Added an EventDispatcher to the ExponentialBackoffPlugin and added an ExponentialBackoffLogger to log backoff retries * Added the ability of the MockPlugin to consume mocked request bodies * LogPlugin now exposes request and response objects in the extras array ## 2.8.3 - 2012-07-30 * Bug: Fixed a case where empty POST requests were sent as GET requests * Bug: Fixed a bug in ExponentialBackoffPlugin that caused fatal errors when retrying an EntityEnclosingRequest that does not have a body * Bug: Setting the response body of a request to null after completing a request, not when setting the state of a request to new * Added multiple inheritance to service description commands * Added an ApiCommandInterface and added `getParamNames()` and `hasParam()` * Removed the default 2mb size cutoff from the Md5ValidatorPlugin so that it now defaults to validating everything * Changed CurlMulti::perform to pass a smaller timeout to CurlMulti::executeHandles ## 2.8.2 - 2012-07-24 * Bug: Query string values set to 0 are no longer dropped from the query string * Bug: A Collection object is no longer created each time a call is made to `Guzzle\Service\Command\AbstractCommand::getRequestHeaders()` * Bug: `+` is now treated as an encoded space when parsing query strings * QueryString and Collection performance improvements * Allowing dot notation for class paths in filters attribute of a service descriptions ## 2.8.1 - 2012-07-16 * Loosening Event Dispatcher dependency * POST redirects can now be customized using CURLOPT_POSTREDIR ## 2.8.0 - 2012-07-15 * BC: Guzzle\Http\Query * Query strings with empty variables will always show an equal sign unless the variable is set to QueryString::BLANK (e.g. ?acl= vs ?acl) * Changed isEncodingValues() and isEncodingFields() to isUrlEncoding() * Changed setEncodeValues(bool) and setEncodeFields(bool) to useUrlEncoding(bool) * Changed the aggregation functions of QueryString to be static methods * Can now use fromString() with querystrings that have a leading ? * cURL configuration values can be specified in service descriptions using `curl.` prefixed parameters * Content-Length is set to 0 before emitting the request.before_send event when sending an empty request body * Cookies are no longer URL decoded by default * Bug: URI template variables set to null are no longer expanded ## 2.7.2 - 2012-07-02 * BC: Moving things to get ready for subtree splits. Moving Inflection into Common. Moving Guzzle\Http\Parser to Guzzle\Parser. * BC: Removing Guzzle\Common\Batch\Batch::count() and replacing it with isEmpty() * CachePlugin now allows for a custom request parameter function to check if a request can be cached * Bug fix: CachePlugin now only caches GET and HEAD requests by default * Bug fix: Using header glue when transferring headers over the wire * Allowing deeply nested arrays for composite variables in URI templates * Batch divisors can now return iterators or arrays ## 2.7.1 - 2012-06-26 * Minor patch to update version number in UA string * Updating build process ## 2.7.0 - 2012-06-25 * BC: Inflection classes moved to Guzzle\Inflection. No longer static methods. Can now inject custom inflectors into classes. * BC: Removed magic setX methods from commands * BC: Magic methods mapped to service description commands are now inflected in the command factory rather than the client __call() method * Verbose cURL options are no longer enabled by default. Set curl.debug to true on a client to enable. * Bug: Now allowing colons in a response start-line (e.g. HTTP/1.1 503 Service Unavailable: Back-end server is at capacity) * Guzzle\Service\Resource\ResourceIteratorApplyBatched now internally uses the Guzzle\Common\Batch namespace * Added Guzzle\Service\Plugin namespace and a PluginCollectionPlugin * Added the ability to set POST fields and files in a service description * Guzzle\Http\EntityBody::factory() now accepts objects with a __toString() method * Adding a command.before_prepare event to clients * Added BatchClosureTransfer and BatchClosureDivisor * BatchTransferException now includes references to the batch divisor and transfer strategies * Fixed some tests so that they pass more reliably * Added Guzzle\Common\Log\ArrayLogAdapter ## 2.6.6 - 2012-06-10 * BC: Removing Guzzle\Http\Plugin\BatchQueuePlugin * BC: Removing Guzzle\Service\Command\CommandSet * Adding generic batching system (replaces the batch queue plugin and command set) * Updating ZF cache and log adapters and now using ZF's composer repository * Bug: Setting the name of each ApiParam when creating through an ApiCommand * Adding result_type, result_doc, deprecated, and doc_url to service descriptions * Bug: Changed the default cookie header casing back to 'Cookie' ## 2.6.5 - 2012-06-03 * BC: Renaming Guzzle\Http\Message\RequestInterface::getResourceUri() to getResource() * BC: Removing unused AUTH_BASIC and AUTH_DIGEST constants from * BC: Guzzle\Http\Cookie is now used to manage Set-Cookie data, not Cookie data * BC: Renaming methods in the CookieJarInterface * Moving almost all cookie logic out of the CookiePlugin and into the Cookie or CookieJar implementations * Making the default glue for HTTP headers ';' instead of ',' * Adding a removeValue to Guzzle\Http\Message\Header * Adding getCookies() to request interface. * Making it easier to add event subscribers to HasDispatcherInterface classes. Can now directly call addSubscriber() ## 2.6.4 - 2012-05-30 * BC: Cleaning up how POST files are stored in EntityEnclosingRequest objects. Adding PostFile class. * BC: Moving ApiCommand specific functionality from the Inspector and on to the ApiCommand * Bug: Fixing magic method command calls on clients * Bug: Email constraint only validates strings * Bug: Aggregate POST fields when POST files are present in curl handle * Bug: Fixing default User-Agent header * Bug: Only appending or prepending parameters in commands if they are specified * Bug: Not requiring response reason phrases or status codes to match a predefined list of codes * Allowing the use of dot notation for class namespaces when using instance_of constraint * Added any_match validation constraint * Added an AsyncPlugin * Passing request object to the calculateWait method of the ExponentialBackoffPlugin * Allowing the result of a command object to be changed * Parsing location and type sub values when instantiating a service description rather than over and over at runtime ## 2.6.3 - 2012-05-23 * [BC] Guzzle\Common\FromConfigInterface no longer requires any config options. * [BC] Refactoring how POST files are stored on an EntityEnclosingRequest. They are now separate from POST fields. * You can now use an array of data when creating PUT request bodies in the request factory. * Removing the requirement that HTTPS requests needed a Cache-Control: public directive to be cacheable. * [Http] Adding support for Content-Type in multipart POST uploads per upload * [Http] Added support for uploading multiple files using the same name (foo[0], foo[1]) * Adding more POST data operations for easier manipulation of POST data. * You can now set empty POST fields. * The body of a request is only shown on EntityEnclosingRequest objects that do not use POST files. * Split the Guzzle\Service\Inspector::validateConfig method into two methods. One to initialize when a command is created, and one to validate. * CS updates ## 2.6.2 - 2012-05-19 * [Http] Better handling of nested scope requests in CurlMulti. Requests are now always prepares in the send() method rather than the addRequest() method. ## 2.6.1 - 2012-05-19 * [BC] Removing 'path' support in service descriptions. Use 'uri'. * [BC] Guzzle\Service\Inspector::parseDocBlock is now protected. Adding getApiParamsForClass() with cache. * [BC] Removing Guzzle\Common\NullObject. Use https://github.com/mtdowling/NullObject if you need it. * [BC] Removing Guzzle\Common\XmlElement. * All commands, both dynamic and concrete, have ApiCommand objects. * Adding a fix for CurlMulti so that if all of the connections encounter some sort of curl error, then the loop exits. * Adding checks to EntityEnclosingRequest so that empty POST files and fields are ignored. * Making the method signature of Guzzle\Service\Builder\ServiceBuilder::factory more flexible. ## 2.6.0 - 2012-05-15 * [BC] Moving Guzzle\Service\Builder to Guzzle\Service\Builder\ServiceBuilder * [BC] Executing a Command returns the result of the command rather than the command * [BC] Moving all HTTP parsing logic to Guzzle\Http\Parsers. Allows for faster C implementations if needed. * [BC] Changing the Guzzle\Http\Message\Response::setProtocol() method to accept a protocol and version in separate args. * [BC] Moving ResourceIterator* to Guzzle\Service\Resource * [BC] Completely refactored ResourceIterators to iterate over a cloned command object * [BC] Moved Guzzle\Http\UriTemplate to Guzzle\Http\Parser\UriTemplate\UriTemplate * [BC] Guzzle\Guzzle is now deprecated * Moving Guzzle\Common\Guzzle::inject to Guzzle\Common\Collection::inject * Adding Guzzle\Version class to give version information about Guzzle * Adding Guzzle\Http\Utils class to provide getDefaultUserAgent() and getHttpDate() * Adding Guzzle\Curl\CurlVersion to manage caching curl_version() data * ServiceDescription and ServiceBuilder are now cacheable using similar configs * Changing the format of XML and JSON service builder configs. Backwards compatible. * Cleaned up Cookie parsing * Trimming the default Guzzle User-Agent header * Adding a setOnComplete() method to Commands that is called when a command completes * Keeping track of requests that were mocked in the MockPlugin * Fixed a caching bug in the CacheAdapterFactory * Inspector objects can be injected into a Command object * Refactoring a lot of code and tests to be case insensitive when dealing with headers * Adding Guzzle\Http\Message\HeaderComparison for easy comparison of HTTP headers using a DSL * Adding the ability to set global option overrides to service builder configs * Adding the ability to include other service builder config files from within XML and JSON files * Moving the parseQuery method out of Url and on to QueryString::fromString() as a static factory method. ## 2.5.0 - 2012-05-08 * Major performance improvements * [BC] Simplifying Guzzle\Common\Collection. Please check to see if you are using features that are now deprecated. * [BC] Using a custom validation system that allows a flyweight implementation for much faster validation. No longer using Symfony2 Validation component. * [BC] No longer supporting "{{ }}" for injecting into command or UriTemplates. Use "{}" * Added the ability to passed parameters to all requests created by a client * Added callback functionality to the ExponentialBackoffPlugin * Using microtime in ExponentialBackoffPlugin to allow more granular backoff strategies. * Rewinding request stream bodies when retrying requests * Exception is thrown when JSON response body cannot be decoded * Added configurable magic method calls to clients and commands. This is off by default. * Fixed a defect that added a hash to every parsed URL part * Fixed duplicate none generation for OauthPlugin. * Emitting an event each time a client is generated by a ServiceBuilder * Using an ApiParams object instead of a Collection for parameters of an ApiCommand * cache.* request parameters should be renamed to params.cache.* * Added the ability to set arbitrary curl options on requests (disable_wire, progress, etc.). See CurlHandle. * Added the ability to disable type validation of service descriptions * ServiceDescriptions and ServiceBuilders are now Serializable { "name": "guzzlehttp/guzzle", "type": "library", "description": "Guzzle is a PHP HTTP client library", "keywords": [ "framework", "http", "rest", "web service", "curl", "client", "HTTP client" ], "homepage": "http://guzzlephp.org/", "license": "MIT", "authors": [ { "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" } ], "require": { "php": ">=5.5", "ext-json": "*", "guzzlehttp/promises": "^1.0", "guzzlehttp/psr7": "^1.6.1" }, "require-dev": { "ext-curl": "*", "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0", "psr/log": "^1.1" }, "suggest": { "psr/log": "Required for using the Log middleware", "ext-intl": "Required for Internationalized Domain Name (IDN) support" }, "config": { "sort-packages": true }, "extra": { "branch-alias": { "dev-master": "6.5-dev" } }, "autoload": { "psr-4": { "GuzzleHttp\\": "src/" }, "files": [ "src/functions_include.php" ] }, "autoload-dev": { "psr-4": { "GuzzleHttp\\Tests\\": "tests/" } } } Guzzle Upgrade Guide ==================== 5.0 to 6.0 ---------- Guzzle now uses [PSR-7](http://www.php-fig.org/psr/psr-7/) for HTTP messages. Due to the fact that these messages are immutable, this prompted a refactoring of Guzzle to use a middleware based system rather than an event system. Any HTTP message interaction (e.g., `GuzzleHttp\Message\Request`) need to be updated to work with the new immutable PSR-7 request and response objects. Any event listeners or subscribers need to be updated to become middleware functions that wrap handlers (or are injected into a `GuzzleHttp\HandlerStack`). - Removed `GuzzleHttp\BatchResults` - Removed `GuzzleHttp\Collection` - Removed `GuzzleHttp\HasDataTrait` - Removed `GuzzleHttp\ToArrayInterface` - The `guzzlehttp/streams` dependency has been removed. Stream functionality is now present in the `GuzzleHttp\Psr7` namespace provided by the `guzzlehttp/psr7` package. - Guzzle no longer uses ReactPHP promises and now uses the `guzzlehttp/promises` library. We use a custom promise library for three significant reasons: 1. React promises (at the time of writing this) are recursive. Promise chaining and promise resolution will eventually blow the stack. Guzzle promises are not recursive as they use a sort of trampolining technique. Note: there has been movement in the React project to modify promises to no longer utilize recursion. 2. Guzzle needs to have the ability to synchronously block on a promise to wait for a result. Guzzle promises allows this functionality (and does not require the use of recursion). 3. Because we need to be able to wait on a result, doing so using React promises requires wrapping react promises with RingPHP futures. This overhead is no longer needed, reducing stack sizes, reducing complexity, and improving performance. - `GuzzleHttp\Mimetypes` has been moved to a function in `GuzzleHttp\Psr7\mimetype_from_extension` and `GuzzleHttp\Psr7\mimetype_from_filename`. - `GuzzleHttp\Query` and `GuzzleHttp\QueryParser` have been removed. Query strings must now be passed into request objects as strings, or provided to the `query` request option when creating requests with clients. The `query` option uses PHP's `http_build_query` to convert an array to a string. If you need a different serialization technique, you will need to pass the query string in as a string. There are a couple helper functions that will make working with query strings easier: `GuzzleHttp\Psr7\parse_query` and `GuzzleHttp\Psr7\build_query`. - Guzzle no longer has a dependency on RingPHP. Due to the use of a middleware system based on PSR-7, using RingPHP and it's middleware system as well adds more complexity than the benefits it provides. All HTTP handlers that were present in RingPHP have been modified to work directly with PSR-7 messages and placed in the `GuzzleHttp\Handler` namespace. This significantly reduces complexity in Guzzle, removes a dependency, and improves performance. RingPHP will be maintained for Guzzle 5 support, but will no longer be a part of Guzzle 6. - As Guzzle now uses a middleware based systems the event system and RingPHP integration has been removed. Note: while the event system has been removed, it is possible to add your own type of event system that is powered by the middleware system. - Removed the `Event` namespace. - Removed the `Subscriber` namespace. - Removed `Transaction` class - Removed `RequestFsm` - Removed `RingBridge` - `GuzzleHttp\Subscriber\Cookie` is now provided by `GuzzleHttp\Middleware::cookies` - `GuzzleHttp\Subscriber\HttpError` is now provided by `GuzzleHttp\Middleware::httpError` - `GuzzleHttp\Subscriber\History` is now provided by `GuzzleHttp\Middleware::history` - `GuzzleHttp\Subscriber\Mock` is now provided by `GuzzleHttp\Handler\MockHandler` - `GuzzleHttp\Subscriber\Prepare` is now provided by `GuzzleHttp\PrepareBodyMiddleware` - `GuzzleHttp\Subscriber\Redirect` is now provided by `GuzzleHttp\RedirectMiddleware` - Guzzle now uses `Psr\Http\Message\UriInterface` (implements in `GuzzleHttp\Psr7\Uri`) for URI support. `GuzzleHttp\Url` is now gone. - Static functions in `GuzzleHttp\Utils` have been moved to namespaced functions under the `GuzzleHttp` namespace. This requires either a Composer based autoloader or you to include functions.php. - `GuzzleHttp\ClientInterface::getDefaultOption` has been renamed to `GuzzleHttp\ClientInterface::getConfig`. - `GuzzleHttp\ClientInterface::setDefaultOption` has been removed. - The `json` and `xml` methods of response objects has been removed. With the migration to strictly adhering to PSR-7 as the interface for Guzzle messages, adding methods to message interfaces would actually require Guzzle messages to extend from PSR-7 messages rather then work with them directly. ## Migrating to middleware The change to PSR-7 unfortunately required significant refactoring to Guzzle due to the fact that PSR-7 messages are immutable. Guzzle 5 relied on an event system from plugins. The event system relied on mutability of HTTP messages and side effects in order to work. With immutable messages, you have to change your workflow to become more about either returning a value (e.g., functional middlewares) or setting a value on an object. Guzzle v6 has chosen the functional middleware approach. Instead of using the event system to listen for things like the `before` event, you now create a stack based middleware function that intercepts a request on the way in and the promise of the response on the way out. This is a much simpler and more predictable approach than the event system and works nicely with PSR-7 middleware. Due to the use of promises, the middleware system is also asynchronous. v5: ```php use GuzzleHttp\Event\BeforeEvent; $client = new GuzzleHttp\Client(); // Get the emitter and listen to the before event. $client->getEmitter()->on('before', function (BeforeEvent $e) { // Guzzle v5 events relied on mutation $e->getRequest()->setHeader('X-Foo', 'Bar'); }); ``` v6: In v6, you can modify the request before it is sent using the `mapRequest` middleware. The idiomatic way in v6 to modify the request/response lifecycle is to setup a handler middleware stack up front and inject the handler into a client. ```php use GuzzleHttp\Middleware; // Create a handler stack that has all of the default middlewares attached $handler = GuzzleHttp\HandlerStack::create(); // Push the handler onto the handler stack $handler->push(Middleware::mapRequest(function (RequestInterface $request) { // Notice that we have to return a request object return $request->withHeader('X-Foo', 'Bar'); })); // Inject the handler into the client $client = new GuzzleHttp\Client(['handler' => $handler]); ``` ## POST Requests This version added the [`form_params`](http://guzzle.readthedocs.org/en/latest/request-options.html#form_params) and `multipart` request options. `form_params` is an associative array of strings or array of strings and is used to serialize an `application/x-www-form-urlencoded` POST request. The [`multipart`](http://guzzle.readthedocs.org/en/latest/request-options.html#multipart) option is now used to send a multipart/form-data POST request. `GuzzleHttp\Post\PostFile` has been removed. Use the `multipart` option to add POST files to a multipart/form-data request. The `body` option no longer accepts an array to send POST requests. Please use `multipart` or `form_params` instead. The `base_url` option has been renamed to `base_uri`. 4.x to 5.0 ---------- ## Rewritten Adapter Layer Guzzle now uses [RingPHP](http://ringphp.readthedocs.org/en/latest) to send HTTP requests. The `adapter` option in a `GuzzleHttp\Client` constructor is still supported, but it has now been renamed to `handler`. Instead of passing a `GuzzleHttp\Adapter\AdapterInterface`, you must now pass a PHP `callable` that follows the RingPHP specification. ## Removed Fluent Interfaces [Fluent interfaces were removed](http://ocramius.github.io/blog/fluent-interfaces-are-evil) from the following classes: - `GuzzleHttp\Collection` - `GuzzleHttp\Url` - `GuzzleHttp\Query` - `GuzzleHttp\Post\PostBody` - `GuzzleHttp\Cookie\SetCookie` ## Removed functions.php Removed "functions.php", so that Guzzle is truly PSR-4 compliant. The following functions can be used as replacements. - `GuzzleHttp\json_decode` -> `GuzzleHttp\Utils::jsonDecode` - `GuzzleHttp\get_path` -> `GuzzleHttp\Utils::getPath` - `GuzzleHttp\Utils::setPath` -> `GuzzleHttp\set_path` - `GuzzleHttp\Pool::batch` -> `GuzzleHttp\batch`. This function is, however, deprecated in favor of using `GuzzleHttp\Pool::batch()`. The "procedural" global client has been removed with no replacement (e.g., `GuzzleHttp\get()`, `GuzzleHttp\post()`, etc.). Use a `GuzzleHttp\Client` object as a replacement. ## `throwImmediately` has been removed The concept of "throwImmediately" has been removed from exceptions and error events. This control mechanism was used to stop a transfer of concurrent requests from completing. This can now be handled by throwing the exception or by cancelling a pool of requests or each outstanding future request individually. ## headers event has been removed Removed the "headers" event. This event was only useful for changing the body a response once the headers of the response were known. You can implement a similar behavior in a number of ways. One example might be to use a FnStream that has access to the transaction being sent. For example, when the first byte is written, you could check if the response headers match your expectations, and if so, change the actual stream body that is being written to. ## Updates to HTTP Messages Removed the `asArray` parameter from `GuzzleHttp\Message\MessageInterface::getHeader`. If you want to get a header value as an array, then use the newly added `getHeaderAsArray()` method of `MessageInterface`. This change makes the Guzzle interfaces compatible with the PSR-7 interfaces. 3.x to 4.0 ---------- ## Overarching changes: - Now requires PHP 5.4 or greater. - No longer requires cURL to send requests. - Guzzle no longer wraps every exception it throws. Only exceptions that are recoverable are now wrapped by Guzzle. - Various namespaces have been removed or renamed. - No longer requiring the Symfony EventDispatcher. A custom event dispatcher based on the Symfony EventDispatcher is now utilized in `GuzzleHttp\Event\EmitterInterface` (resulting in significant speed and functionality improvements). Changes per Guzzle 3.x namespace are described below. ## Batch The `Guzzle\Batch` namespace has been removed. This is best left to third-parties to implement on top of Guzzle's core HTTP library. ## Cache The `Guzzle\Cache` namespace has been removed. (Todo: No suitable replacement has been implemented yet, but hoping to utilize a PSR cache interface). ## Common - Removed all of the wrapped exceptions. It's better to use the standard PHP library for unrecoverable exceptions. - `FromConfigInterface` has been removed. - `Guzzle\Common\Version` has been removed. The VERSION constant can be found at `GuzzleHttp\ClientInterface::VERSION`. ### Collection - `getAll` has been removed. Use `toArray` to convert a collection to an array. - `inject` has been removed. - `keySearch` has been removed. - `getPath` no longer supports wildcard expressions. Use something better like JMESPath for this. - `setPath` now supports appending to an existing array via the `[]` notation. ### Events Guzzle no longer requires Symfony's EventDispatcher component. Guzzle now uses `GuzzleHttp\Event\Emitter`. - `Symfony\Component\EventDispatcher\EventDispatcherInterface` is replaced by `GuzzleHttp\Event\EmitterInterface`. - `Symfony\Component\EventDispatcher\EventDispatcher` is replaced by `GuzzleHttp\Event\Emitter`. - `Symfony\Component\EventDispatcher\Event` is replaced by `GuzzleHttp\Event\Event`, and Guzzle now has an EventInterface in `GuzzleHttp\Event\EventInterface`. - `AbstractHasDispatcher` has moved to a trait, `HasEmitterTrait`, and `HasDispatcherInterface` has moved to `HasEmitterInterface`. Retrieving the event emitter of a request, client, etc. now uses the `getEmitter` method rather than the `getDispatcher` method. #### Emitter - Use the `once()` method to add a listener that automatically removes itself the first time it is invoked. - Use the `listeners()` method to retrieve a list of event listeners rather than the `getListeners()` method. - Use `emit()` instead of `dispatch()` to emit an event from an emitter. - Use `attach()` instead of `addSubscriber()` and `detach()` instead of `removeSubscriber()`. ```php $mock = new Mock(); // 3.x $request->getEventDispatcher()->addSubscriber($mock); $request->getEventDispatcher()->removeSubscriber($mock); // 4.x $request->getEmitter()->attach($mock); $request->getEmitter()->detach($mock); ``` Use the `on()` method to add a listener rather than the `addListener()` method. ```php // 3.x $request->getEventDispatcher()->addListener('foo', function (Event $event) { /* ... */ } ); // 4.x $request->getEmitter()->on('foo', function (Event $event, $name) { /* ... */ } ); ``` ## Http ### General changes - The cacert.pem certificate has been moved to `src/cacert.pem`. - Added the concept of adapters that are used to transfer requests over the wire. - Simplified the event system. - Sending requests in parallel is still possible, but batching is no longer a concept of the HTTP layer. Instead, you must use the `complete` and `error` events to asynchronously manage parallel request transfers. - `Guzzle\Http\Url` has moved to `GuzzleHttp\Url`. - `Guzzle\Http\QueryString` has moved to `GuzzleHttp\Query`. - QueryAggregators have been rewritten so that they are simply callable functions. - `GuzzleHttp\StaticClient` has been removed. Use the functions provided in `functions.php` for an easy to use static client instance. - Exceptions in `GuzzleHttp\Exception` have been updated to all extend from `GuzzleHttp\Exception\TransferException`. ### Client Calling methods like `get()`, `post()`, `head()`, etc. no longer create and return a request, but rather creates a request, sends the request, and returns the response. ```php // 3.0 $request = $client->get('/'); $response = $request->send(); // 4.0 $response = $client->get('/'); // or, to mirror the previous behavior $request = $client->createRequest('GET', '/'); $response = $client->send($request); ``` `GuzzleHttp\ClientInterface` has changed. - The `send` method no longer accepts more than one request. Use `sendAll` to send multiple requests in parallel. - `setUserAgent()` has been removed. Use a default request option instead. You could, for example, do something like: `$client->setConfig('defaults/headers/User-Agent', 'Foo/Bar ' . $client::getDefaultUserAgent())`. - `setSslVerification()` has been removed. Use default request options instead, like `$client->setConfig('defaults/verify', true)`. `GuzzleHttp\Client` has changed. - The constructor now accepts only an associative array. You can include a `base_url` string or array to use a URI template as the base URL of a client. You can also specify a `defaults` key that is an associative array of default request options. You can pass an `adapter` to use a custom adapter, `batch_adapter` to use a custom adapter for sending requests in parallel, or a `message_factory` to change the factory used to create HTTP requests and responses. - The client no longer emits a `client.create_request` event. - Creating requests with a client no longer automatically utilize a URI template. You must pass an array into a creational method (e.g., `createRequest`, `get`, `put`, etc.) in order to expand a URI template. ### Messages Messages no longer have references to their counterparts (i.e., a request no longer has a reference to it's response, and a response no loger has a reference to its request). This association is now managed through a `GuzzleHttp\Adapter\TransactionInterface` object. You can get references to these transaction objects using request events that are emitted over the lifecycle of a request. #### Requests with a body - `GuzzleHttp\Message\EntityEnclosingRequest` and `GuzzleHttp\Message\EntityEnclosingRequestInterface` have been removed. The separation between requests that contain a body and requests that do not contain a body has been removed, and now `GuzzleHttp\Message\RequestInterface` handles both use cases. - Any method that previously accepts a `GuzzleHttp\Response` object now accept a `GuzzleHttp\Message\ResponseInterface`. - `GuzzleHttp\Message\RequestFactoryInterface` has been renamed to `GuzzleHttp\Message\MessageFactoryInterface`. This interface is used to create both requests and responses and is implemented in `GuzzleHttp\Message\MessageFactory`. - POST field and file methods have been removed from the request object. You must now use the methods made available to `GuzzleHttp\Post\PostBodyInterface` to control the format of a POST body. Requests that are created using a standard `GuzzleHttp\Message\MessageFactoryInterface` will automatically use a `GuzzleHttp\Post\PostBody` body if the body was passed as an array or if the method is POST and no body is provided. ```php $request = $client->createRequest('POST', '/'); $request->getBody()->setField('foo', 'bar'); $request->getBody()->addFile(new PostFile('file_key', fopen('/path/to/content', 'r'))); ``` #### Headers - `GuzzleHttp\Message\Header` has been removed. Header values are now simply represented by an array of values or as a string. Header values are returned as a string by default when retrieving a header value from a message. You can pass an optional argument of `true` to retrieve a header value as an array of strings instead of a single concatenated string. - `GuzzleHttp\PostFile` and `GuzzleHttp\PostFileInterface` have been moved to `GuzzleHttp\Post`. This interface has been simplified and now allows the addition of arbitrary headers. - Custom headers like `GuzzleHttp\Message\Header\Link` have been removed. Most of the custom headers are now handled separately in specific subscribers/plugins, and `GuzzleHttp\Message\HeaderValues::parseParams()` has been updated to properly handle headers that contain parameters (like the `Link` header). #### Responses - `GuzzleHttp\Message\Response::getInfo()` and `GuzzleHttp\Message\Response::setInfo()` have been removed. Use the event system to retrieve this type of information. - `GuzzleHttp\Message\Response::getRawHeaders()` has been removed. - `GuzzleHttp\Message\Response::getMessage()` has been removed. - `GuzzleHttp\Message\Response::calculateAge()` and other cache specific methods have moved to the CacheSubscriber. - Header specific helper functions like `getContentMd5()` have been removed. Just use `getHeader('Content-MD5')` instead. - `GuzzleHttp\Message\Response::setRequest()` and `GuzzleHttp\Message\Response::getRequest()` have been removed. Use the event system to work with request and response objects as a transaction. - `GuzzleHttp\Message\Response::getRedirectCount()` has been removed. Use the Redirect subscriber instead. - `GuzzleHttp\Message\Response::isSuccessful()` and other related methods have been removed. Use `getStatusCode()` instead. #### Streaming responses Streaming requests can now be created by a client directly, returning a `GuzzleHttp\Message\ResponseInterface` object that contains a body stream referencing an open PHP HTTP stream. ```php // 3.0 use Guzzle\Stream\PhpStreamRequestFactory; $request = $client->get('/'); $factory = new PhpStreamRequestFactory(); $stream = $factory->fromRequest($request); $data = $stream->read(1024); // 4.0 $response = $client->get('/', ['stream' => true]); // Read some data off of the stream in the response body $data = $response->getBody()->read(1024); ``` #### Redirects The `configureRedirects()` method has been removed in favor of a `allow_redirects` request option. ```php // Standard redirects with a default of a max of 5 redirects $request = $client->createRequest('GET', '/', ['allow_redirects' => true]); // Strict redirects with a custom number of redirects $request = $client->createRequest('GET', '/', [ 'allow_redirects' => ['max' => 5, 'strict' => true] ]); ``` #### EntityBody EntityBody interfaces and classes have been removed or moved to `GuzzleHttp\Stream`. All classes and interfaces that once required `GuzzleHttp\EntityBodyInterface` now require `GuzzleHttp\Stream\StreamInterface`. Creating a new body for a request no longer uses `GuzzleHttp\EntityBody::factory` but now uses `GuzzleHttp\Stream\Stream::factory` or even better: `GuzzleHttp\Stream\create()`. - `Guzzle\Http\EntityBodyInterface` is now `GuzzleHttp\Stream\StreamInterface` - `Guzzle\Http\EntityBody` is now `GuzzleHttp\Stream\Stream` - `Guzzle\Http\CachingEntityBody` is now `GuzzleHttp\Stream\CachingStream` - `Guzzle\Http\ReadLimitEntityBody` is now `GuzzleHttp\Stream\LimitStream` - `Guzzle\Http\IoEmittyinEntityBody` has been removed. #### Request lifecycle events Requests previously submitted a large number of requests. The number of events emitted over the lifecycle of a request has been significantly reduced to make it easier to understand how to extend the behavior of a request. All events emitted during the lifecycle of a request now emit a custom `GuzzleHttp\Event\EventInterface` object that contains context providing methods and a way in which to modify the transaction at that specific point in time (e.g., intercept the request and set a response on the transaction). - `request.before_send` has been renamed to `before` and now emits a `GuzzleHttp\Event\BeforeEvent` - `request.complete` has been renamed to `complete` and now emits a `GuzzleHttp\Event\CompleteEvent`. - `request.sent` has been removed. Use `complete`. - `request.success` has been removed. Use `complete`. - `error` is now an event that emits a `GuzzleHttp\Event\ErrorEvent`. - `request.exception` has been removed. Use `error`. - `request.receive.status_line` has been removed. - `curl.callback.progress` has been removed. Use a custom `StreamInterface` to maintain a status update. - `curl.callback.write` has been removed. Use a custom `StreamInterface` to intercept writes. - `curl.callback.read` has been removed. Use a custom `StreamInterface` to intercept reads. `headers` is a new event that is emitted after the response headers of a request have been received before the body of the response is downloaded. This event emits a `GuzzleHttp\Event\HeadersEvent`. You can intercept a request and inject a response using the `intercept()` event of a `GuzzleHttp\Event\BeforeEvent`, `GuzzleHttp\Event\CompleteEvent`, and `GuzzleHttp\Event\ErrorEvent` event. See: http://docs.guzzlephp.org/en/latest/events.html ## Inflection The `Guzzle\Inflection` namespace has been removed. This is not a core concern of Guzzle. ## Iterator The `Guzzle\Iterator` namespace has been removed. - `Guzzle\Iterator\AppendIterator`, `Guzzle\Iterator\ChunkedIterator`, and `Guzzle\Iterator\MethodProxyIterator` are nice, but not a core requirement of Guzzle itself. - `Guzzle\Iterator\FilterIterator` is no longer needed because an equivalent class is shipped with PHP 5.4. - `Guzzle\Iterator\MapIterator` is not really needed when using PHP 5.5 because it's easier to just wrap an iterator in a generator that maps values. For a replacement of these iterators, see https://github.com/nikic/iter ## Log The LogPlugin has moved to https://github.com/guzzle/log-subscriber. The `Guzzle\Log` namespace has been removed. Guzzle now relies on `Psr\Log\LoggerInterface` for all logging. The MessageFormatter class has been moved to `GuzzleHttp\Subscriber\Log\Formatter`. ## Parser The `Guzzle\Parser` namespace has been removed. This was previously used to make it possible to plug in custom parsers for cookies, messages, URI templates, and URLs; however, this level of complexity is not needed in Guzzle so it has been removed. - Cookie: Cookie parsing logic has been moved to `GuzzleHttp\Cookie\SetCookie::fromString`. - Message: Message parsing logic for both requests and responses has been moved to `GuzzleHttp\Message\MessageFactory::fromMessage`. Message parsing is only used in debugging or deserializing messages, so it doesn't make sense for Guzzle as a library to add this level of complexity to parsing messages. - UriTemplate: URI template parsing has been moved to `GuzzleHttp\UriTemplate`. The Guzzle library will automatically use the PECL URI template library if it is installed. - Url: URL parsing is now performed in `GuzzleHttp\Url::fromString` (previously it was `Guzzle\Http\Url::factory()`). If custom URL parsing is necessary, then developers are free to subclass `GuzzleHttp\Url`. ## Plugin The `Guzzle\Plugin` namespace has been renamed to `GuzzleHttp\Subscriber`. Several plugins are shipping with the core Guzzle library under this namespace. - `GuzzleHttp\Subscriber\Cookie`: Replaces the old CookiePlugin. Cookie jar code has moved to `GuzzleHttp\Cookie`. - `GuzzleHttp\Subscriber\History`: Replaces the old HistoryPlugin. - `GuzzleHttp\Subscriber\HttpError`: Throws errors when a bad HTTP response is received. - `GuzzleHttp\Subscriber\Mock`: Replaces the old MockPlugin. - `GuzzleHttp\Subscriber\Prepare`: Prepares the body of a request just before sending. This subscriber is attached to all requests by default. - `GuzzleHttp\Subscriber\Redirect`: Replaces the RedirectPlugin. The following plugins have been removed (third-parties are free to re-implement these if needed): - `GuzzleHttp\Plugin\Async` has been removed. - `GuzzleHttp\Plugin\CurlAuth` has been removed. - `GuzzleHttp\Plugin\ErrorResponse\ErrorResponsePlugin` has been removed. This functionality should instead be implemented with event listeners that occur after normal response parsing occurs in the guzzle/command package. The following plugins are not part of the core Guzzle package, but are provided in separate repositories: - `Guzzle\Http\Plugin\BackoffPlugin` has been rewritten to be much simpler to build custom retry policies using simple functions rather than various chained classes. See: https://github.com/guzzle/retry-subscriber - `Guzzle\Http\Plugin\Cache\CachePlugin` has moved to https://github.com/guzzle/cache-subscriber - `Guzzle\Http\Plugin\Log\LogPlugin` has moved to https://github.com/guzzle/log-subscriber - `Guzzle\Http\Plugin\Md5\Md5Plugin` has moved to https://github.com/guzzle/message-integrity-subscriber - `Guzzle\Http\Plugin\Mock\MockPlugin` has moved to `GuzzleHttp\Subscriber\MockSubscriber`. - `Guzzle\Http\Plugin\Oauth\OauthPlugin` has moved to https://github.com/guzzle/oauth-subscriber ## Service The service description layer of Guzzle has moved into two separate packages: - http://github.com/guzzle/command Provides a high level abstraction over web services by representing web service operations using commands. - http://github.com/guzzle/guzzle-services Provides an implementation of guzzle/command that provides request serialization and response parsing using Guzzle service descriptions. ## Stream Stream have moved to a separate package available at https://github.com/guzzle/streams. `Guzzle\Stream\StreamInterface` has been given a large update to cleanly take on the responsibilities of `Guzzle\Http\EntityBody` and `Guzzle\Http\EntityBodyInterface` now that they have been removed. The number of methods implemented by the `StreamInterface` has been drastically reduced to allow developers to more easily extend and decorate stream behavior. ## Removed methods from StreamInterface - `getStream` and `setStream` have been removed to better encapsulate streams. - `getMetadata` and `setMetadata` have been removed in favor of `GuzzleHttp\Stream\MetadataStreamInterface`. - `getWrapper`, `getWrapperData`, `getStreamType`, and `getUri` have all been removed. This data is accessible when using streams that implement `GuzzleHttp\Stream\MetadataStreamInterface`. - `rewind` has been removed. Use `seek(0)` for a similar behavior. ## Renamed methods - `detachStream` has been renamed to `detach`. - `feof` has been renamed to `eof`. - `ftell` has been renamed to `tell`. - `readLine` has moved from an instance method to a static class method of `GuzzleHttp\Stream\Stream`. ## Metadata streams `GuzzleHttp\Stream\MetadataStreamInterface` has been added to denote streams that contain additional metadata accessible via `getMetadata()`. `GuzzleHttp\Stream\StreamInterface::getMetadata` and `GuzzleHttp\Stream\StreamInterface::setMetadata` have been removed. ## StreamRequestFactory The entire concept of the StreamRequestFactory has been removed. The way this was used in Guzzle 3 broke the actual interface of sending streaming requests (instead of getting back a Response, you got a StreamInterface). Streaming PHP requests are now implemented through the `GuzzleHttp\Adapter\StreamAdapter`. 3.6 to 3.7 ---------- ### Deprecations - You can now enable E_USER_DEPRECATED warnings to see if you are using any deprecated methods.: ```php \Guzzle\Common\Version::$emitWarnings = true; ``` The following APIs and options have been marked as deprecated: - Marked `Guzzle\Http\Message\Request::isResponseBodyRepeatable()` as deprecated. Use `$request->getResponseBody()->isRepeatable()` instead. - Marked `Guzzle\Http\Message\Request::canCache()` as deprecated. Use `Guzzle\Plugin\Cache\DefaultCanCacheStrategy->canCacheRequest()` instead. - Marked `Guzzle\Http\Message\Request::canCache()` as deprecated. Use `Guzzle\Plugin\Cache\DefaultCanCacheStrategy->canCacheRequest()` instead. - Marked `Guzzle\Http\Message\Request::setIsRedirect()` as deprecated. Use the HistoryPlugin instead. - Marked `Guzzle\Http\Message\Request::isRedirect()` as deprecated. Use the HistoryPlugin instead. - Marked `Guzzle\Cache\CacheAdapterFactory::factory()` as deprecated - Marked `Guzzle\Service\Client::enableMagicMethods()` as deprecated. Magic methods can no longer be disabled on a Guzzle\Service\Client. - Marked `Guzzle\Parser\Url\UrlParser` as deprecated. Just use PHP's `parse_url()` and percent encode your UTF-8. - Marked `Guzzle\Common\Collection::inject()` as deprecated. - Marked `Guzzle\Plugin\CurlAuth\CurlAuthPlugin` as deprecated. Use `$client->getConfig()->setPath('request.options/auth', array('user', 'pass', 'Basic|Digest|NTLM|Any'));` or `$client->setDefaultOption('auth', array('user', 'pass', 'Basic|Digest|NTLM|Any'));` 3.7 introduces `request.options` as a parameter for a client configuration and as an optional argument to all creational request methods. When paired with a client's configuration settings, these options allow you to specify default settings for various aspects of a request. Because these options make other previous configuration options redundant, several configuration options and methods of a client and AbstractCommand have been deprecated. - Marked `Guzzle\Service\Client::getDefaultHeaders()` as deprecated. Use `$client->getDefaultOption('headers')`. - Marked `Guzzle\Service\Client::setDefaultHeaders()` as deprecated. Use `$client->setDefaultOption('headers/{header_name}', 'value')`. - Marked 'request.params' for `Guzzle\Http\Client` as deprecated. Use `$client->setDefaultOption('params/{param_name}', 'value')` - Marked 'command.headers', 'command.response_body' and 'command.on_complete' as deprecated for AbstractCommand. These will work through Guzzle 4.0 $command = $client->getCommand('foo', array( 'command.headers' => array('Test' => '123'), 'command.response_body' => '/path/to/file' )); // Should be changed to: $command = $client->getCommand('foo', array( 'command.request_options' => array( 'headers' => array('Test' => '123'), 'save_as' => '/path/to/file' ) )); ### Interface changes Additions and changes (you will need to update any implementations or subclasses you may have created): - Added an `$options` argument to the end of the following methods of `Guzzle\Http\ClientInterface`: createRequest, head, delete, put, patch, post, options, prepareRequest - Added an `$options` argument to the end of `Guzzle\Http\Message\Request\RequestFactoryInterface::createRequest()` - Added an `applyOptions()` method to `Guzzle\Http\Message\Request\RequestFactoryInterface` - Changed `Guzzle\Http\ClientInterface::get($uri = null, $headers = null, $body = null)` to `Guzzle\Http\ClientInterface::get($uri = null, $headers = null, $options = array())`. You can still pass in a resource, string, or EntityBody into the $options parameter to specify the download location of the response. - Changed `Guzzle\Common\Collection::__construct($data)` to no longer accepts a null value for `$data` but a default `array()` - Added `Guzzle\Stream\StreamInterface::isRepeatable` - Made `Guzzle\Http\Client::expandTemplate` and `getUriTemplate` protected methods. The following methods were removed from interfaces. All of these methods are still available in the concrete classes that implement them, but you should update your code to use alternative methods: - Removed `Guzzle\Http\ClientInterface::setDefaultHeaders(). Use `$client->getConfig()->setPath('request.options/headers/{header_name}', 'value')`. or `$client->getConfig()->setPath('request.options/headers', array('header_name' => 'value'))` or `$client->setDefaultOption('headers/{header_name}', 'value')`. or `$client->setDefaultOption('headers', array('header_name' => 'value'))`. - Removed `Guzzle\Http\ClientInterface::getDefaultHeaders(). Use `$client->getConfig()->getPath('request.options/headers')`. - Removed `Guzzle\Http\ClientInterface::expandTemplate()`. This is an implementation detail. - Removed `Guzzle\Http\ClientInterface::setRequestFactory()`. This is an implementation detail. - Removed `Guzzle\Http\ClientInterface::getCurlMulti()`. This is a very specific implementation detail. - Removed `Guzzle\Http\Message\RequestInterface::canCache`. Use the CachePlugin. - Removed `Guzzle\Http\Message\RequestInterface::setIsRedirect`. Use the HistoryPlugin. - Removed `Guzzle\Http\Message\RequestInterface::isRedirect`. Use the HistoryPlugin. ### Cache plugin breaking changes - CacheKeyProviderInterface and DefaultCacheKeyProvider are no longer used. All of this logic is handled in a CacheStorageInterface. These two objects and interface will be removed in a future version. - Always setting X-cache headers on cached responses - Default cache TTLs are now handled by the CacheStorageInterface of a CachePlugin - `CacheStorageInterface::cache($key, Response $response, $ttl = null)` has changed to `cache(RequestInterface $request, Response $response);` - `CacheStorageInterface::fetch($key)` has changed to `fetch(RequestInterface $request);` - `CacheStorageInterface::delete($key)` has changed to `delete(RequestInterface $request);` - Added `CacheStorageInterface::purge($url)` - `DefaultRevalidation::__construct(CacheKeyProviderInterface $cacheKey, CacheStorageInterface $cache, CachePlugin $plugin)` has changed to `DefaultRevalidation::__construct(CacheStorageInterface $cache, CanCacheStrategyInterface $canCache = null)` - Added `RevalidationInterface::shouldRevalidate(RequestInterface $request, Response $response)` 3.5 to 3.6 ---------- * Mixed casing of headers are now forced to be a single consistent casing across all values for that header. * Messages internally use a HeaderCollection object to delegate handling case-insensitive header resolution * Removed the whole changedHeader() function system of messages because all header changes now go through addHeader(). For example, setHeader() first removes the header using unset on a HeaderCollection and then calls addHeader(). Keeping the Host header and URL host in sync is now handled by overriding the addHeader method in Request. * Specific header implementations can be created for complex headers. When a message creates a header, it uses a HeaderFactory which can map specific headers to specific header classes. There is now a Link header and CacheControl header implementation. * Moved getLinks() from Response to just be used on a Link header object. If you previously relied on Guzzle\Http\Message\Header::raw(), then you will need to update your code to use the HeaderInterface (e.g. toArray(), getAll(), etc.). ### Interface changes * Removed from interface: Guzzle\Http\ClientInterface::setUriTemplate * Removed from interface: Guzzle\Http\ClientInterface::setCurlMulti() * Removed Guzzle\Http\Message\Request::receivedRequestHeader() and implemented this functionality in Guzzle\Http\Curl\RequestMediator * Removed the optional $asString parameter from MessageInterface::getHeader(). Just cast the header to a string. * Removed the optional $tryChunkedTransfer option from Guzzle\Http\Message\EntityEnclosingRequestInterface * Removed the $asObjects argument from Guzzle\Http\Message\MessageInterface::getHeaders() ### Removed deprecated functions * Removed Guzzle\Parser\ParserRegister::get(). Use getParser() * Removed Guzzle\Parser\ParserRegister::set(). Use registerParser(). ### Deprecations * The ability to case-insensitively search for header values * Guzzle\Http\Message\Header::hasExactHeader * Guzzle\Http\Message\Header::raw. Use getAll() * Deprecated cache control specific methods on Guzzle\Http\Message\AbstractMessage. Use the CacheControl header object instead. ### Other changes * All response header helper functions return a string rather than mixing Header objects and strings inconsistently * Removed cURL blacklist support. This is no longer necessary now that Expect, Accept, etc. are managed by Guzzle directly via interfaces * Removed the injecting of a request object onto a response object. The methods to get and set a request still exist but are a no-op until removed. * Most classes that used to require a `Guzzle\Service\Command\CommandInterface` typehint now request a `Guzzle\Service\Command\ArrayCommandInterface`. * Added `Guzzle\Http\Message\RequestInterface::startResponse()` to the RequestInterface to handle injecting a response on a request while the request is still being transferred * `Guzzle\Service\Command\CommandInterface` now extends from ToArrayInterface and ArrayAccess 3.3 to 3.4 ---------- Base URLs of a client now follow the rules of http://tools.ietf.org/html/rfc3986#section-5.2.2 when merging URLs. 3.2 to 3.3 ---------- ### Response::getEtag() quote stripping removed `Guzzle\Http\Message\Response::getEtag()` no longer strips quotes around the ETag response header ### Removed `Guzzle\Http\Utils` The `Guzzle\Http\Utils` class was removed. This class was only used for testing. ### Stream wrapper and type `Guzzle\Stream\Stream::getWrapper()` and `Guzzle\Stream\Stream::getStreamType()` are no longer converted to lowercase. ### curl.emit_io became emit_io Emitting IO events from a RequestMediator is now a parameter that must be set in a request's curl options using the 'emit_io' key. This was previously set under a request's parameters using 'curl.emit_io' 3.1 to 3.2 ---------- ### CurlMulti is no longer reused globally Before 3.2, the same CurlMulti object was reused globally for each client. This can cause issue where plugins added to a single client can pollute requests dispatched from other clients. If you still wish to reuse the same CurlMulti object with each client, then you can add a listener to the ServiceBuilder's `service_builder.create_client` event to inject a custom CurlMulti object into each client as it is created. ```php $multi = new Guzzle\Http\Curl\CurlMulti(); $builder = Guzzle\Service\Builder\ServiceBuilder::factory('/path/to/config.json'); $builder->addListener('service_builder.create_client', function ($event) use ($multi) { $event['client']->setCurlMulti($multi); } }); ``` ### No default path URLs no longer have a default path value of '/' if no path was specified. Before: ```php $request = $client->get('http://www.foo.com'); echo $request->getUrl(); // >> http://www.foo.com/ ``` After: ```php $request = $client->get('http://www.foo.com'); echo $request->getUrl(); // >> http://www.foo.com ``` ### Less verbose BadResponseException The exception message for `Guzzle\Http\Exception\BadResponseException` no longer contains the full HTTP request and response information. You can, however, get access to the request and response object by calling `getRequest()` or `getResponse()` on the exception object. ### Query parameter aggregation Multi-valued query parameters are no longer aggregated using a callback function. `Guzzle\Http\Query` now has a setAggregator() method that accepts a `Guzzle\Http\QueryAggregator\QueryAggregatorInterface` object. This object is responsible for handling the aggregation of multi-valued query string variables into a flattened hash. 2.8 to 3.x ---------- ### Guzzle\Service\Inspector Change `\Guzzle\Service\Inspector::fromConfig` to `\Guzzle\Common\Collection::fromConfig` **Before** ```php use Guzzle\Service\Inspector; class YourClient extends \Guzzle\Service\Client { public static function factory($config = array()) { $default = array(); $required = array('base_url', 'username', 'api_key'); $config = Inspector::fromConfig($config, $default, $required); $client = new self( $config->get('base_url'), $config->get('username'), $config->get('api_key') ); $client->setConfig($config); $client->setDescription(ServiceDescription::factory(__DIR__ . DIRECTORY_SEPARATOR . 'client.json')); return $client; } ``` **After** ```php use Guzzle\Common\Collection; class YourClient extends \Guzzle\Service\Client { public static function factory($config = array()) { $default = array(); $required = array('base_url', 'username', 'api_key'); $config = Collection::fromConfig($config, $default, $required); $client = new self( $config->get('base_url'), $config->get('username'), $config->get('api_key') ); $client->setConfig($config); $client->setDescription(ServiceDescription::factory(__DIR__ . DIRECTORY_SEPARATOR . 'client.json')); return $client; } ``` ### Convert XML Service Descriptions to JSON **Before** ```xml <?xml version="1.0" encoding="UTF-8"?> <client> <commands> <!-- Groups --> <command name="list_groups" method="GET" uri="groups.json"> <doc>Get a list of groups</doc> </command> <command name="search_groups" method="GET" uri='search.json?query="{{query}} type:group"'> <doc>Uses a search query to get a list of groups</doc> <param name="query" type="string" required="true" /> </command> <command name="create_group" method="POST" uri="groups.json"> <doc>Create a group</doc> <param name="data" type="array" location="body" filters="json_encode" doc="Group JSON"/> <param name="Content-Type" location="header" static="application/json"/> </command> <command name="delete_group" method="DELETE" uri="groups/{{id}}.json"> <doc>Delete a group by ID</doc> <param name="id" type="integer" required="true"/> </command> <command name="get_group" method="GET" uri="groups/{{id}}.json"> <param name="id" type="integer" required="true"/> </command> <command name="update_group" method="PUT" uri="groups/{{id}}.json"> <doc>Update a group</doc> <param name="id" type="integer" required="true"/> <param name="data" type="array" location="body" filters="json_encode" doc="Group JSON"/> <param name="Content-Type" location="header" static="application/json"/> </command> </commands> </client> ``` **After** ```json { "name": "Zendesk REST API v2", "apiVersion": "2012-12-31", "description":"Provides access to Zendesk views, groups, tickets, ticket fields, and users", "operations": { "list_groups": { "httpMethod":"GET", "uri": "groups.json", "summary": "Get a list of groups" }, "search_groups":{ "httpMethod":"GET", "uri": "search.json?query=\"{query} type:group\"", "summary": "Uses a search query to get a list of groups", "parameters":{ "query":{ "location": "uri", "description":"Zendesk Search Query", "type": "string", "required": true } } }, "create_group": { "httpMethod":"POST", "uri": "groups.json", "summary": "Create a group", "parameters":{ "data": { "type": "array", "location": "body", "description":"Group JSON", "filters": "json_encode", "required": true }, "Content-Type":{ "type": "string", "location":"header", "static": "application/json" } } }, "delete_group": { "httpMethod":"DELETE", "uri": "groups/{id}.json", "summary": "Delete a group", "parameters":{ "id":{ "location": "uri", "description":"Group to delete by ID", "type": "integer", "required": true } } }, "get_group": { "httpMethod":"GET", "uri": "groups/{id}.json", "summary": "Get a ticket", "parameters":{ "id":{ "location": "uri", "description":"Group to get by ID", "type": "integer", "required": true } } }, "update_group": { "httpMethod":"PUT", "uri": "groups/{id}.json", "summary": "Update a group", "parameters":{ "id": { "location": "uri", "description":"Group to update by ID", "type": "integer", "required": true }, "data": { "type": "array", "location": "body", "description":"Group JSON", "filters": "json_encode", "required": true }, "Content-Type":{ "type": "string", "location":"header", "static": "application/json" } } } } ``` ### Guzzle\Service\Description\ServiceDescription Commands are now called Operations **Before** ```php use Guzzle\Service\Description\ServiceDescription; $sd = new ServiceDescription(); $sd->getCommands(); // @returns ApiCommandInterface[] $sd->hasCommand($name); $sd->getCommand($name); // @returns ApiCommandInterface|null $sd->addCommand($command); // @param ApiCommandInterface $command ``` **After** ```php use Guzzle\Service\Description\ServiceDescription; $sd = new ServiceDescription(); $sd->getOperations(); // @returns OperationInterface[] $sd->hasOperation($name); $sd->getOperation($name); // @returns OperationInterface|null $sd->addOperation($operation); // @param OperationInterface $operation ``` ### Guzzle\Common\Inflection\Inflector Namespace is now `Guzzle\Inflection\Inflector` ### Guzzle\Http\Plugin Namespace is now `Guzzle\Plugin`. Many other changes occur within this namespace and are detailed in their own sections below. ### Guzzle\Http\Plugin\LogPlugin and Guzzle\Common\Log Now `Guzzle\Plugin\Log\LogPlugin` and `Guzzle\Log` respectively. **Before** ```php use Guzzle\Common\Log\ClosureLogAdapter; use Guzzle\Http\Plugin\LogPlugin; /** @var \Guzzle\Http\Client */ $client; // $verbosity is an integer indicating desired message verbosity level $client->addSubscriber(new LogPlugin(new ClosureLogAdapter(function($m) { echo $m; }, $verbosity = LogPlugin::LOG_VERBOSE); ``` **After** ```php use Guzzle\Log\ClosureLogAdapter; use Guzzle\Log\MessageFormatter; use Guzzle\Plugin\Log\LogPlugin; /** @var \Guzzle\Http\Client */ $client; // $format is a string indicating desired message format -- @see MessageFormatter $client->addSubscriber(new LogPlugin(new ClosureLogAdapter(function($m) { echo $m; }, $format = MessageFormatter::DEBUG_FORMAT); ``` ### Guzzle\Http\Plugin\CurlAuthPlugin Now `Guzzle\Plugin\CurlAuth\CurlAuthPlugin`. ### Guzzle\Http\Plugin\ExponentialBackoffPlugin Now `Guzzle\Plugin\Backoff\BackoffPlugin`, and other changes. **Before** ```php use Guzzle\Http\Plugin\ExponentialBackoffPlugin; $backoffPlugin = new ExponentialBackoffPlugin($maxRetries, array_merge( ExponentialBackoffPlugin::getDefaultFailureCodes(), array(429) )); $client->addSubscriber($backoffPlugin); ``` **After** ```php use Guzzle\Plugin\Backoff\BackoffPlugin; use Guzzle\Plugin\Backoff\HttpBackoffStrategy; // Use convenient factory method instead -- see implementation for ideas of what // you can do with chaining backoff strategies $backoffPlugin = BackoffPlugin::getExponentialBackoff($maxRetries, array_merge( HttpBackoffStrategy::getDefaultFailureCodes(), array(429) )); $client->addSubscriber($backoffPlugin); ``` ### Known Issues #### [BUG] Accept-Encoding header behavior changed unintentionally. (See #217) (Fixed in 09daeb8c666fb44499a0646d655a8ae36456575e) In version 2.8 setting the `Accept-Encoding` header would set the CURLOPT_ENCODING option, which permitted cURL to properly handle gzip/deflate compressed responses from the server. In versions affected by this bug this does not happen. See issue #217 for a workaround, or use a version containing the fix. Guzzle, PHP HTTP client ======================= [](https://github.com/guzzle/guzzle/releases) [](https://travis-ci.org/guzzle/guzzle) [](https://packagist.org/packages/guzzlehttp/guzzle) Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. - Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc... - Can send both synchronous and asynchronous requests using the same interface. - Uses PSR-7 interfaces for requests, responses, and streams. This allows you to utilize other PSR-7 compatible libraries with Guzzle. - Abstracts away the underlying HTTP transport, allowing you to write environment and transport agnostic code; i.e., no hard dependency on cURL, PHP streams, sockets, or non-blocking event loops. - Middleware system allows you to augment and compose client behavior. ```php $client = new \GuzzleHttp\Client(); $response = $client->request('GET', 'https://api.github.com/repos/guzzle/guzzle'); echo $response->getStatusCode(); # 200 echo $response->getHeaderLine('content-type'); # 'application/json; charset=utf8' echo $response->getBody(); # '{"id": 1420053, "name": "guzzle", ...}' # Send an asynchronous request. $request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org'); $promise = $client->sendAsync($request)->then(function ($response) { echo 'I completed! ' . $response->getBody(); }); $promise->wait(); ``` ## Help and docs - [Documentation](http://guzzlephp.org/) - [Stack Overflow](http://stackoverflow.com/questions/tagged/guzzle) - [Gitter](https://gitter.im/guzzle/guzzle) ## Installing Guzzle The recommended way to install Guzzle is through [Composer](http://getcomposer.org). ```bash # Install Composer curl -sS https://getcomposer.org/installer | php ``` Next, run the Composer command to install the latest stable version of Guzzle: ```bash composer require guzzlehttp/guzzle ``` After installing, you need to require Composer's autoloader: ```php require 'vendor/autoload.php'; ``` You can then later update Guzzle using composer: ```bash composer update ``` ## Version Guidance | Version | Status | Packagist | Namespace | Repo | Docs | PSR-7 | PHP Version | |---------|------------|---------------------|--------------|---------------------|---------------------|-------|-------------| | 3.x | EOL | `guzzle/guzzle` | `Guzzle` | [v3][guzzle-3-repo] | [v3][guzzle-3-docs] | No | >= 5.3.3 | | 4.x | EOL | `guzzlehttp/guzzle` | `GuzzleHttp` | [v4][guzzle-4-repo] | N/A | No | >= 5.4 | | 5.x | EOL | `guzzlehttp/guzzle` | `GuzzleHttp` | [v5][guzzle-5-repo] | [v5][guzzle-5-docs] | No | >= 5.4 | | 6.x | Latest | `guzzlehttp/guzzle` | `GuzzleHttp` | [v6][guzzle-6-repo] | [v6][guzzle-6-docs] | Yes | >= 5.5 | [guzzle-3-repo]: https://github.com/guzzle/guzzle3 [guzzle-4-repo]: https://github.com/guzzle/guzzle/tree/4.x [guzzle-5-repo]: https://github.com/guzzle/guzzle/tree/5.3 [guzzle-6-repo]: https://github.com/guzzle/guzzle [guzzle-3-docs]: http://guzzle3.readthedocs.org [guzzle-5-docs]: http://guzzle.readthedocs.org/en/5.3/ [guzzle-6-docs]: http://guzzle.readthedocs.org/en/latest/ <?php // autoload_files.php @generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( '7b11c4dc42b3b3023073cb14e519683c' => $vendorDir . '/ralouphie/getallheaders/src/getallheaders.php', 'a0edc8309cc5e1d60e3047b5df6b7052' => $vendorDir . '/guzzlehttp/psr7/src/functions_include.php', 'c964ee0ededf28c96ebd9db5099ef910' => $vendorDir . '/guzzlehttp/promises/src/functions_include.php', '37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php', 'decc78cc4436b1292c6c0d151b19445c' => $vendorDir . '/phpseclib/phpseclib/phpseclib/bootstrap.php', ); <?php /* * This file is part of Composer. * * (c) Nils Adermann <naderman@naderman.de> * Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Composer\Autoload; /** * ClassLoader implements a PSR-0, PSR-4 and classmap class loader. * * $loader = new \Composer\Autoload\ClassLoader(); * * // register classes with namespaces * $loader->add('Symfony\Component', __DIR__.'/component'); * $loader->add('Symfony', __DIR__.'/framework'); * * // activate the autoloader * $loader->register(); * * // to enable searching the include path (eg. for PEAR packages) * $loader->setUseIncludePath(true); * * In this example, if you try to use a class in the Symfony\Component * namespace or one of its children (Symfony\Component\Console for instance), * the autoloader will first look for the class under the component/ * directory, and it will then fallback to the framework/ directory if not * found before giving up. * * This class is loosely based on the Symfony UniversalClassLoader. * * @author Fabien Potencier <fabien@symfony.com> * @author Jordi Boggiano <j.boggiano@seld.be> * @see http://www.php-fig.org/psr/psr-0/ * @see http://www.php-fig.org/psr/psr-4/ */ class ClassLoader { // PSR-4 private $prefixLengthsPsr4 = array(); private $prefixDirsPsr4 = array(); private $fallbackDirsPsr4 = array(); // PSR-0 private $prefixesPsr0 = array(); private $fallbackDirsPsr0 = array(); private $useIncludePath = false; private $classMap = array(); private $classMapAuthoritative = false; private $missingClasses = array(); private $apcuPrefix; public function getPrefixes() { if (!empty($this->prefixesPsr0)) { return call_user_func_array('array_merge', $this->prefixesPsr0); } return array(); } public function getPrefixesPsr4() { return $this->prefixDirsPsr4; } public function getFallbackDirs() { return $this->fallbackDirsPsr0; } public function getFallbackDirsPsr4() { return $this->fallbackDirsPsr4; } public function getClassMap() { return $this->classMap; } /** * @param array $classMap Class to filename map */ public function addClassMap(array $classMap) { if ($this->classMap) { $this->classMap = array_merge($this->classMap, $classMap); } else { $this->classMap = $classMap; } } /** * Registers a set of PSR-0 directories for a given prefix, either * appending or prepending to the ones previously set for this prefix. * * @param string $prefix The prefix * @param array|string $paths The PSR-0 root directories * @param bool $prepend Whether to prepend the directories */ public function add($prefix, $paths, $prepend = false) { if (!$prefix) { if ($prepend) { $this->fallbackDirsPsr0 = array_merge( (array) $paths, $this->fallbackDirsPsr0 ); } else { $this->fallbackDirsPsr0 = array_merge( $this->fallbackDirsPsr0, (array) $paths ); } return; } $first = $prefix[0]; if (!isset($this->prefixesPsr0[$first][$prefix])) { $this->prefixesPsr0[$first][$prefix] = (array) $paths; return; } if ($prepend) { $this->prefixesPsr0[$first][$prefix] = array_merge( (array) $paths, $this->prefixesPsr0[$first][$prefix] ); } else { $this->prefixesPsr0[$first][$prefix] = array_merge( $this->prefixesPsr0[$first][$prefix], (array) $paths ); } } /** * Registers a set of PSR-4 directories for a given namespace, either * appending or prepending to the ones previously set for this namespace. * * @param string $prefix The prefix/namespace, with trailing '\\' * @param array|string $paths The PSR-4 base directories * @param bool $prepend Whether to prepend the directories * * @throws \InvalidArgumentException */ public function addPsr4($prefix, $paths, $prepend = false) { if (!$prefix) { // Register directories for the root namespace. if ($prepend) { $this->fallbackDirsPsr4 = array_merge( (array) $paths, $this->fallbackDirsPsr4 ); } else { $this->fallbackDirsPsr4 = array_merge( $this->fallbackDirsPsr4, (array) $paths ); } } elseif (!isset($this->prefixDirsPsr4[$prefix])) { // Register directories for a new namespace. $length = strlen($prefix); if ('\\' !== $prefix[$length - 1]) { throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); } $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; $this->prefixDirsPsr4[$prefix] = (array) $paths; } elseif ($prepend) { // Prepend directories for an already registered namespace. $this->prefixDirsPsr4[$prefix] = array_merge( (array) $paths, $this->prefixDirsPsr4[$prefix] ); } else { // Append directories for an already registered namespace. $this->prefixDirsPsr4[$prefix] = array_merge( $this->prefixDirsPsr4[$prefix], (array) $paths ); } } /** * Registers a set of PSR-0 directories for a given prefix, * replacing any others previously set for this prefix. * * @param string $prefix The prefix * @param array|string $paths The PSR-0 base directories */ public function set($prefix, $paths) { if (!$prefix) { $this->fallbackDirsPsr0 = (array) $paths; } else { $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; } } /** * Registers a set of PSR-4 directories for a given namespace, * replacing any others previously set for this namespace. * * @param string $prefix The prefix/namespace, with trailing '\\' * @param array|string $paths The PSR-4 base directories * * @throws \InvalidArgumentException */ public function setPsr4($prefix, $paths) { if (!$prefix) { $this->fallbackDirsPsr4 = (array) $paths; } else { $length = strlen($prefix); if ('\\' !== $prefix[$length - 1]) { throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); } $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; $this->prefixDirsPsr4[$prefix] = (array) $paths; } } /** * Turns on searching the include path for class files. * * @param bool $useIncludePath */ public function setUseIncludePath($useIncludePath) { $this->useIncludePath = $useIncludePath; } /** * Can be used to check if the autoloader uses the include path to check * for classes. * * @return bool */ public function getUseIncludePath() { return $this->useIncludePath; } /** * Turns off searching the prefix and fallback directories for classes * that have not been registered with the class map. * * @param bool $classMapAuthoritative */ public function setClassMapAuthoritative($classMapAuthoritative) { $this->classMapAuthoritative = $classMapAuthoritative; } /** * Should class lookup fail if not found in the current class map? * * @return bool */ public function isClassMapAuthoritative() { return $this->classMapAuthoritative; } /** * APCu prefix to use to cache found/not-found classes, if the extension is enabled. * * @param string|null $apcuPrefix */ public function setApcuPrefix($apcuPrefix) { $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; } /** * The APCu prefix in use, or null if APCu caching is not enabled. * * @return string|null */ public function getApcuPrefix() { return $this->apcuPrefix; } /** * Registers this instance as an autoloader. * * @param bool $prepend Whether to prepend the autoloader or not */ public function register($prepend = false) { spl_autoload_register(array($this, 'loadClass'), true, $prepend); } /** * Unregisters this instance as an autoloader. */ public function unregister() { spl_autoload_unregister(array($this, 'loadClass')); } /** * Loads the given class or interface. * * @param string $class The name of the class * @return bool|null True if loaded, null otherwise */ public function loadClass($class) { if ($file = $this->findFile($class)) { includeFile($file); return true; } } /** * Finds the path to the file where the class is defined. * * @param string $class The name of the class * * @return string|false The path if found, false otherwise */ public function findFile($class) { // class map lookup if (isset($this->classMap[$class])) { return $this->classMap[$class]; } if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { return false; } if (null !== $this->apcuPrefix) { $file = apcu_fetch($this->apcuPrefix.$class, $hit); if ($hit) { return $file; } } $file = $this->findFileWithExtension($class, '.php'); // Search for Hack files if we are running on HHVM if (false === $file && defined('HHVM_VERSION')) { $file = $this->findFileWithExtension($class, '.hh'); } if (null !== $this->apcuPrefix) { apcu_add($this->apcuPrefix.$class, $file); } if (false === $file) { // Remember that this class does not exist. $this->missingClasses[$class] = true; } return $file; } private function findFileWithExtension($class, $ext) { // PSR-4 lookup $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; $first = $class[0]; if (isset($this->prefixLengthsPsr4[$first])) { $subPath = $class; while (false !== $lastPos = strrpos($subPath, '\\')) { $subPath = substr($subPath, 0, $lastPos); $search = $subPath . '\\'; if (isset($this->prefixDirsPsr4[$search])) { $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); foreach ($this->prefixDirsPsr4[$search] as $dir) { if (file_exists($file = $dir . $pathEnd)) { return $file; } } } } } // PSR-4 fallback dirs foreach ($this->fallbackDirsPsr4 as $dir) { if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { return $file; } } // PSR-0 lookup if (false !== $pos = strrpos($class, '\\')) { // namespaced class name $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); } else { // PEAR-like class name $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; } if (isset($this->prefixesPsr0[$first])) { foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { if (0 === strpos($class, $prefix)) { foreach ($dirs as $dir) { if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { return $file; } } } } } // PSR-0 fallback dirs foreach ($this->fallbackDirsPsr0 as $dir) { if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { return $file; } } // PSR-0 include paths. if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { return $file; } return false; } } /** * Scope isolated include. * * Prevents access to $this/self from included files. */ function includeFile($file) { include $file; } <?php // autoload_real.php @generated by Composer class ComposerAutoloaderInitc53c06361dfa4f223f81ea4ac493255a { private static $loader; public static function loadClassLoader($class) { if ('Composer\Autoload\ClassLoader' === $class) { require __DIR__ . '/ClassLoader.php'; } } public static function getLoader() { if (null !== self::$loader) { return self::$loader; } spl_autoload_register(array('ComposerAutoloaderInitc53c06361dfa4f223f81ea4ac493255a', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(); spl_autoload_unregister(array('ComposerAutoloaderInitc53c06361dfa4f223f81ea4ac493255a', 'loadClassLoader')); $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); if ($useStaticLoader) { require_once __DIR__ . '/autoload_static.php'; call_user_func(\Composer\Autoload\ComposerStaticInitc53c06361dfa4f223f81ea4ac493255a::getInitializer($loader)); } else { $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { $loader->set($namespace, $path); } $map = require __DIR__ . '/autoload_psr4.php'; foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); } $classMap = require __DIR__ . '/autoload_classmap.php'; if ($classMap) { $loader->addClassMap($classMap); } } $loader->register(true); if ($useStaticLoader) { $includeFiles = Composer\Autoload\ComposerStaticInitc53c06361dfa4f223f81ea4ac493255a::$files; } else { $includeFiles = require __DIR__ . '/autoload_files.php'; } foreach ($includeFiles as $fileIdentifier => $file) { composerRequirec53c06361dfa4f223f81ea4ac493255a($fileIdentifier, $file); } return $loader; } } function composerRequirec53c06361dfa4f223f81ea4ac493255a($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file; $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; } } <?php // autoload_namespaces.php @generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( 'Google_Service_' => array($vendorDir . '/google/apiclient-services/src'), 'Google_' => array($vendorDir . '/google/apiclient/src'), ); [ { "name": "firebase/php-jwt", "version": "v5.0.0", "version_normalized": "5.0.0.0", "source": { "type": "git", "url": "https://github.com/firebase/php-jwt.git", "reference": "9984a4d3a32ae7673d6971ea00bae9d0a1abba0e" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/firebase/php-jwt/zipball/9984a4d3a32ae7673d6971ea00bae9d0a1abba0e", "reference": "9984a4d3a32ae7673d6971ea00bae9d0a1abba0e", "shasum": "" }, "require": { "php": ">=5.3.0" }, "require-dev": { "phpunit/phpunit": " 4.8.35" }, "time": "2017-06-27T22:17:23+00:00", "type": "library", "installation-source": "dist", "autoload": { "psr-4": { "Firebase\\JWT\\": "src" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "BSD-3-Clause" ], "authors": [ { "name": "Neuman Vong", "email": "neuman+pear@twilio.com", "role": "Developer" }, { "name": "Anant Narayanan", "email": "anant@php.net", "role": "Developer" } ], "description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.", "homepage": "https://github.com/firebase/php-jwt" }, { "name": "google/apiclient", "version": "v2.4.0", "version_normalized": "2.4.0.0", "source": { "type": "git", "url": "https://github.com/googleapis/google-api-php-client.git", "reference": "cd3c37998020d91ae4eafca4f26a92da4dabba83" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/googleapis/google-api-php-client/zipball/cd3c37998020d91ae4eafca4f26a92da4dabba83", "reference": "cd3c37998020d91ae4eafca4f26a92da4dabba83", "shasum": "" }, "require": { "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", "google/apiclient-services": "~0.13", "google/auth": "^1.0", "guzzlehttp/guzzle": "~5.3.1||~6.0", "guzzlehttp/psr7": "^1.2", "monolog/monolog": "^1.17|^2.0", "php": ">=5.4", "phpseclib/phpseclib": "~0.3.10||~2.0" }, "require-dev": { "cache/filesystem-adapter": "^0.3.2", "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", "phpcompatibility/php-compatibility": "^9.2", "phpunit/phpunit": "~4.8.36", "squizlabs/php_codesniffer": "~2.3", "symfony/css-selector": "~2.1", "symfony/dom-crawler": "~2.1" }, "suggest": { "cache/filesystem-adapter": "For caching certs and tokens (using Google_Client::setCache)" }, "time": "2019-09-11T17:38:10+00:00", "type": "library", "extra": { "branch-alias": { "dev-master": "2.x-dev" } }, "installation-source": "dist", "autoload": { "psr-0": { "Google_": "src/" }, "classmap": [ "src/Google/Service/" ] }, "notification-url": "https://packagist.org/downloads/", "license": [ "Apache-2.0" ], "description": "Client library for Google APIs", "homepage": "http://developers.google.com/api-client-library/php", "keywords": [ "google" ] }, { "name": "google/apiclient-services", "version": "v0.121", "version_normalized": "0.121.0.0", "source": { "type": "git", "url": "https://github.com/googleapis/google-api-php-client-services.git", "reference": "a33fd9ed19fe4e27f2ccebbf45646f38e7cb95af" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/googleapis/google-api-php-client-services/zipball/a33fd9ed19fe4e27f2ccebbf45646f38e7cb95af", "reference": "a33fd9ed19fe4e27f2ccebbf45646f38e7cb95af", "shasum": "" }, "require": { "php": ">=5.4" }, "require-dev": { "phpunit/phpunit": "~4.8" }, "time": "2019-11-03T00:23:34+00:00", "type": "library", "installation-source": "dist", "autoload": { "psr-0": { "Google_Service_": "src" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "Apache-2.0" ], "description": "Client library for Google APIs", "homepage": "http://developers.google.com/api-client-library/php", "keywords": [ "google" ] }, { "name": "google/auth", "version": "v1.6.1", "version_normalized": "1.6.1.0", "source": { "type": "git", "url": "https://github.com/googleapis/google-auth-library-php.git", "reference": "45635ac69d0b95f38885531d4ebcdfcb2ebb6f36" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/googleapis/google-auth-library-php/zipball/45635ac69d0b95f38885531d4ebcdfcb2ebb6f36", "reference": "45635ac69d0b95f38885531d4ebcdfcb2ebb6f36", "shasum": "" }, "require": { "firebase/php-jwt": "~2.0|~3.0|~4.0|~5.0", "guzzlehttp/guzzle": "~5.3.1|~6.0", "guzzlehttp/psr7": "^1.2", "php": ">=5.4", "psr/cache": "^1.0", "psr/http-message": "^1.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^1.11", "guzzlehttp/promises": "0.1.1|^1.3", "phpseclib/phpseclib": "^2", "phpunit/phpunit": "^4.8.36|^5.7", "sebastian/comparator": ">=1.2.3" }, "suggest": { "phpseclib/phpseclib": "May be used in place of OpenSSL for signing strings or for token management. Please require version ^2." }, "time": "2019-10-29T20:13:04+00:00", "type": "library", "installation-source": "dist", "autoload": { "psr-4": { "Google\\Auth\\": "src" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "Apache-2.0" ], "description": "Google Auth Library for PHP", "homepage": "http://github.com/google/google-auth-library-php", "keywords": [ "Authentication", "google", "oauth2" ] }, { "name": "guzzlehttp/guzzle", "version": "6.5.0", "version_normalized": "6.5.0.0", "source": { "type": "git", "url": "https://github.com/guzzle/guzzle.git", "reference": "dbc2bc3a293ed6b1ae08a3651e2bfd213d19b6a5" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/guzzle/guzzle/zipball/dbc2bc3a293ed6b1ae08a3651e2bfd213d19b6a5", "reference": "dbc2bc3a293ed6b1ae08a3651e2bfd213d19b6a5", "shasum": "" }, "require": { "ext-json": "*", "guzzlehttp/promises": "^1.0", "guzzlehttp/psr7": "^1.6.1", "php": ">=5.5" }, "require-dev": { "ext-curl": "*", "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0", "psr/log": "^1.1" }, "suggest": { "ext-intl": "Required for Internationalized Domain Name (IDN) support", "psr/log": "Required for using the Log middleware" }, "time": "2019-12-07T18:20:45+00:00", "type": "library", "extra": { "branch-alias": { "dev-master": "6.5-dev" } }, "installation-source": "dist", "autoload": { "psr-4": { "GuzzleHttp\\": "src/" }, "files": [ "src/functions_include.php" ] }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" } ], "description": "Guzzle is a PHP HTTP client library", "homepage": "http://guzzlephp.org/", "keywords": [ "client", "curl", "framework", "http", "http client", "rest", "web service" ] }, { "name": "guzzlehttp/promises", "version": "v1.3.1", "version_normalized": "1.3.1.0", "source": { "type": "git", "url": "https://github.com/guzzle/promises.git", "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", "shasum": "" }, "require": { "php": ">=5.5.0" }, "require-dev": { "phpunit/phpunit": "^4.0" }, "time": "2016-12-20T10:07:11+00:00", "type": "library", "extra": { "branch-alias": { "dev-master": "1.4-dev" } }, "installation-source": "dist", "autoload": { "psr-4": { "GuzzleHttp\\Promise\\": "src/" }, "files": [ "src/functions_include.php" ] }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" } ], "description": "Guzzle promises library", "keywords": [ "promise" ] }, { "name": "guzzlehttp/psr7", "version": "1.6.1", "version_normalized": "1.6.1.0", "source": { "type": "git", "url": "https://github.com/guzzle/psr7.git", "reference": "239400de7a173fe9901b9ac7c06497751f00727a" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/guzzle/psr7/zipball/239400de7a173fe9901b9ac7c06497751f00727a", "reference": "239400de7a173fe9901b9ac7c06497751f00727a", "shasum": "" }, "require": { "php": ">=5.4.0", "psr/http-message": "~1.0", "ralouphie/getallheaders": "^2.0.5 || ^3.0.0" }, "provide": { "psr/http-message-implementation": "1.0" }, "require-dev": { "ext-zlib": "*", "phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.8" }, "suggest": { "zendframework/zend-httphandlerrunner": "Emit PSR-7 responses" }, "time": "2019-07-01T23:21:34+00:00", "type": "library", "extra": { "branch-alias": { "dev-master": "1.6-dev" } }, "installation-source": "dist", "autoload": { "psr-4": { "GuzzleHttp\\Psr7\\": "src/" }, "files": [ "src/functions_include.php" ] }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" }, { "name": "Tobias Schultze", "homepage": "https://github.com/Tobion" } ], "description": "PSR-7 message implementation that also provides common utility methods", "keywords": [ "http", "message", "psr-7", "request", "response", "stream", "uri", "url" ] }, { "name": "monolog/monolog", "version": "1.25.2", "version_normalized": "1.25.2.0", "source": { "type": "git", "url": "https://github.com/Seldaek/monolog.git", "reference": "d5e2fb341cb44f7e2ab639d12a1e5901091ec287" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/Seldaek/monolog/zipball/d5e2fb341cb44f7e2ab639d12a1e5901091ec287", "reference": "d5e2fb341cb44f7e2ab639d12a1e5901091ec287", "shasum": "" }, "require": { "php": ">=5.3.0", "psr/log": "~1.0" }, "provide": { "psr/log-implementation": "1.0.0" }, "require-dev": { "aws/aws-sdk-php": "^2.4.9 || ^3.0", "doctrine/couchdb": "~1.0@dev", "graylog2/gelf-php": "~1.0", "jakub-onderka/php-parallel-lint": "0.9", "php-amqplib/php-amqplib": "~2.4", "php-console/php-console": "^3.1.3", "phpunit/phpunit": "~4.5", "phpunit/phpunit-mock-objects": "2.3.0", "ruflin/elastica": ">=0.90 <3.0", "sentry/sentry": "^0.13", "swiftmailer/swiftmailer": "^5.3|^6.0" }, "suggest": { "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", "doctrine/couchdb": "Allow sending log messages to a CouchDB server", "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", "ext-mongo": "Allow sending log messages to a MongoDB server", "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", "mongodb/mongodb": "Allow sending log messages to a MongoDB server via PHP Driver", "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", "php-console/php-console": "Allow sending log messages to Google Chrome", "rollbar/rollbar": "Allow sending log messages to Rollbar", "ruflin/elastica": "Allow sending log messages to an Elastic Search server", "sentry/sentry": "Allow sending log messages to a Sentry server" }, "time": "2019-11-13T10:00:05+00:00", "type": "library", "extra": { "branch-alias": { "dev-master": "2.0.x-dev" } }, "installation-source": "dist", "autoload": { "psr-4": { "Monolog\\": "src/Monolog" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "Jordi Boggiano", "email": "j.boggiano@seld.be", "homepage": "http://seld.be" } ], "description": "Sends your logs to files, sockets, inboxes, databases and various web services", "homepage": "http://github.com/Seldaek/monolog", "keywords": [ "log", "logging", "psr-3" ] }, { "name": "phpseclib/phpseclib", "version": "2.0.23", "version_normalized": "2.0.23.0", "source": { "type": "git", "url": "https://github.com/phpseclib/phpseclib.git", "reference": "c78eb5058d5bb1a183133c36d4ba5b6675dfa099" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/c78eb5058d5bb1a183133c36d4ba5b6675dfa099", "reference": "c78eb5058d5bb1a183133c36d4ba5b6675dfa099", "shasum": "" }, "require": { "php": ">=5.3.3" }, "require-dev": { "phing/phing": "~2.7", "phpunit/phpunit": "^4.8.35|^5.7|^6.0", "sami/sami": "~2.0", "squizlabs/php_codesniffer": "~2.0" }, "suggest": { "ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.", "ext-libsodium": "SSH2/SFTP can make use of some algorithms provided by the libsodium-php extension.", "ext-mcrypt": "Install the Mcrypt extension in order to speed up a few other cryptographic operations.", "ext-openssl": "Install the OpenSSL extension in order to speed up a wide variety of cryptographic operations." }, "time": "2019-09-17T03:41:22+00:00", "type": "library", "installation-source": "dist", "autoload": { "files": [ "phpseclib/bootstrap.php" ], "psr-4": { "phpseclib\\": "phpseclib/" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "Jim Wigginton", "email": "terrafrost@php.net", "role": "Lead Developer" }, { "name": "Patrick Monnerat", "email": "pm@datasphere.ch", "role": "Developer" }, { "name": "Andreas Fischer", "email": "bantu@phpbb.com", "role": "Developer" }, { "name": "Hans-Jürgen Petrich", "email": "petrich@tronic-media.com", "role": "Developer" }, { "name": "Graham Campbell", "email": "graham@alt-three.com", "role": "Developer" } ], "description": "PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.", "homepage": "http://phpseclib.sourceforge.net", "keywords": [ "BigInteger", "aes", "asn.1", "asn1", "blowfish", "crypto", "cryptography", "encryption", "rsa", "security", "sftp", "signature", "signing", "ssh", "twofish", "x.509", "x509" ] }, { "name": "psr/cache", "version": "1.0.1", "version_normalized": "1.0.1.0", "source": { "type": "git", "url": "https://github.com/php-fig/cache.git", "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/php-fig/cache/zipball/d11b50ad223250cf17b86e38383413f5a6764bf8", "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8", "shasum": "" }, "require": { "php": ">=5.3.0" }, "time": "2016-08-06T20:24:11+00:00", "type": "library", "extra": { "branch-alias": { "dev-master": "1.0.x-dev" } }, "installation-source": "dist", "autoload": { "psr-4": { "Psr\\Cache\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "PHP-FIG", "homepage": "http://www.php-fig.org/" } ], "description": "Common interface for caching libraries", "keywords": [ "cache", "psr", "psr-6" ] }, { "name": "psr/http-message", "version": "1.0.1", "version_normalized": "1.0.1.0", "source": { "type": "git", "url": "https://github.com/php-fig/http-message.git", "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", "shasum": "" }, "require": { "php": ">=5.3.0" }, "time": "2016-08-06T14:39:51+00:00", "type": "library", "extra": { "branch-alias": { "dev-master": "1.0.x-dev" } }, "installation-source": "dist", "autoload": { "psr-4": { "Psr\\Http\\Message\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "PHP-FIG", "homepage": "http://www.php-fig.org/" } ], "description": "Common interface for HTTP messages", "homepage": "https://github.com/php-fig/http-message", "keywords": [ "http", "http-message", "psr", "psr-7", "request", "response" ] }, { "name": "psr/log", "version": "1.1.2", "version_normalized": "1.1.2.0", "source": { "type": "git", "url": "https://github.com/php-fig/log.git", "reference": "446d54b4cb6bf489fc9d75f55843658e6f25d801" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/php-fig/log/zipball/446d54b4cb6bf489fc9d75f55843658e6f25d801", "reference": "446d54b4cb6bf489fc9d75f55843658e6f25d801", "shasum": "" }, "require": { "php": ">=5.3.0" }, "time": "2019-11-01T11:05:21+00:00", "type": "library", "extra": { "branch-alias": { "dev-master": "1.1.x-dev" } }, "installation-source": "dist", "autoload": { "psr-4": { "Psr\\Log\\": "Psr/Log/" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "PHP-FIG", "homepage": "http://www.php-fig.org/" } ], "description": "Common interface for logging libraries", "homepage": "https://github.com/php-fig/log", "keywords": [ "log", "psr", "psr-3" ] }, { "name": "ralouphie/getallheaders", "version": "3.0.3", "version_normalized": "3.0.3.0", "source": { "type": "git", "url": "https://github.com/ralouphie/getallheaders.git", "reference": "120b605dfeb996808c31b6477290a714d356e822" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822", "reference": "120b605dfeb996808c31b6477290a714d356e822", "shasum": "" }, "require": { "php": ">=5.6" }, "require-dev": { "php-coveralls/php-coveralls": "^2.1", "phpunit/phpunit": "^5 || ^6.5" }, "time": "2019-03-08T08:55:37+00:00", "type": "library", "installation-source": "dist", "autoload": { "files": [ "src/getallheaders.php" ] }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "Ralph Khattar", "email": "ralph.khattar@gmail.com" } ], "description": "A polyfill for getallheaders." } ] <?php // autoload_classmap.php @generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( 'Google_Service_Exception' => $vendorDir . '/google/apiclient/src/Google/Service/Exception.php', 'Google_Service_Resource' => $vendorDir . '/google/apiclient/src/Google/Service/Resource.php', ); <?php // autoload_psr4.php @generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( 'phpseclib\\' => array($vendorDir . '/phpseclib/phpseclib/phpseclib'), 'Psr\\Log\\' => array($vendorDir . '/psr/log/Psr/Log'), 'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src'), 'Psr\\Cache\\' => array($vendorDir . '/psr/cache/src'), 'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'), 'GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'), 'GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'), 'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'), 'Google\\Auth\\' => array($vendorDir . '/google/auth/src'), 'Firebase\\JWT\\' => array($vendorDir . '/firebase/php-jwt/src'), ); <?php // autoload_static.php @generated by Composer namespace Composer\Autoload; class ComposerStaticInitc53c06361dfa4f223f81ea4ac493255a { public static $files = array ( '7b11c4dc42b3b3023073cb14e519683c' => __DIR__ . '/..' . '/ralouphie/getallheaders/src/getallheaders.php', 'a0edc8309cc5e1d60e3047b5df6b7052' => __DIR__ . '/..' . '/guzzlehttp/psr7/src/functions_include.php', 'c964ee0ededf28c96ebd9db5099ef910' => __DIR__ . '/..' . '/guzzlehttp/promises/src/functions_include.php', '37a3dc5111fe8f707ab4c132ef1dbc62' => __DIR__ . '/..' . '/guzzlehttp/guzzle/src/functions_include.php', 'decc78cc4436b1292c6c0d151b19445c' => __DIR__ . '/..' . '/phpseclib/phpseclib/phpseclib/bootstrap.php', ); public static $prefixLengthsPsr4 = array ( 'p' => array ( 'phpseclib\\' => 10, ), 'P' => array ( 'Psr\\Log\\' => 8, 'Psr\\Http\\Message\\' => 17, 'Psr\\Cache\\' => 10, ), 'M' => array ( 'Monolog\\' => 8, ), 'G' => array ( 'GuzzleHttp\\Psr7\\' => 16, 'GuzzleHttp\\Promise\\' => 19, 'GuzzleHttp\\' => 11, 'Google\\Auth\\' => 12, ), 'F' => array ( 'Firebase\\JWT\\' => 13, ), ); public static $prefixDirsPsr4 = array ( 'phpseclib\\' => array ( 0 => __DIR__ . '/..' . '/phpseclib/phpseclib/phpseclib', ), 'Psr\\Log\\' => array ( 0 => __DIR__ . '/..' . '/psr/log/Psr/Log', ), 'Psr\\Http\\Message\\' => array ( 0 => __DIR__ . '/..' . '/psr/http-message/src', ), 'Psr\\Cache\\' => array ( 0 => __DIR__ . '/..' . '/psr/cache/src', ), 'Monolog\\' => array ( 0 => __DIR__ . '/..' . '/monolog/monolog/src/Monolog', ), 'GuzzleHttp\\Psr7\\' => array ( 0 => __DIR__ . '/..' . '/guzzlehttp/psr7/src', ), 'GuzzleHttp\\Promise\\' => array ( 0 => __DIR__ . '/..' . '/guzzlehttp/promises/src', ), 'GuzzleHttp\\' => array ( 0 => __DIR__ . '/..' . '/guzzlehttp/guzzle/src', ), 'Google\\Auth\\' => array ( 0 => __DIR__ . '/..' . '/google/auth/src', ), 'Firebase\\JWT\\' => array ( 0 => __DIR__ . '/..' . '/firebase/php-jwt/src', ), ); public static $prefixesPsr0 = array ( 'G' => array ( 'Google_Service_' => array ( 0 => __DIR__ . '/..' . '/google/apiclient-services/src', ), 'Google_' => array ( 0 => __DIR__ . '/..' . '/google/apiclient/src', ), ), ); public static $classMap = array ( 'Google_Service_Exception' => __DIR__ . '/..' . '/google/apiclient/src/Google/Service/Exception.php', 'Google_Service_Resource' => __DIR__ . '/..' . '/google/apiclient/src/Google/Service/Resource.php', ); public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { $loader->prefixLengthsPsr4 = ComposerStaticInitc53c06361dfa4f223f81ea4ac493255a::$prefixLengthsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInitc53c06361dfa4f223f81ea4ac493255a::$prefixDirsPsr4; $loader->prefixesPsr0 = ComposerStaticInitc53c06361dfa4f223f81ea4ac493255a::$prefixesPsr0; $loader->classMap = ComposerStaticInitc53c06361dfa4f223f81ea4ac493255a::$classMap; }, null, ClassLoader::class); } } <?php /* * Copyright 2010 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ class Google_Service { public $batchPath; public $rootUrl; public $version; public $servicePath; public $availableScopes; public $resource; private $client; public function __construct(Google_Client $client) { $this->client = $client; } /** * Return the associated Google_Client class. * @return Google_Client */ public function getClient() { return $this->client; } /** * Create a new HTTP Batch handler for this service * * @return Google_Http_Batch */ public function createBatch() { return new Google_Http_Batch( $this->client, false, $this->rootUrl, $this->batchPath ); } } <?php if (!class_exists('Google_Client')) { require_once __DIR__ . '/autoload.php'; } /** * Extension to the regular Google_Model that automatically * exposes the items array for iteration, so you can just * iterate over the object rather than a reference inside. */ class Google_Collection extends Google_Model implements Iterator, Countable { protected $collection_key = 'items'; public function rewind() { if (isset($this->{$this->collection_key}) && is_array($this->{$this->collection_key})) { reset($this->{$this->collection_key}); } } public function current() { $this->coerceType($this->key()); if (is_array($this->{$this->collection_key})) { return current($this->{$this->collection_key}); } } public function key() { if (isset($this->{$this->collection_key}) && is_array($this->{$this->collection_key})) { return key($this->{$this->collection_key}); } } public function next() { return next($this->{$this->collection_key}); } public function valid() { $key = $this->key(); return $key !== null && $key !== false; } public function count() { if (!isset($this->{$this->collection_key})) { return 0; } return count($this->{$this->collection_key}); } public function offsetExists($offset) { if (!is_numeric($offset)) { return parent::offsetExists($offset); } return isset($this->{$this->collection_key}[$offset]); } public function offsetGet($offset) { if (!is_numeric($offset)) { return parent::offsetGet($offset); } $this->coerceType($offset); return $this->{$this->collection_key}[$offset]; } public function offsetSet($offset, $value) { if (!is_numeric($offset)) { return parent::offsetSet($offset, $value); } $this->{$this->collection_key}[$offset] = $value; } public function offsetUnset($offset) { if (!is_numeric($offset)) { return parent::offsetUnset($offset); } unset($this->{$this->collection_key}[$offset]); } private function coerceType($offset) { $keyType = $this->keyType($this->collection_key); if ($keyType && !is_object($this->{$this->collection_key}[$offset])) { $this->{$this->collection_key}[$offset] = new $keyType($this->{$this->collection_key}[$offset]); } } } <?php /* * Copyright 2011 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * This class defines attributes, valid values, and usage which is generated * from a given json schema. * http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5 * */ class Google_Model implements ArrayAccess { /** * If you need to specify a NULL JSON value, use Google_Model::NULL_VALUE * instead - it will be replaced when converting to JSON with a real null. */ const NULL_VALUE = "{}gapi-php-null"; protected $internal_gapi_mappings = array(); protected $modelData = array(); protected $processed = array(); /** * Polymorphic - accepts a variable number of arguments dependent * on the type of the model subclass. */ final public function __construct() { if (func_num_args() == 1 && is_array(func_get_arg(0))) { // Initialize the model with the array's contents. $array = func_get_arg(0); $this->mapTypes($array); } $this->gapiInit(); } /** * Getter that handles passthrough access to the data array, and lazy object creation. * @param string $key Property name. * @return mixed The value if any, or null. */ public function __get($key) { $keyType = $this->keyType($key); $keyDataType = $this->dataType($key); if ($keyType && !isset($this->processed[$key])) { if (isset($this->modelData[$key])) { $val = $this->modelData[$key]; } elseif ($keyDataType == 'array' || $keyDataType == 'map') { $val = array(); } else { $val = null; } if ($this->isAssociativeArray($val)) { if ($keyDataType && 'map' == $keyDataType) { foreach ($val as $arrayKey => $arrayItem) { $this->modelData[$key][$arrayKey] = new $keyType($arrayItem); } } else { $this->modelData[$key] = new $keyType($val); } } else if (is_array($val)) { $arrayObject = array(); foreach ($val as $arrayIndex => $arrayItem) { $arrayObject[$arrayIndex] = new $keyType($arrayItem); } $this->modelData[$key] = $arrayObject; } $this->processed[$key] = true; } return isset($this->modelData[$key]) ? $this->modelData[$key] : null; } /** * Initialize this object's properties from an array. * * @param array $array Used to seed this object's properties. * @return void */ protected function mapTypes($array) { // Hard initialise simple types, lazy load more complex ones. foreach ($array as $key => $val) { if ($keyType = $this->keyType($key)) { $dataType = $this->dataType($key); if ($dataType == 'array' || $dataType == 'map') { $this->$key = array(); foreach ($val as $itemKey => $itemVal) { if ($itemVal instanceof $keyType) { $this->{$key}[$itemKey] = $itemVal; } else { $this->{$key}[$itemKey] = new $keyType($itemVal); } } } elseif ($val instanceof $keyType) { $this->$key = $val; } else { $this->$key = new $keyType($val); } unset($array[$key]); } elseif (property_exists($this, $key)) { $this->$key = $val; unset($array[$key]); } elseif (property_exists($this, $camelKey = $this->camelCase($key))) { // This checks if property exists as camelCase, leaving it in array as snake_case // in case of backwards compatibility issues. $this->$camelKey = $val; } } $this->modelData = $array; } /** * Blank initialiser to be used in subclasses to do post-construction initialisation - this * avoids the need for subclasses to have to implement the variadics handling in their * constructors. */ protected function gapiInit() { return; } /** * Create a simplified object suitable for straightforward * conversion to JSON. This is relatively expensive * due to the usage of reflection, but shouldn't be called * a whole lot, and is the most straightforward way to filter. */ public function toSimpleObject() { $object = new stdClass(); // Process all other data. foreach ($this->modelData as $key => $val) { $result = $this->getSimpleValue($val); if ($result !== null) { $object->$key = $this->nullPlaceholderCheck($result); } } // Process all public properties. $reflect = new ReflectionObject($this); $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC); foreach ($props as $member) { $name = $member->getName(); $result = $this->getSimpleValue($this->$name); if ($result !== null) { $name = $this->getMappedName($name); $object->$name = $this->nullPlaceholderCheck($result); } } return $object; } /** * Handle different types of values, primarily * other objects and map and array data types. */ private function getSimpleValue($value) { if ($value instanceof Google_Model) { return $value->toSimpleObject(); } else if (is_array($value)) { $return = array(); foreach ($value as $key => $a_value) { $a_value = $this->getSimpleValue($a_value); if ($a_value !== null) { $key = $this->getMappedName($key); $return[$key] = $this->nullPlaceholderCheck($a_value); } } return $return; } return $value; } /** * Check whether the value is the null placeholder and return true null. */ private function nullPlaceholderCheck($value) { if ($value === self::NULL_VALUE) { return null; } return $value; } /** * If there is an internal name mapping, use that. */ private function getMappedName($key) { if (isset($this->internal_gapi_mappings, $this->internal_gapi_mappings[$key])) { $key = $this->internal_gapi_mappings[$key]; } return $key; } /** * Returns true only if the array is associative. * @param array $array * @return bool True if the array is associative. */ protected function isAssociativeArray($array) { if (!is_array($array)) { return false; } $keys = array_keys($array); foreach ($keys as $key) { if (is_string($key)) { return true; } } return false; } /** * Verify if $obj is an array. * @throws Google_Exception Thrown if $obj isn't an array. * @param array $obj Items that should be validated. * @param string $method Method expecting an array as an argument. */ public function assertIsArray($obj, $method) { if ($obj && !is_array($obj)) { throw new Google_Exception( "Incorrect parameter type passed to $method(). Expected an array." ); } } public function offsetExists($offset) { return isset($this->$offset) || isset($this->modelData[$offset]); } public function offsetGet($offset) { return isset($this->$offset) ? $this->$offset : $this->__get($offset); } public function offsetSet($offset, $value) { if (property_exists($this, $offset)) { $this->$offset = $value; } else { $this->modelData[$offset] = $value; $this->processed[$offset] = true; } } public function offsetUnset($offset) { unset($this->modelData[$offset]); } protected function keyType($key) { $keyType = $key . "Type"; // ensure keyType is a valid class if (property_exists($this, $keyType) && class_exists($this->$keyType)) { return $this->$keyType; } } protected function dataType($key) { $dataType = $key . "DataType"; if (property_exists($this, $dataType)) { return $this->$dataType; } } public function __isset($key) { return isset($this->modelData[$key]); } public function __unset($key) { unset($this->modelData[$key]); } /** * Convert a string to camelCase * @param string $value * @return string */ private function camelCase($value) { $value = ucwords(str_replace(array('-', '_'), ' ', $value)); $value = str_replace(' ', '', $value); $value[0] = strtolower($value[0]); return $value; } } <?php use Google\Auth\CredentialsLoader; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\FetchAuthTokenCache; use Google\Auth\Middleware\AuthTokenMiddleware; use Google\Auth\Middleware\ScopedAccessTokenMiddleware; use Google\Auth\Middleware\SimpleMiddleware; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use Psr\Cache\CacheItemPoolInterface; /** * */ class Google_AuthHandler_Guzzle6AuthHandler { protected $cache; protected $cacheConfig; public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = []) { $this->cache = $cache; $this->cacheConfig = $cacheConfig; } public function attachCredentials( ClientInterface $http, CredentialsLoader $credentials, callable $tokenCallback = null ) { // use the provided cache if ($this->cache) { $credentials = new FetchAuthTokenCache( $credentials, $this->cacheConfig, $this->cache ); } // if we end up needing to make an HTTP request to retrieve credentials, we // can use our existing one, but we need to throw exceptions so the error // bubbles up. $authHttp = $this->createAuthHttp($http); $authHttpHandler = HttpHandlerFactory::build($authHttp); $middleware = new AuthTokenMiddleware( $credentials, $authHttpHandler, $tokenCallback ); $config = $http->getConfig(); $config['handler']->remove('google_auth'); $config['handler']->push($middleware, 'google_auth'); $config['auth'] = 'google_auth'; $http = new Client($config); return $http; } public function attachToken(ClientInterface $http, array $token, array $scopes) { $tokenFunc = function ($scopes) use ($token) { return $token['access_token']; }; $middleware = new ScopedAccessTokenMiddleware( $tokenFunc, $scopes, $this->cacheConfig, $this->cache ); $config = $http->getConfig(); $config['handler']->remove('google_auth'); $config['handler']->push($middleware, 'google_auth'); $config['auth'] = 'scoped'; $http = new Client($config); return $http; } public function attachKey(ClientInterface $http, $key) { $middleware = new SimpleMiddleware(['key' => $key]); $config = $http->getConfig(); $config['handler']->remove('google_auth'); $config['handler']->push($middleware, 'google_auth'); $config['auth'] = 'simple'; $http = new Client($config); return $http; } private function createAuthHttp(ClientInterface $http) { return new Client( [ 'base_uri' => $http->getConfig('base_uri'), 'exceptions' => true, 'verify' => $http->getConfig('verify'), 'proxy' => $http->getConfig('proxy'), ] ); } } <?php /** * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; class Google_AuthHandler_AuthHandlerFactory { /** * Builds out a default http handler for the installed version of guzzle. * * @return Google_AuthHandler_Guzzle5AuthHandler|Google_AuthHandler_Guzzle6AuthHandler * @throws Exception */ public static function build($cache = null, array $cacheConfig = []) { $version = ClientInterface::VERSION; switch ($version[0]) { case '5': return new Google_AuthHandler_Guzzle5AuthHandler($cache, $cacheConfig); case '6': return new Google_AuthHandler_Guzzle6AuthHandler($cache, $cacheConfig); default: throw new Exception('Version not supported'); } } } <?php use Google\Auth\CredentialsLoader; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\FetchAuthTokenCache; use Google\Auth\Subscriber\AuthTokenSubscriber; use Google\Auth\Subscriber\ScopedAccessTokenSubscriber; use Google\Auth\Subscriber\SimpleSubscriber; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use Psr\Cache\CacheItemPoolInterface; /** * */ class Google_AuthHandler_Guzzle5AuthHandler { protected $cache; protected $cacheConfig; public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = []) { $this->cache = $cache; $this->cacheConfig = $cacheConfig; } public function attachCredentials( ClientInterface $http, CredentialsLoader $credentials, callable $tokenCallback = null ) { // use the provided cache if ($this->cache) { $credentials = new FetchAuthTokenCache( $credentials, $this->cacheConfig, $this->cache ); } // if we end up needing to make an HTTP request to retrieve credentials, we // can use our existing one, but we need to throw exceptions so the error // bubbles up. $authHttp = $this->createAuthHttp($http); $authHttpHandler = HttpHandlerFactory::build($authHttp); $subscriber = new AuthTokenSubscriber( $credentials, $authHttpHandler, $tokenCallback ); $http->setDefaultOption('auth', 'google_auth'); $http->getEmitter()->attach($subscriber); return $http; } public function attachToken(ClientInterface $http, array $token, array $scopes) { $tokenFunc = function ($scopes) use ($token) { return $token['access_token']; }; $subscriber = new ScopedAccessTokenSubscriber( $tokenFunc, $scopes, $this->cacheConfig, $this->cache ); $http->setDefaultOption('auth', 'scoped'); $http->getEmitter()->attach($subscriber); return $http; } public function attachKey(ClientInterface $http, $key) { $subscriber = new SimpleSubscriber(['key' => $key]); $http->setDefaultOption('auth', 'simple'); $http->getEmitter()->attach($subscriber); return $http; } private function createAuthHttp(ClientInterface $http) { return new Client( [ 'base_url' => $http->getBaseUrl(), 'defaults' => [ 'exceptions' => true, 'verify' => $http->getDefaultOption('verify'), 'proxy' => $http->getDefaultOption('proxy'), ] ] ); } } <?php /* * Copyright 2010 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use Google\Auth\ApplicationDefaultCredentials; use Google\Auth\Cache\MemoryCacheItemPool; use Google\Auth\CredentialsLoader; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\OAuth2; use Google\Auth\Credentials\ServiceAccountCredentials; use Google\Auth\Credentials\UserRefreshCredentials; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use GuzzleHttp\Ring\Client\StreamHandler; use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; use Monolog\Logger; use Monolog\Handler\StreamHandler as MonologStreamHandler; use Monolog\Handler\SyslogHandler as MonologSyslogHandler; /** * The Google API Client * https://github.com/google/google-api-php-client */ class Google_Client { const LIBVER = "2.4.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; const OAUTH2_AUTH_URL = 'https://accounts.google.com/o/oauth2/auth'; const API_BASE_PATH = 'https://www.googleapis.com'; /** * @var Google\Auth\OAuth2 $auth */ private $auth; /** * @var GuzzleHttp\ClientInterface $http */ private $http; /** * @var Psr\Cache\CacheItemPoolInterface $cache */ private $cache; /** * @var array access token */ private $token; /** * @var array $config */ private $config; /** * @var Psr\Log\LoggerInterface $logger */ private $logger; /** * @var boolean $deferExecution */ private $deferExecution = false; /** @var array $scopes */ // Scopes requested by the client protected $requestedScopes = []; /** * Construct the Google Client. * * @param array $config */ public function __construct(array $config = array()) { $this->config = array_merge( [ 'application_name' => '', // Don't change these unless you're working against a special development // or testing environment. 'base_path' => self::API_BASE_PATH, // https://developers.google.com/console 'client_id' => '', 'client_secret' => '', 'redirect_uri' => null, 'state' => null, // Simple API access key, also from the API console. Ensure you get // a Server key, and not a Browser key. 'developer_key' => '', // For use with Google Cloud Platform // fetch the ApplicationDefaultCredentials, if applicable // @see https://developers.google.com/identity/protocols/application-default-credentials 'use_application_default_credentials' => false, 'signing_key' => null, 'signing_algorithm' => null, 'subject' => null, // Other OAuth2 parameters. 'hd' => '', 'prompt' => '', 'openid.realm' => '', 'include_granted_scopes' => null, 'login_hint' => '', 'request_visible_actions' => '', 'access_type' => 'online', 'approval_prompt' => 'auto', // Task Runner retry configuration // @see Google_Task_Runner 'retry' => array(), 'retry_map' => null, // cache config for downstream auth caching 'cache_config' => [], // function to be called when an access token is fetched // follows the signature function ($cacheKey, $accessToken) 'token_callback' => null, // Service class used in Google_Client::verifyIdToken. // Explicitly pass this in to avoid setting JWT::$leeway 'jwt' => null, // Setting api_format_v2 will return more detailed error messages // from certain APIs. 'api_format_v2' => false ], $config ); } /** * Get a string containing the version of the library. * * @return string */ public function getLibraryVersion() { return self::LIBVER; } /** * For backwards compatibility * alias for fetchAccessTokenWithAuthCode * * @param $code string code from accounts.google.com * @return array access token * @deprecated */ public function authenticate($code) { return $this->fetchAccessTokenWithAuthCode($code); } /** * Attempt to exchange a code for an valid authentication token. * Helper wrapped around the OAuth 2.0 implementation. * * @param $code string code from accounts.google.com * @return array access token */ public function fetchAccessTokenWithAuthCode($code) { if (strlen($code) == 0) { throw new InvalidArgumentException("Invalid code"); } $auth = $this->getOAuth2Service(); $auth->setCode($code); $auth->setRedirectUri($this->getRedirectUri()); $httpHandler = HttpHandlerFactory::build($this->getHttpClient()); $creds = $auth->fetchAuthToken($httpHandler); if ($creds && isset($creds['access_token'])) { $creds['created'] = time(); $this->setAccessToken($creds); } return $creds; } /** * For backwards compatibility * alias for fetchAccessTokenWithAssertion * * @return array access token * @deprecated */ public function refreshTokenWithAssertion() { return $this->fetchAccessTokenWithAssertion(); } /** * Fetches a fresh access token with a given assertion token. * @param ClientInterface $authHttp optional. * @return array access token */ public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null) { if (!$this->isUsingApplicationDefaultCredentials()) { throw new DomainException( 'set the JSON service account credentials using' . ' Google_Client::setAuthConfig or set the path to your JSON file' . ' with the "GOOGLE_APPLICATION_CREDENTIALS" environment variable' . ' and call Google_Client::useApplicationDefaultCredentials to' . ' refresh a token with assertion.' ); } $this->getLogger()->log( 'info', 'OAuth2 access token refresh with Signed JWT assertion grants.' ); $credentials = $this->createApplicationDefaultCredentials(); $httpHandler = HttpHandlerFactory::build($authHttp); $creds = $credentials->fetchAuthToken($httpHandler); if ($creds && isset($creds['access_token'])) { $creds['created'] = time(); $this->setAccessToken($creds); } return $creds; } /** * For backwards compatibility * alias for fetchAccessTokenWithRefreshToken * * @param string $refreshToken * @return array access token */ public function refreshToken($refreshToken) { return $this->fetchAccessTokenWithRefreshToken($refreshToken); } /** * Fetches a fresh OAuth 2.0 access token with the given refresh token. * @param string $refreshToken * @return array access token */ public function fetchAccessTokenWithRefreshToken($refreshToken = null) { if (null === $refreshToken) { if (!isset($this->token['refresh_token'])) { throw new LogicException( 'refresh token must be passed in or set as part of setAccessToken' ); } $refreshToken = $this->token['refresh_token']; } $this->getLogger()->info('OAuth2 access token refresh'); $auth = $this->getOAuth2Service(); $auth->setRefreshToken($refreshToken); $httpHandler = HttpHandlerFactory::build($this->getHttpClient()); $creds = $auth->fetchAuthToken($httpHandler); if ($creds && isset($creds['access_token'])) { $creds['created'] = time(); if (!isset($creds['refresh_token'])) { $creds['refresh_token'] = $refreshToken; } $this->setAccessToken($creds); } return $creds; } /** * Create a URL to obtain user authorization. * The authorization endpoint allows the user to first * authenticate, and then grant/deny the access request. * @param string|array $scope The scope is expressed as an array or list of space-delimited strings. * @return string */ public function createAuthUrl($scope = null) { if (empty($scope)) { $scope = $this->prepareScopes(); } if (is_array($scope)) { $scope = implode(' ', $scope); } // only accept one of prompt or approval_prompt $approvalPrompt = $this->config['prompt'] ? null : $this->config['approval_prompt']; // include_granted_scopes should be string "true", string "false", or null $includeGrantedScopes = $this->config['include_granted_scopes'] === null ? null : var_export($this->config['include_granted_scopes'], true); $params = array_filter( [ 'access_type' => $this->config['access_type'], 'approval_prompt' => $approvalPrompt, 'hd' => $this->config['hd'], 'include_granted_scopes' => $includeGrantedScopes, 'login_hint' => $this->config['login_hint'], 'openid.realm' => $this->config['openid.realm'], 'prompt' => $this->config['prompt'], 'response_type' => 'code', 'scope' => $scope, 'state' => $this->config['state'], ] ); // If the list of scopes contains plus.login, add request_visible_actions // to auth URL. $rva = $this->config['request_visible_actions']; if (strlen($rva) > 0 && false !== strpos($scope, 'plus.login')) { $params['request_visible_actions'] = $rva; } $auth = $this->getOAuth2Service(); return (string) $auth->buildFullAuthorizationUri($params); } /** * Adds auth listeners to the HTTP client based on the credentials * set in the Google API Client object * * @param GuzzleHttp\ClientInterface $http the http client object. * @return GuzzleHttp\ClientInterface the http client object */ public function authorize(ClientInterface $http = null) { $credentials = null; $token = null; $scopes = null; if (null === $http) { $http = $this->getHttpClient(); } // These conditionals represent the decision tree for authentication // 1. Check for Application Default Credentials // 2. Check for API Key // 3a. Check for an Access Token // 3b. If access token exists but is expired, try to refresh it if ($this->isUsingApplicationDefaultCredentials()) { $credentials = $this->createApplicationDefaultCredentials(); } elseif ($token = $this->getAccessToken()) { $scopes = $this->prepareScopes(); // add refresh subscriber to request a new token if (isset($token['refresh_token']) && $this->isAccessTokenExpired()) { $credentials = $this->createUserRefreshCredentials( $scopes, $token['refresh_token'] ); } } $authHandler = $this->getAuthHandler(); if ($credentials) { $callback = $this->config['token_callback']; $http = $authHandler->attachCredentials($http, $credentials, $callback); } elseif ($token) { $http = $authHandler->attachToken($http, $token, (array) $scopes); } elseif ($key = $this->config['developer_key']) { $http = $authHandler->attachKey($http, $key); } return $http; } /** * Set the configuration to use application default credentials for * authentication * * @see https://developers.google.com/identity/protocols/application-default-credentials * @param boolean $useAppCreds */ public function useApplicationDefaultCredentials($useAppCreds = true) { $this->config['use_application_default_credentials'] = $useAppCreds; } /** * To prevent useApplicationDefaultCredentials from inappropriately being * called in a conditional * * @see https://developers.google.com/identity/protocols/application-default-credentials */ public function isUsingApplicationDefaultCredentials() { return $this->config['use_application_default_credentials']; } /** * Set the access token used for requests. * * Note that at the time requests are sent, tokens are cached. A token will be * cached for each combination of service and authentication scopes. If a * cache pool is not provided, creating a new instance of the client will * allow modification of access tokens. If a persistent cache pool is * provided, in order to change the access token, you must clear the cached * token by calling `$client->getCache()->clear()`. (Use caution in this case, * as calling `clear()` will remove all cache items, including any items not * related to Google API PHP Client.) * * @param string|array $token * @throws InvalidArgumentException */ public function setAccessToken($token) { if (is_string($token)) { if ($json = json_decode($token, true)) { $token = $json; } else { // assume $token is just the token string $token = array( 'access_token' => $token, ); } } if ($token == null) { throw new InvalidArgumentException('invalid json token'); } if (!isset($token['access_token'])) { throw new InvalidArgumentException("Invalid token format"); } $this->token = $token; } public function getAccessToken() { return $this->token; } /** * @return string|null */ public function getRefreshToken() { if (isset($this->token['refresh_token'])) { return $this->token['refresh_token']; } return null; } /** * Returns if the access_token is expired. * @return bool Returns True if the access_token is expired. */ public function isAccessTokenExpired() { if (!$this->token) { return true; } $created = 0; if (isset($this->token['created'])) { $created = $this->token['created']; } elseif (isset($this->token['id_token'])) { // check the ID token for "iat" // signature verification is not required here, as we are just // using this for convenience to save a round trip request // to the Google API server $idToken = $this->token['id_token']; if (substr_count($idToken, '.') == 2) { $parts = explode('.', $idToken); $payload = json_decode(base64_decode($parts[1]), true); if ($payload && isset($payload['iat'])) { $created = $payload['iat']; } } } // If the token is set to expire in the next 30 seconds. return ($created + ($this->token['expires_in'] - 30)) < time(); } /** * @deprecated See UPGRADING.md for more information */ public function getAuth() { throw new BadMethodCallException( 'This function no longer exists. See UPGRADING.md for more information' ); } /** * @deprecated See UPGRADING.md for more information */ public function setAuth($auth) { throw new BadMethodCallException( 'This function no longer exists. See UPGRADING.md for more information' ); } /** * Set the OAuth 2.0 Client ID. * @param string $clientId */ public function setClientId($clientId) { $this->config['client_id'] = $clientId; } public function getClientId() { return $this->config['client_id']; } /** * Set the OAuth 2.0 Client Secret. * @param string $clientSecret */ public function setClientSecret($clientSecret) { $this->config['client_secret'] = $clientSecret; } public function getClientSecret() { return $this->config['client_secret']; } /** * Set the OAuth 2.0 Redirect URI. * @param string $redirectUri */ public function setRedirectUri($redirectUri) { $this->config['redirect_uri'] = $redirectUri; } public function getRedirectUri() { return $this->config['redirect_uri']; } /** * Set OAuth 2.0 "state" parameter to achieve per-request customization. * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2 * @param string $state */ public function setState($state) { $this->config['state'] = $state; } /** * @param string $accessType Possible values for access_type include: * {@code "offline"} to request offline access from the user. * {@code "online"} to request online access from the user. */ public function setAccessType($accessType) { $this->config['access_type'] = $accessType; } /** * @param string $approvalPrompt Possible values for approval_prompt include: * {@code "force"} to force the approval UI to appear. * {@code "auto"} to request auto-approval when possible. (This is the default value) */ public function setApprovalPrompt($approvalPrompt) { $this->config['approval_prompt'] = $approvalPrompt; } /** * Set the login hint, email address or sub id. * @param string $loginHint */ public function setLoginHint($loginHint) { $this->config['login_hint'] = $loginHint; } /** * Set the application name, this is included in the User-Agent HTTP header. * @param string $applicationName */ public function setApplicationName($applicationName) { $this->config['application_name'] = $applicationName; } /** * If 'plus.login' is included in the list of requested scopes, you can use * this method to define types of app activities that your app will write. * You can find a list of available types here: * @link https://developers.google.com/+/api/moment-types * * @param array $requestVisibleActions Array of app activity types */ public function setRequestVisibleActions($requestVisibleActions) { if (is_array($requestVisibleActions)) { $requestVisibleActions = implode(" ", $requestVisibleActions); } $this->config['request_visible_actions'] = $requestVisibleActions; } /** * Set the developer key to use, these are obtained through the API Console. * @see http://code.google.com/apis/console-help/#generatingdevkeys * @param string $developerKey */ public function setDeveloperKey($developerKey) { $this->config['developer_key'] = $developerKey; } /** * Set the hd (hosted domain) parameter streamlines the login process for * Google Apps hosted accounts. By including the domain of the user, you * restrict sign-in to accounts at that domain. * @param $hd string - the domain to use. */ public function setHostedDomain($hd) { $this->config['hd'] = $hd; } /** * Set the prompt hint. Valid values are none, consent and select_account. * If no value is specified and the user has not previously authorized * access, then the user is shown a consent screen. * @param $prompt string * {@code "none"} Do not display any authentication or consent screens. Must not be specified with other values. * {@code "consent"} Prompt the user for consent. * {@code "select_account"} Prompt the user to select an account. */ public function setPrompt($prompt) { $this->config['prompt'] = $prompt; } /** * openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth * 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which * an authentication request is valid. * @param $realm string - the URL-space to use. */ public function setOpenidRealm($realm) { $this->config['openid.realm'] = $realm; } /** * If this is provided with the value true, and the authorization request is * granted, the authorization will include any previous authorizations * granted to this user/application combination for other scopes. * @param $include boolean - the URL-space to use. */ public function setIncludeGrantedScopes($include) { $this->config['include_granted_scopes'] = $include; } /** * sets function to be called when an access token is fetched * @param callable $tokenCallback - function ($cacheKey, $accessToken) */ public function setTokenCallback(callable $tokenCallback) { $this->config['token_callback'] = $tokenCallback; } /** * Revoke an OAuth2 access token or refresh token. This method will revoke the current access * token, if a token isn't provided. * * @param string|array|null $token The token (access token or a refresh token) that should be revoked. * @return boolean Returns True if the revocation was successful, otherwise False. */ public function revokeToken($token = null) { $tokenRevoker = new Google_AccessToken_Revoke( $this->getHttpClient() ); return $tokenRevoker->revokeToken($token ?: $this->getAccessToken()); } /** * Verify an id_token. This method will verify the current id_token, if one * isn't provided. * * @throws LogicException If no token was provided and no token was set using `setAccessToken`. * @throws UnexpectedValueException If the token is not a valid JWT. * @param string|null $idToken The token (id_token) that should be verified. * @return array|false Returns the token payload as an array if the verification was * successful, false otherwise. */ public function verifyIdToken($idToken = null) { $tokenVerifier = new Google_AccessToken_Verify( $this->getHttpClient(), $this->getCache(), $this->config['jwt'] ); if (null === $idToken) { $token = $this->getAccessToken(); if (!isset($token['id_token'])) { throw new LogicException( 'id_token must be passed in or set as part of setAccessToken' ); } $idToken = $token['id_token']; } return $tokenVerifier->verifyIdToken( $idToken, $this->getClientId() ); } /** * Set the scopes to be requested. Must be called before createAuthUrl(). * Will remove any previously configured scopes. * @param string|array $scope_or_scopes, ie: array('https://www.googleapis.com/auth/plus.login', * 'https://www.googleapis.com/auth/moderator') */ public function setScopes($scope_or_scopes) { $this->requestedScopes = array(); $this->addScope($scope_or_scopes); } /** * This functions adds a scope to be requested as part of the OAuth2.0 flow. * Will append any scopes not previously requested to the scope parameter. * A single string will be treated as a scope to request. An array of strings * will each be appended. * @param $scope_or_scopes string|array e.g. "profile" */ public function addScope($scope_or_scopes) { if (is_string($scope_or_scopes) && !in_array($scope_or_scopes, $this->requestedScopes)) { $this->requestedScopes[] = $scope_or_scopes; } else if (is_array($scope_or_scopes)) { foreach ($scope_or_scopes as $scope) { $this->addScope($scope); } } } /** * Returns the list of scopes requested by the client * @return array the list of scopes * */ public function getScopes() { return $this->requestedScopes; } /** * @return string|null * @visible For Testing */ public function prepareScopes() { if (empty($this->requestedScopes)) { return null; } return implode(' ', $this->requestedScopes); } /** * Helper method to execute deferred HTTP requests. * * @param $request Psr\Http\Message\RequestInterface|Google_Http_Batch * @throws Google_Exception * @return object of the type of the expected class or Psr\Http\Message\ResponseInterface. */ public function execute(RequestInterface $request, $expectedClass = null) { $request = $request ->withHeader( 'User-Agent', sprintf( '%s %s%s', $this->config['application_name'], self::USER_AGENT_SUFFIX, $this->getLibraryVersion() ) ) ->withHeader( 'x-goog-api-client', sprintf( 'gl-php/%s gdcl/%s', phpversion(), $this->getLibraryVersion() ) ); if ($this->config['api_format_v2']) { $request = $request->withHeader( 'X-GOOG-API-FORMAT-VERSION', 2 ); } // call the authorize method // this is where most of the grunt work is done $http = $this->authorize(); return Google_Http_REST::execute( $http, $request, $expectedClass, $this->config['retry'], $this->config['retry_map'] ); } /** * Declare whether batch calls should be used. This may increase throughput * by making multiple requests in one connection. * * @param boolean $useBatch True if the batch support should * be enabled. Defaults to False. */ public function setUseBatch($useBatch) { // This is actually an alias for setDefer. $this->setDefer($useBatch); } /** * Are we running in Google AppEngine? * return bool */ public function isAppEngine() { return (isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine') !== false); } public function setConfig($name, $value) { $this->config[$name] = $value; } public function getConfig($name, $default = null) { return isset($this->config[$name]) ? $this->config[$name] : $default; } /** * For backwards compatibility * alias for setAuthConfig * * @param string $file the configuration file * @throws Google_Exception * @deprecated */ public function setAuthConfigFile($file) { $this->setAuthConfig($file); } /** * Set the auth config from new or deprecated JSON config. * This structure should match the file downloaded from * the "Download JSON" button on in the Google Developer * Console. * @param string|array $config the configuration json * @throws Google_Exception */ public function setAuthConfig($config) { if (is_string($config)) { if (!file_exists($config)) { throw new InvalidArgumentException(sprintf('file "%s" does not exist', $config)); } $json = file_get_contents($config); if (!$config = json_decode($json, true)) { throw new LogicException('invalid json for auth config'); } } $key = isset($config['installed']) ? 'installed' : 'web'; if (isset($config['type']) && $config['type'] == 'service_account') { // application default credentials $this->useApplicationDefaultCredentials(); // set the information from the config $this->setClientId($config['client_id']); $this->config['client_email'] = $config['client_email']; $this->config['signing_key'] = $config['private_key']; $this->config['signing_algorithm'] = 'HS256'; } elseif (isset($config[$key])) { // old-style $this->setClientId($config[$key]['client_id']); $this->setClientSecret($config[$key]['client_secret']); if (isset($config[$key]['redirect_uris'])) { $this->setRedirectUri($config[$key]['redirect_uris'][0]); } } else { // new-style $this->setClientId($config['client_id']); $this->setClientSecret($config['client_secret']); if (isset($config['redirect_uris'])) { $this->setRedirectUri($config['redirect_uris'][0]); } } } /** * Use when the service account has been delegated domain wide access. * * @param string $subject an email address account to impersonate */ public function setSubject($subject) { $this->config['subject'] = $subject; } /** * Declare whether making API calls should make the call immediately, or * return a request which can be called with ->execute(); * * @param boolean $defer True if calls should not be executed right away. */ public function setDefer($defer) { $this->deferExecution = $defer; } /** * Whether or not to return raw requests * @return boolean */ public function shouldDefer() { return $this->deferExecution; } /** * @return Google\Auth\OAuth2 implementation */ public function getOAuth2Service() { if (!isset($this->auth)) { $this->auth = $this->createOAuth2Service(); } return $this->auth; } /** * create a default google auth object */ protected function createOAuth2Service() { $auth = new OAuth2( [ 'clientId' => $this->getClientId(), 'clientSecret' => $this->getClientSecret(), 'authorizationUri' => self::OAUTH2_AUTH_URL, 'tokenCredentialUri' => self::OAUTH2_TOKEN_URI, 'redirectUri' => $this->getRedirectUri(), 'issuer' => $this->config['client_id'], 'signingKey' => $this->config['signing_key'], 'signingAlgorithm' => $this->config['signing_algorithm'], ] ); return $auth; } /** * Set the Cache object * @param Psr\Cache\CacheItemPoolInterface $cache */ public function setCache(CacheItemPoolInterface $cache) { $this->cache = $cache; } /** * @return Psr\Cache\CacheItemPoolInterface Cache implementation */ public function getCache() { if (!$this->cache) { $this->cache = $this->createDefaultCache(); } return $this->cache; } /** * @param array $cacheConfig */ public function setCacheConfig(array $cacheConfig) { $this->config['cache_config'] = $cacheConfig; } /** * Set the Logger object * @param Psr\Log\LoggerInterface $logger */ public function setLogger(LoggerInterface $logger) { $this->logger = $logger; } /** * @return Psr\Log\LoggerInterface implementation */ public function getLogger() { if (!isset($this->logger)) { $this->logger = $this->createDefaultLogger(); } return $this->logger; } protected function createDefaultLogger() { $logger = new Logger('google-api-php-client'); if ($this->isAppEngine()) { $handler = new MonologSyslogHandler('app', LOG_USER, Logger::NOTICE); } else { $handler = new MonologStreamHandler('php://stderr', Logger::NOTICE); } $logger->pushHandler($handler); return $logger; } protected function createDefaultCache() { return new MemoryCacheItemPool; } /** * Set the Http Client object * @param GuzzleHttp\ClientInterface $http */ public function setHttpClient(ClientInterface $http) { $this->http = $http; } /** * @return GuzzleHttp\ClientInterface implementation */ public function getHttpClient() { if (null === $this->http) { $this->http = $this->createDefaultHttpClient(); } return $this->http; } /** * Set the API format version. * * `true` will use V2, which may return more useful error messages. * * @param bool $value */ public function setApiFormatV2($value) { $this->config['api_format_v2'] = (bool) $value; } protected function createDefaultHttpClient() { $options = ['exceptions' => false]; $version = ClientInterface::VERSION; if ('5' === $version[0]) { $options = [ 'base_url' => $this->config['base_path'], 'defaults' => $options, ]; if ($this->isAppEngine()) { // set StreamHandler on AppEngine by default $options['handler'] = new StreamHandler(); $options['defaults']['verify'] = '/etc/ca-certificates.crt'; } } else { // guzzle 6 $options['base_uri'] = $this->config['base_path']; } return new Client($options); } private function createApplicationDefaultCredentials() { $scopes = $this->prepareScopes(); $sub = $this->config['subject']; $signingKey = $this->config['signing_key']; // create credentials using values supplied in setAuthConfig if ($signingKey) { $serviceAccountCredentials = array( 'client_id' => $this->config['client_id'], 'client_email' => $this->config['client_email'], 'private_key' => $signingKey, 'type' => 'service_account', ); $credentials = CredentialsLoader::makeCredentials($scopes, $serviceAccountCredentials); } else { $credentials = ApplicationDefaultCredentials::getCredentials($scopes); } // for service account domain-wide authority (impersonating a user) // @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount if ($sub) { if (!$credentials instanceof ServiceAccountCredentials) { throw new DomainException('domain-wide authority requires service account credentials'); } $credentials->setSub($sub); } return $credentials; } protected function getAuthHandler() { // Be very careful using the cache, as the underlying auth library's cache // implementation is naive, and the cache keys do not account for user // sessions. // // @see https://github.com/google/google-api-php-client/issues/821 return Google_AuthHandler_AuthHandlerFactory::build( $this->getCache(), $this->config['cache_config'] ); } private function createUserRefreshCredentials($scope, $refreshToken) { $creds = array_filter( array( 'client_id' => $this->getClientId(), 'client_secret' => $this->getClientSecret(), 'refresh_token' => $refreshToken, ) ); return new UserRefreshCredentials($scope, $creds); } } <?php /* * Copyright 2013 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ class Google_Exception extends Exception { } <?php /* * Copyright 2013 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Implementation of levels 1-3 of the URI Template spec. * @see http://tools.ietf.org/html/rfc6570 */ class Google_Utils_UriTemplate { const TYPE_MAP = "1"; const TYPE_LIST = "2"; const TYPE_SCALAR = "4"; /** * @var $operators array * These are valid at the start of a template block to * modify the way in which the variables inside are * processed. */ private $operators = array( "+" => "reserved", "/" => "segments", "." => "dotprefix", "#" => "fragment", ";" => "semicolon", "?" => "form", "&" => "continuation" ); /** * @var reserved array * These are the characters which should not be URL encoded in reserved * strings. */ private $reserved = array( "=", ",", "!", "@", "|", ":", "/", "?", "#", "[", "]",'$', "&", "'", "(", ")", "*", "+", ";" ); private $reservedEncoded = array( "%3D", "%2C", "%21", "%40", "%7C", "%3A", "%2F", "%3F", "%23", "%5B", "%5D", "%24", "%26", "%27", "%28", "%29", "%2A", "%2B", "%3B" ); public function parse($string, array $parameters) { return $this->resolveNextSection($string, $parameters); } /** * This function finds the first matching {...} block and * executes the replacement. It then calls itself to find * subsequent blocks, if any. */ private function resolveNextSection($string, $parameters) { $start = strpos($string, "{"); if ($start === false) { return $string; } $end = strpos($string, "}"); if ($end === false) { return $string; } $string = $this->replace($string, $start, $end, $parameters); return $this->resolveNextSection($string, $parameters); } private function replace($string, $start, $end, $parameters) { // We know a data block will have {} round it, so we can strip that. $data = substr($string, $start + 1, $end - $start - 1); // If the first character is one of the reserved operators, it effects // the processing of the stream. if (isset($this->operators[$data[0]])) { $op = $this->operators[$data[0]]; $data = substr($data, 1); $prefix = ""; $prefix_on_missing = false; switch ($op) { case "reserved": // Reserved means certain characters should not be URL encoded $data = $this->replaceVars($data, $parameters, ",", null, true); break; case "fragment": // Comma separated with fragment prefix. Bare values only. $prefix = "#"; $prefix_on_missing = true; $data = $this->replaceVars($data, $parameters, ",", null, true); break; case "segments": // Slash separated data. Bare values only. $prefix = "/"; $data =$this->replaceVars($data, $parameters, "/"); break; case "dotprefix": // Dot separated data. Bare values only. $prefix = "."; $prefix_on_missing = true; $data = $this->replaceVars($data, $parameters, "."); break; case "semicolon": // Semicolon prefixed and separated. Uses the key name $prefix = ";"; $data = $this->replaceVars($data, $parameters, ";", "=", false, true, false); break; case "form": // Standard URL format. Uses the key name $prefix = "?"; $data = $this->replaceVars($data, $parameters, "&", "="); break; case "continuation": // Standard URL, but with leading ampersand. Uses key name. $prefix = "&"; $data = $this->replaceVars($data, $parameters, "&", "="); break; } // Add the initial prefix character if data is valid. if ($data || ($data !== false && $prefix_on_missing)) { $data = $prefix . $data; } } else { // If no operator we replace with the defaults. $data = $this->replaceVars($data, $parameters); } // This is chops out the {...} and replaces with the new section. return substr($string, 0, $start) . $data . substr($string, $end + 1); } private function replaceVars( $section, $parameters, $sep = ",", $combine = null, $reserved = false, $tag_empty = false, $combine_on_empty = true ) { if (strpos($section, ",") === false) { // If we only have a single value, we can immediately process. return $this->combine( $section, $parameters, $sep, $combine, $reserved, $tag_empty, $combine_on_empty ); } else { // If we have multiple values, we need to split and loop over them. // Each is treated individually, then glued together with the // separator character. $vars = explode(",", $section); return $this->combineList( $vars, $sep, $parameters, $combine, $reserved, false, // Never emit empty strings in multi-param replacements $combine_on_empty ); } } public function combine( $key, $parameters, $sep, $combine, $reserved, $tag_empty, $combine_on_empty ) { $length = false; $explode = false; $skip_final_combine = false; $value = false; // Check for length restriction. if (strpos($key, ":") !== false) { list($key, $length) = explode(":", $key); } // Check for explode parameter. if ($key[strlen($key) - 1] == "*") { $explode = true; $key = substr($key, 0, -1); $skip_final_combine = true; } // Define the list separator. $list_sep = $explode ? $sep : ","; if (isset($parameters[$key])) { $data_type = $this->getDataType($parameters[$key]); switch ($data_type) { case self::TYPE_SCALAR: $value = $this->getValue($parameters[$key], $length); break; case self::TYPE_LIST: $values = array(); foreach ($parameters[$key] as $pkey => $pvalue) { $pvalue = $this->getValue($pvalue, $length); if ($combine && $explode) { $values[$pkey] = $key . $combine . $pvalue; } else { $values[$pkey] = $pvalue; } } $value = implode($list_sep, $values); if ($value == '') { return ''; } break; case self::TYPE_MAP: $values = array(); foreach ($parameters[$key] as $pkey => $pvalue) { $pvalue = $this->getValue($pvalue, $length); if ($explode) { $pkey = $this->getValue($pkey, $length); $values[] = $pkey . "=" . $pvalue; // Explode triggers = combine. } else { $values[] = $pkey; $values[] = $pvalue; } } $value = implode($list_sep, $values); if ($value == '') { return false; } break; } } else if ($tag_empty) { // If we are just indicating empty values with their key name, return that. return $key; } else { // Otherwise we can skip this variable due to not being defined. return false; } if ($reserved) { $value = str_replace($this->reservedEncoded, $this->reserved, $value); } // If we do not need to include the key name, we just return the raw // value. if (!$combine || $skip_final_combine) { return $value; } // Else we combine the key name: foo=bar, if value is not the empty string. return $key . ($value != '' || $combine_on_empty ? $combine . $value : ''); } /** * Return the type of a passed in value */ private function getDataType($data) { if (is_array($data)) { reset($data); if (key($data) !== 0) { return self::TYPE_MAP; } return self::TYPE_LIST; } return self::TYPE_SCALAR; } /** * Utility function that merges multiple combine calls * for multi-key templates. */ private function combineList( $vars, $sep, $parameters, $combine, $reserved, $tag_empty, $combine_on_empty ) { $ret = array(); foreach ($vars as $var) { $response = $this->combine( $var, $parameters, $sep, $combine, $reserved, $tag_empty, $combine_on_empty ); if ($response === false) { continue; } $ret[] = $response; } return implode($sep, $ret); } /** * Utility function to encode and trim values */ private function getValue($value, $length) { if ($length) { $value = substr($value, 0, $length); } $value = rawurlencode($value); return $value; } } <?php /** * THIS FILE IS FOR BACKWARDS COMPATIBLITY ONLY * * If you were not already including this file in your project, please ignore it */ $file = __DIR__ . '/../../vendor/autoload.php'; if (!file_exists($file)) { $exception = 'This library must be installed via composer or by downloading the full package.'; $exception .= ' See the instructions at https://github.com/google/google-api-php-client#installation.'; throw new Exception($exception); } $error = 'google-api-php-client\'s autoloader was moved to vendor/autoload.php in 2.0.0. This '; $error .= 'redirect will be removed in 2.1. Please adjust your code to use the new location.'; trigger_error($error, E_USER_DEPRECATED); require_once $file; <?php /** * Copyright 2012 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Uri; use Psr\Http\Message\RequestInterface; /** * Manage large file uploads, which may be media but can be any type * of sizable data. */ class Google_Http_MediaFileUpload { const UPLOAD_MEDIA_TYPE = 'media'; const UPLOAD_MULTIPART_TYPE = 'multipart'; const UPLOAD_RESUMABLE_TYPE = 'resumable'; /** @var string $mimeType */ private $mimeType; /** @var string $data */ private $data; /** @var bool $resumable */ private $resumable; /** @var int $chunkSize */ private $chunkSize; /** @var int $size */ private $size; /** @var string $resumeUri */ private $resumeUri; /** @var int $progress */ private $progress; /** @var Google_Client */ private $client; /** @var Psr\Http\Message\RequestInterface */ private $request; /** @var string */ private $boundary; /** * Result code from last HTTP call * @var int */ private $httpResultCode; /** * @param Google_Client $client * @param RequestInterface $request * @param string $mimeType * @param string $data The bytes you want to upload. * @param bool $resumable * @param bool $chunkSize File will be uploaded in chunks of this many bytes. * only used if resumable=True */ public function __construct( Google_Client $client, RequestInterface $request, $mimeType, $data, $resumable = false, $chunkSize = false ) { $this->client = $client; $this->request = $request; $this->mimeType = $mimeType; $this->data = $data; $this->resumable = $resumable; $this->chunkSize = $chunkSize; $this->progress = 0; $this->process(); } /** * Set the size of the file that is being uploaded. * @param $size - int file size in bytes */ public function setFileSize($size) { $this->size = $size; } /** * Return the progress on the upload * @return int progress in bytes uploaded. */ public function getProgress() { return $this->progress; } /** * Send the next part of the file to upload. * @param [$chunk] the next set of bytes to send. If false will used $data passed * at construct time. */ public function nextChunk($chunk = false) { $resumeUri = $this->getResumeUri(); if (false == $chunk) { $chunk = substr($this->data, $this->progress, $this->chunkSize); } $lastBytePos = $this->progress + strlen($chunk) - 1; $headers = array( 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", 'content-length' => strlen($chunk), 'expect' => '', ); $request = new Request( 'PUT', $resumeUri, $headers, Psr7\stream_for($chunk) ); return $this->makePutRequest($request); } /** * Return the HTTP result code from the last call made. * @return int code */ public function getHttpResultCode() { return $this->httpResultCode; } /** * Sends a PUT-Request to google drive and parses the response, * setting the appropiate variables from the response() * * @param Google_Http_Request $httpRequest the Reuqest which will be send * * @return false|mixed false when the upload is unfinished or the decoded http response * */ private function makePutRequest(RequestInterface $request) { $response = $this->client->execute($request); $this->httpResultCode = $response->getStatusCode(); if (308 == $this->httpResultCode) { // Track the amount uploaded. $range = $response->getHeaderLine('range'); if ($range) { $range_array = explode('-', $range); $this->progress = $range_array[1] + 1; } // Allow for changing upload URLs. $location = $response->getHeaderLine('location'); if ($location) { $this->resumeUri = $location; } // No problems, but upload not complete. return false; } return Google_Http_REST::decodeHttpResponse($response, $this->request); } /** * Resume a previously unfinished upload * @param $resumeUri the resume-URI of the unfinished, resumable upload. */ public function resume($resumeUri) { $this->resumeUri = $resumeUri; $headers = array( 'content-range' => "bytes */$this->size", 'content-length' => 0, ); $httpRequest = new Request( 'PUT', $this->resumeUri, $headers ); return $this->makePutRequest($httpRequest); } /** * @return Psr\Http\Message\RequestInterface $request * @visible for testing */ private function process() { $this->transformToUploadUrl(); $request = $this->request; $postBody = ''; $contentType = false; $meta = (string) $request->getBody(); $meta = is_string($meta) ? json_decode($meta, true) : $meta; $uploadType = $this->getUploadType($meta); $request = $request->withUri( Uri::withQueryValue($request->getUri(), 'uploadType', $uploadType) ); $mimeType = $this->mimeType ?: $request->getHeaderLine('content-type'); if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { $contentType = $mimeType; $postBody = is_string($meta) ? $meta : json_encode($meta); } else if (self::UPLOAD_MEDIA_TYPE == $uploadType) { $contentType = $mimeType; $postBody = $this->data; } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { // This is a multipart/related upload. $boundary = $this->boundary ?: mt_rand(); $boundary = str_replace('"', '', $boundary); $contentType = 'multipart/related; boundary=' . $boundary; $related = "--$boundary\r\n"; $related .= "Content-Type: application/json; charset=UTF-8\r\n"; $related .= "\r\n" . json_encode($meta) . "\r\n"; $related .= "--$boundary\r\n"; $related .= "Content-Type: $mimeType\r\n"; $related .= "Content-Transfer-Encoding: base64\r\n"; $related .= "\r\n" . base64_encode($this->data) . "\r\n"; $related .= "--$boundary--"; $postBody = $related; } $request = $request->withBody(Psr7\stream_for($postBody)); if (isset($contentType) && $contentType) { $request = $request->withHeader('content-type', $contentType); } return $this->request = $request; } /** * Valid upload types: * - resumable (UPLOAD_RESUMABLE_TYPE) * - media (UPLOAD_MEDIA_TYPE) * - multipart (UPLOAD_MULTIPART_TYPE) * @param $meta * @return string * @visible for testing */ public function getUploadType($meta) { if ($this->resumable) { return self::UPLOAD_RESUMABLE_TYPE; } if (false == $meta && $this->data) { return self::UPLOAD_MEDIA_TYPE; } return self::UPLOAD_MULTIPART_TYPE; } public function getResumeUri() { if (null === $this->resumeUri) { $this->resumeUri = $this->fetchResumeUri(); } return $this->resumeUri; } private function fetchResumeUri() { $body = $this->request->getBody(); if ($body) { $headers = array( 'content-type' => 'application/json; charset=UTF-8', 'content-length' => $body->getSize(), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => '', ); foreach ($headers as $key => $value) { $this->request = $this->request->withHeader($key, $value); } } $response = $this->client->execute($this->request, false); $location = $response->getHeaderLine('location'); $code = $response->getStatusCode(); if (200 == $code && true == $location) { return $location; } $message = $code; $body = json_decode((string) $this->request->getBody(), true); if (isset($body['error']['errors'])) { $message .= ': '; foreach ($body['error']['errors'] as $error) { $message .= "{$error['domain']}, {$error['message']};"; } $message = rtrim($message, ';'); } $error = "Failed to start the resumable upload (HTTP {$message})"; $this->client->getLogger()->error($error); throw new Google_Exception($error); } private function transformToUploadUrl() { $parts = parse_url((string) $this->request->getUri()); if (!isset($parts['path'])) { $parts['path'] = ''; } $parts['path'] = '/upload' . $parts['path']; $uri = Uri::fromParts($parts); $this->request = $this->request->withUri($uri); } public function setChunkSize($chunkSize) { $this->chunkSize = $chunkSize; } public function getRequest() { return $this->request; } } <?php /* * Copyright 2010 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use Google\Auth\HttpHandler\HttpHandlerFactory; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\Response; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** * This class implements the RESTful transport of apiServiceRequest()'s */ class Google_Http_REST { /** * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries * when errors occur. * * @param Google_Client $client * @param Psr\Http\Message\RequestInterface $req * @return array decoded result * @throws Google_Service_Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ public static function execute( ClientInterface $client, RequestInterface $request, $expectedClass = null, $config = array(), $retryMap = null ) { $runner = new Google_Task_Runner( $config, sprintf('%s %s', $request->getMethod(), (string) $request->getUri()), array(get_class(), 'doExecute'), array($client, $request, $expectedClass) ); if (null !== $retryMap) { $runner->setRetryMap($retryMap); } return $runner->run(); } /** * Executes a Psr\Http\Message\RequestInterface * * @param Google_Client $client * @param Psr\Http\Message\RequestInterface $request * @return array decoded result * @throws Google_Service_Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null) { try { $httpHandler = HttpHandlerFactory::build($client); $response = $httpHandler($request); } catch (RequestException $e) { // if Guzzle throws an exception, catch it and handle the response if (!$e->hasResponse()) { throw $e; } $response = $e->getResponse(); // specific checking for Guzzle 5: convert to PSR7 response if ($response instanceof \GuzzleHttp\Message\ResponseInterface) { $response = new Response( $response->getStatusCode(), $response->getHeaders() ?: [], $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase() ); } } return self::decodeHttpResponse($response, $request, $expectedClass); } /** * Decode an HTTP Response. * @static * @throws Google_Service_Exception * @param Psr\Http\Message\RequestInterface $response The http response to be decoded. * @param Psr\Http\Message\ResponseInterface $response * @return mixed|null */ public static function decodeHttpResponse( ResponseInterface $response, RequestInterface $request = null, $expectedClass = null ) { $code = $response->getStatusCode(); // retry strategy if (intVal($code) >= 400) { // if we errored out, it should be safe to grab the response body $body = (string) $response->getBody(); // Check if we received errors, and add those to the Exception for convenience throw new Google_Service_Exception($body, $code, null, self::getResponseErrors($body)); } // Ensure we only pull the entire body into memory if the request is not // of media type $body = self::decodeBody($response, $request); if ($expectedClass = self::determineExpectedClass($expectedClass, $request)) { $json = json_decode($body, true); return new $expectedClass($json); } return $response; } private static function decodeBody(ResponseInterface $response, RequestInterface $request = null) { if (self::isAltMedia($request)) { // don't decode the body, it's probably a really long string return ''; } return (string) $response->getBody(); } private static function determineExpectedClass($expectedClass, RequestInterface $request = null) { // "false" is used to explicitly prevent an expected class from being returned if (false === $expectedClass) { return null; } // if we don't have a request, we just use what's passed in if (null === $request) { return $expectedClass; } // return what we have in the request header if one was not supplied return $expectedClass ?: $request->getHeaderLine('X-Php-Expected-Class'); } private static function getResponseErrors($body) { $json = json_decode($body, true); if (isset($json['error']['errors'])) { return $json['error']['errors']; } return null; } private static function isAltMedia(RequestInterface $request = null) { if ($request && $qs = $request->getUri()->getQuery()) { parse_str($qs, $query); if (isset($query['alt']) && $query['alt'] == 'media') { return true; } } return false; } } <?php /* * Copyright 2012 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** * Class to handle batched requests to the Google API service. * * Note that calls to `Google_Http_Batch::execute()` do not clear the queued * requests. To start a new batch, be sure to create a new instance of this * class. */ class Google_Http_Batch { const BATCH_PATH = 'batch'; private static $CONNECTION_ESTABLISHED_HEADERS = array( "HTTP/1.0 200 Connection established\r\n\r\n", "HTTP/1.1 200 Connection established\r\n\r\n", ); /** @var string Multipart Boundary. */ private $boundary; /** @var array service requests to be executed. */ private $requests = array(); /** @var Google_Client */ private $client; private $rootUrl; private $batchPath; public function __construct( Google_Client $client, $boundary = false, $rootUrl = null, $batchPath = null ) { $this->client = $client; $this->boundary = $boundary ?: mt_rand(); $this->rootUrl = rtrim($rootUrl ?: $this->client->getConfig('base_path'), '/'); $this->batchPath = $batchPath ?: self::BATCH_PATH; } public function add(RequestInterface $request, $key = false) { if (false == $key) { $key = mt_rand(); } $this->requests[$key] = $request; } public function execute() { $body = ''; $classes = array(); $batchHttpTemplate = <<<EOF --%s Content-Type: application/http Content-Transfer-Encoding: binary MIME-Version: 1.0 Content-ID: %s %s %s%s EOF; /** @var Google_Http_Request $req */ foreach ($this->requests as $key => $request) { $firstLine = sprintf( '%s %s HTTP/%s', $request->getMethod(), $request->getRequestTarget(), $request->getProtocolVersion() ); $content = (string) $request->getBody(); $headers = ''; foreach ($request->getHeaders() as $name => $values) { $headers .= sprintf("%s:%s\r\n", $name, implode(', ', $values)); } $body .= sprintf( $batchHttpTemplate, $this->boundary, $key, $firstLine, $headers, $content ? "\n".$content : '' ); $classes['response-' . $key] = $request->getHeaderLine('X-Php-Expected-Class'); } $body .= "--{$this->boundary}--"; $body = trim($body); $url = $this->rootUrl . '/' . $this->batchPath; $headers = array( 'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary), 'Content-Length' => strlen($body), ); $request = new Request( 'POST', $url, $headers, $body ); $response = $this->client->execute($request); return $this->parseResponse($response, $classes); } public function parseResponse(ResponseInterface $response, $classes = array()) { $contentType = $response->getHeaderLine('content-type'); $contentType = explode(';', $contentType); $boundary = false; foreach ($contentType as $part) { $part = explode('=', $part, 2); if (isset($part[0]) && 'boundary' == trim($part[0])) { $boundary = $part[1]; } } $body = (string) $response->getBody(); if (!empty($body)) { $body = str_replace("--$boundary--", "--$boundary", $body); $parts = explode("--$boundary", $body); $responses = array(); $requests = array_values($this->requests); foreach ($parts as $i => $part) { $part = trim($part); if (!empty($part)) { list($rawHeaders, $part) = explode("\r\n\r\n", $part, 2); $headers = $this->parseRawHeaders($rawHeaders); $status = substr($part, 0, strpos($part, "\n")); $status = explode(" ", $status); $status = $status[1]; list($partHeaders, $partBody) = $this->parseHttpResponse($part, false); $response = new Response( $status, $partHeaders, Psr7\stream_for($partBody) ); // Need content id. $key = $headers['content-id']; try { $response = Google_Http_REST::decodeHttpResponse($response, $requests[$i-1]); } catch (Google_Service_Exception $e) { // Store the exception as the response, so successful responses // can be processed. $response = $e; } $responses[$key] = $response; } } return $responses; } return null; } private function parseRawHeaders($rawHeaders) { $headers = array(); $responseHeaderLines = explode("\r\n", $rawHeaders); foreach ($responseHeaderLines as $headerLine) { if ($headerLine && strpos($headerLine, ':') !== false) { list($header, $value) = explode(': ', $headerLine, 2); $header = strtolower($header); if (isset($headers[$header])) { $headers[$header] .= "\n" . $value; } else { $headers[$header] = $value; } } } return $headers; } /** * Used by the IO lib and also the batch processing. * * @param $respData * @param $headerSize * @return array */ private function parseHttpResponse($respData, $headerSize) { // check proxy header foreach (self::$CONNECTION_ESTABLISHED_HEADERS as $established_header) { if (stripos($respData, $established_header) !== false) { // existed, remove it $respData = str_ireplace($established_header, '', $respData); // Subtract the proxy header size unless the cURL bug prior to 7.30.0 // is present which prevented the proxy header size from being taken into // account. // @TODO look into this // if (!$this->needsQuirk()) { // $headerSize -= strlen($established_header); // } break; } } if ($headerSize) { $responseBody = substr($respData, $headerSize); $responseHeaders = substr($respData, 0, $headerSize); } else { $responseSegments = explode("\r\n\r\n", $respData, 2); $responseHeaders = $responseSegments[0]; $responseBody = isset($responseSegments[1]) ? $responseSegments[1] : null; } $responseHeaders = $this->parseRawHeaders($responseHeaders); return array($responseHeaders, $responseBody); } } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ class Google_Service_Exception extends Google_Exception { /** * Optional list of errors returned in a JSON body of an HTTP error response. */ protected $errors = array(); /** * Override default constructor to add the ability to set $errors and a retry * map. * * @param string $message * @param int $code * @param Exception|null $previous * @param [{string, string}] errors List of errors returned in an HTTP * response. Defaults to []. * @param array|null $retryMap Map of errors with retry counts. */ public function __construct( $message, $code = 0, Exception $previous = null, $errors = array() ) { if (version_compare(PHP_VERSION, '5.3.0') >= 0) { parent::__construct($message, $code, $previous); } else { parent::__construct($message, $code); } $this->errors = $errors; } /** * An example of the possible errors returned. * * { * "domain": "global", * "reason": "authError", * "message": "Invalid Credentials", * "locationType": "header", * "location": "Authorization", * } * * @return [{string, string}] List of errors return in an HTTP response or []. */ public function getErrors() { return $this->errors; } } <?php /** * Copyright 2010 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use GuzzleHttp\Psr7\Request; /** * Implements the actual methods/resources of the discovered Google API using magic function * calling overloading (__call()), which on call will see if the method name (plus.activities.list) * is available in this service, and if so construct an apiHttpRequest representing it. * */ class Google_Service_Resource { // Valid query parameters that work, but don't appear in discovery. private $stackParameters = array( 'alt' => array('type' => 'string', 'location' => 'query'), 'fields' => array('type' => 'string', 'location' => 'query'), 'trace' => array('type' => 'string', 'location' => 'query'), 'userIp' => array('type' => 'string', 'location' => 'query'), 'quotaUser' => array('type' => 'string', 'location' => 'query'), 'data' => array('type' => 'string', 'location' => 'body'), 'mimeType' => array('type' => 'string', 'location' => 'header'), 'uploadType' => array('type' => 'string', 'location' => 'query'), 'mediaUpload' => array('type' => 'complex', 'location' => 'query'), 'prettyPrint' => array('type' => 'string', 'location' => 'query'), ); /** @var string $rootUrl */ private $rootUrl; /** @var Google_Client $client */ private $client; /** @var string $serviceName */ private $serviceName; /** @var string $servicePath */ private $servicePath; /** @var string $resourceName */ private $resourceName; /** @var array $methods */ private $methods; public function __construct($service, $serviceName, $resourceName, $resource) { $this->rootUrl = $service->rootUrl; $this->client = $service->getClient(); $this->servicePath = $service->servicePath; $this->serviceName = $serviceName; $this->resourceName = $resourceName; $this->methods = is_array($resource) && isset($resource['methods']) ? $resource['methods'] : array($resourceName => $resource); } /** * TODO: This function needs simplifying. * @param $name * @param $arguments * @param $expectedClass - optional, the expected class name * @return Google_Http_Request|expectedClass * @throws Google_Exception */ public function call($name, $arguments, $expectedClass = null) { if (! isset($this->methods[$name])) { $this->client->getLogger()->error( 'Service method unknown', array( 'service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name ) ); throw new Google_Exception( "Unknown function: " . "{$this->serviceName}->{$this->resourceName}->{$name}()" ); } $method = $this->methods[$name]; $parameters = $arguments[0]; // postBody is a special case since it's not defined in the discovery // document as parameter, but we abuse the param entry for storing it. $postBody = null; if (isset($parameters['postBody'])) { if ($parameters['postBody'] instanceof Google_Model) { // In the cases the post body is an existing object, we want // to use the smart method to create a simple object for // for JSONification. $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); } else if (is_object($parameters['postBody'])) { // If the post body is another kind of object, we will try and // wrangle it into a sensible format. $parameters['postBody'] = $this->convertToArrayAndStripNulls($parameters['postBody']); } $postBody = (array) $parameters['postBody']; unset($parameters['postBody']); } // TODO: optParams here probably should have been // handled already - this may well be redundant code. if (isset($parameters['optParams'])) { $optParams = $parameters['optParams']; unset($parameters['optParams']); $parameters = array_merge($parameters, $optParams); } if (!isset($method['parameters'])) { $method['parameters'] = array(); } $method['parameters'] = array_merge( $this->stackParameters, $method['parameters'] ); foreach ($parameters as $key => $val) { if ($key != 'postBody' && ! isset($method['parameters'][$key])) { $this->client->getLogger()->error( 'Service parameter unknown', array( 'service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $key ) ); throw new Google_Exception("($name) unknown parameter: '$key'"); } } foreach ($method['parameters'] as $paramName => $paramSpec) { if (isset($paramSpec['required']) && $paramSpec['required'] && ! isset($parameters[$paramName]) ) { $this->client->getLogger()->error( 'Service parameter missing', array( 'service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $paramName ) ); throw new Google_Exception("($name) missing required param: '$paramName'"); } if (isset($parameters[$paramName])) { $value = $parameters[$paramName]; $parameters[$paramName] = $paramSpec; $parameters[$paramName]['value'] = $value; unset($parameters[$paramName]['required']); } else { // Ensure we don't pass nulls. unset($parameters[$paramName]); } } $this->client->getLogger()->info( 'Service Call', array( 'service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'arguments' => $parameters, ) ); // build the service uri $url = $this->createRequestUri( $method['path'], $parameters ); // NOTE: because we're creating the request by hand, // and because the service has a rootUrl property // the "base_uri" of the Http Client is not accounted for $request = new Request( $method['httpMethod'], $url, ['content-type' => 'application/json'], $postBody ? json_encode($postBody) : '' ); // support uploads if (isset($parameters['data'])) { $mimeType = isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream'; $data = $parameters['data']['value']; $upload = new Google_Http_MediaFileUpload($this->client, $request, $mimeType, $data); // pull down the modified request $request = $upload->getRequest(); } // if this is a media type, we will return the raw response // rather than using an expected class if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') { $expectedClass = null; } // if the client is marked for deferring, rather than // execute the request, return the response if ($this->client->shouldDefer()) { // @TODO find a better way to do this $request = $request ->withHeader('X-Php-Expected-Class', $expectedClass); return $request; } return $this->client->execute($request, $expectedClass); } protected function convertToArrayAndStripNulls($o) { $o = (array) $o; foreach ($o as $k => $v) { if ($v === null) { unset($o[$k]); } elseif (is_object($v) || is_array($v)) { $o[$k] = $this->convertToArrayAndStripNulls($o[$k]); } } return $o; } /** * Parse/expand request parameters and create a fully qualified * request uri. * @static * @param string $restPath * @param array $params * @return string $requestUrl */ public function createRequestUri($restPath, $params) { // Override the default servicePath address if the $restPath use a / if ('/' == substr($restPath, 0, 1)) { $requestUrl = substr($restPath, 1); } else { $requestUrl = $this->servicePath . $restPath; } // code for leading slash if ($this->rootUrl) { if ('/' !== substr($this->rootUrl, -1) && '/' !== substr($requestUrl, 0, 1)) { $requestUrl = '/' . $requestUrl; } $requestUrl = $this->rootUrl . $requestUrl; } $uriTemplateVars = array(); $queryVars = array(); foreach ($params as $paramName => $paramSpec) { if ($paramSpec['type'] == 'boolean') { $paramSpec['value'] = $paramSpec['value'] ? 'true' : 'false'; } if ($paramSpec['location'] == 'path') { $uriTemplateVars[$paramName] = $paramSpec['value']; } else if ($paramSpec['location'] == 'query') { if (is_array($paramSpec['value'])) { foreach ($paramSpec['value'] as $value) { $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); } } else { $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); } } } if (count($uriTemplateVars)) { $uriTemplateParser = new Google_Utils_UriTemplate(); $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); } if (count($queryVars)) { $requestUrl .= '?' . implode('&', $queryVars); } return $requestUrl; } } # Google API Client Services Google API Client Service classes have been moved to the [google-api-php-client-services](https://github.com/google/google-api-php-client-services) repository. <?php /* * Copyright 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use Google\Auth\HttpHandler\HttpHandlerFactory; use GuzzleHttp\ClientInterface; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; /** * Wrapper around Google Access Tokens which provides convenience functions * */ class Google_AccessToken_Revoke { /** * @var GuzzleHttp\ClientInterface The http client */ private $http; /** * Instantiates the class, but does not initiate the login flow, leaving it * to the discretion of the caller. */ public function __construct(ClientInterface $http = null) { $this->http = $http; } /** * Revoke an OAuth2 access token or refresh token. This method will revoke the current access * token, if a token isn't provided. * * @param string|array $token The token (access token or a refresh token) that should be revoked. * @return boolean Returns True if the revocation was successful, otherwise False. */ public function revokeToken($token) { if (is_array($token)) { if (isset($token['refresh_token'])) { $token = $token['refresh_token']; } else { $token = $token['access_token']; } } $body = Psr7\stream_for(http_build_query(array('token' => $token))); $request = new Request( 'POST', Google_Client::OAUTH2_REVOKE_URI, [ 'Cache-Control' => 'no-store', 'Content-Type' => 'application/x-www-form-urlencoded', ], $body ); $httpHandler = HttpHandlerFactory::build($this->http); $response = $httpHandler($request); return $response->getStatusCode() == 200; } } <?php /* * Copyright 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use Firebase\JWT\ExpiredException as ExpiredExceptionV3; use Firebase\JWT\SignatureInvalidException; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use Psr\Cache\CacheItemPoolInterface; use Google\Auth\Cache\MemoryCacheItemPool; use Stash\Driver\FileSystem; use Stash\Pool; /** * Wrapper around Google Access Tokens which provides convenience functions * */ class Google_AccessToken_Verify { const FEDERATED_SIGNON_CERT_URL = 'https://www.googleapis.com/oauth2/v3/certs'; const OAUTH2_ISSUER = 'accounts.google.com'; const OAUTH2_ISSUER_HTTPS = 'https://accounts.google.com'; /** * @var GuzzleHttp\ClientInterface The http client */ private $http; /** * @var Psr\Cache\CacheItemPoolInterface cache class */ private $cache; /** * Instantiates the class, but does not initiate the login flow, leaving it * to the discretion of the caller. */ public function __construct( ClientInterface $http = null, CacheItemPoolInterface $cache = null, $jwt = null ) { if (null === $http) { $http = new Client(); } if (null === $cache) { $cache = new MemoryCacheItemPool; } $this->http = $http; $this->cache = $cache; $this->jwt = $jwt ?: $this->getJwtService(); } /** * Verifies an id token and returns the authenticated apiLoginTicket. * Throws an exception if the id token is not valid. * The audience parameter can be used to control which id tokens are * accepted. By default, the id token must have been issued to this OAuth2 client. * * @param $audience * @return array the token payload, if successful */ public function verifyIdToken($idToken, $audience = null) { if (empty($idToken)) { throw new LogicException('id_token cannot be null'); } // set phpseclib constants if applicable $this->setPhpsecConstants(); // Check signature $certs = $this->getFederatedSignOnCerts(); foreach ($certs as $cert) { $bigIntClass = $this->getBigIntClass(); $rsaClass = $this->getRsaClass(); $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); $rsa = new $rsaClass(); $rsa->loadKey(array('n' => $modulus, 'e' => $exponent)); try { $payload = $this->jwt->decode( $idToken, $rsa->getPublicKey(), array('RS256') ); if (property_exists($payload, 'aud')) { if ($audience && $payload->aud != $audience) { return false; } } // support HTTP and HTTPS issuers // @see https://developers.google.com/identity/sign-in/web/backend-auth $issuers = array(self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS); if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) { return false; } return (array) $payload; } catch (ExpiredException $e) { return false; } catch (ExpiredExceptionV3 $e) { return false; } catch (SignatureInvalidException $e) { // continue } catch (DomainException $e) { // continue } } return false; } private function getCache() { return $this->cache; } /** * Retrieve and cache a certificates file. * * @param $url string location * @throws Google_Exception * @return array certificates */ private function retrieveCertsFromLocation($url) { // If we're retrieving a local file, just grab it. if (0 !== strpos($url, 'http')) { if (!$file = file_get_contents($url)) { throw new Google_Exception( "Failed to retrieve verification certificates: '" . $url . "'." ); } return json_decode($file, true); } $response = $this->http->get($url); if ($response->getStatusCode() == 200) { return json_decode((string) $response->getBody(), true); } throw new Google_Exception( sprintf( 'Failed to retrieve verification certificates: "%s".', $response->getBody()->getContents() ), $response->getStatusCode() ); } // Gets federated sign-on certificates to use for verifying identity tokens. // Returns certs as array structure, where keys are key ids, and values // are PEM encoded certificates. private function getFederatedSignOnCerts() { $certs = null; if ($cache = $this->getCache()) { $cacheItem = $cache->getItem('federated_signon_certs_v3'); $certs = $cacheItem->get(); } if (!$certs) { $certs = $this->retrieveCertsFromLocation( self::FEDERATED_SIGNON_CERT_URL ); if ($cache) { $cacheItem->expiresAt(new DateTime('+1 hour')); $cacheItem->set($certs); $cache->save($cacheItem); } } if (!isset($certs['keys'])) { throw new InvalidArgumentException( 'federated sign-on certs expects "keys" to be set' ); } return $certs['keys']; } private function getJwtService() { $jwtClass = 'JWT'; if (class_exists('\Firebase\JWT\JWT')) { $jwtClass = 'Firebase\JWT\JWT'; } if (property_exists($jwtClass, 'leeway') && $jwtClass::$leeway < 1) { // Ensures JWT leeway is at least 1 // @see https://github.com/google/google-api-php-client/issues/827 $jwtClass::$leeway = 1; } return new $jwtClass; } private function getRsaClass() { if (class_exists('phpseclib\Crypt\RSA')) { return 'phpseclib\Crypt\RSA'; } return 'Crypt_RSA'; } private function getBigIntClass() { if (class_exists('phpseclib\Math\BigInteger')) { return 'phpseclib\Math\BigInteger'; } return 'Math_BigInteger'; } private function getOpenSslConstant() { if (class_exists('phpseclib\Crypt\RSA')) { return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; } if (class_exists('Crypt_RSA')) { return 'CRYPT_RSA_MODE_OPENSSL'; } throw new \Exception('Cannot find RSA class'); } /** * phpseclib calls "phpinfo" by default, which requires special * whitelisting in the AppEngine VM environment. This function * sets constants to bypass the need for phpseclib to check phpinfo * * @see phpseclib/Math/BigInteger * @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85 */ private function setPhpsecConstants() { if (filter_var(getenv('GAE_VM'), FILTER_VALIDATE_BOOLEAN)) { if (!defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); } if (!defined('CRYPT_RSA_MODE')) { define('CRYPT_RSA_MODE', constant($this->getOpenSslConstant())); } } } } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * A task runner with exponential backoff support. * * @see https://developers.google.com/drive/web/handle-errors#implementing_exponential_backoff */ class Google_Task_Runner { const TASK_RETRY_NEVER = 0; const TASK_RETRY_ONCE = 1; const TASK_RETRY_ALWAYS = -1; /** * @var integer $maxDelay The max time (in seconds) to wait before a retry. */ private $maxDelay = 60; /** * @var integer $delay The previous delay from which the next is calculated. */ private $delay = 1; /** * @var integer $factor The base number for the exponential back off. */ private $factor = 2; /** * @var float $jitter A random number between -$jitter and $jitter will be * added to $factor on each iteration to allow for a better distribution of * retries. */ private $jitter = 0.5; /** * @var integer $attempts The number of attempts that have been tried so far. */ private $attempts = 0; /** * @var integer $maxAttempts The max number of attempts allowed. */ private $maxAttempts = 1; /** * @var callable $action The task to run and possibly retry. */ private $action; /** * @var array $arguments The task arguments. */ private $arguments; /** * @var array $retryMap Map of errors with retry counts. */ protected $retryMap = [ '500' => self::TASK_RETRY_ALWAYS, '503' => self::TASK_RETRY_ALWAYS, 'rateLimitExceeded' => self::TASK_RETRY_ALWAYS, 'userRateLimitExceeded' => self::TASK_RETRY_ALWAYS, 6 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_RESOLVE_HOST 7 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_CONNECT 28 => self::TASK_RETRY_ALWAYS, // CURLE_OPERATION_TIMEOUTED 35 => self::TASK_RETRY_ALWAYS, // CURLE_SSL_CONNECT_ERROR 52 => self::TASK_RETRY_ALWAYS // CURLE_GOT_NOTHING ]; /** * Creates a new task runner with exponential backoff support. * * @param array $config The task runner config * @param string $name The name of the current task (used for logging) * @param callable $action The task to run and possibly retry * @param array $arguments The task arguments * @throws Google_Task_Exception when misconfigured */ public function __construct( $config, $name, $action, array $arguments = array() ) { if (isset($config['initial_delay'])) { if ($config['initial_delay'] < 0) { throw new Google_Task_Exception( 'Task configuration `initial_delay` must not be negative.' ); } $this->delay = $config['initial_delay']; } if (isset($config['max_delay'])) { if ($config['max_delay'] <= 0) { throw new Google_Task_Exception( 'Task configuration `max_delay` must be greater than 0.' ); } $this->maxDelay = $config['max_delay']; } if (isset($config['factor'])) { if ($config['factor'] <= 0) { throw new Google_Task_Exception( 'Task configuration `factor` must be greater than 0.' ); } $this->factor = $config['factor']; } if (isset($config['jitter'])) { if ($config['jitter'] <= 0) { throw new Google_Task_Exception( 'Task configuration `jitter` must be greater than 0.' ); } $this->jitter = $config['jitter']; } if (isset($config['retries'])) { if ($config['retries'] < 0) { throw new Google_Task_Exception( 'Task configuration `retries` must not be negative.' ); } $this->maxAttempts += $config['retries']; } if (!is_callable($action)) { throw new Google_Task_Exception( 'Task argument `$action` must be a valid callable.' ); } $this->action = $action; $this->arguments = $arguments; } /** * Checks if a retry can be attempted. * * @return boolean */ public function canAttempt() { return $this->attempts < $this->maxAttempts; } /** * Runs the task and (if applicable) automatically retries when errors occur. * * @return mixed * @throws Google_Task_Retryable on failure when no retries are available. */ public function run() { while ($this->attempt()) { try { return call_user_func_array($this->action, $this->arguments); } catch (Google_Service_Exception $exception) { $allowedRetries = $this->allowedRetries( $exception->getCode(), $exception->getErrors() ); if (!$this->canAttempt() || !$allowedRetries) { throw $exception; } if ($allowedRetries > 0) { $this->maxAttempts = min( $this->maxAttempts, $this->attempts + $allowedRetries ); } } } } /** * Runs a task once, if possible. This is useful for bypassing the `run()` * loop. * * NOTE: If this is not the first attempt, this function will sleep in * accordance to the backoff configurations before running the task. * * @return boolean */ public function attempt() { if (!$this->canAttempt()) { return false; } if ($this->attempts > 0) { $this->backOff(); } $this->attempts++; return true; } /** * Sleeps in accordance to the backoff configurations. */ private function backOff() { $delay = $this->getDelay(); usleep($delay * 1000000); } /** * Gets the delay (in seconds) for the current backoff period. * * @return float */ private function getDelay() { $jitter = $this->getJitter(); $factor = $this->attempts > 1 ? $this->factor + $jitter : 1 + abs($jitter); return $this->delay = min($this->maxDelay, $this->delay * $factor); } /** * Gets the current jitter (random number between -$this->jitter and * $this->jitter). * * @return float */ private function getJitter() { return $this->jitter * 2 * mt_rand() / mt_getrandmax() - $this->jitter; } /** * Gets the number of times the associated task can be retried. * * NOTE: -1 is returned if the task can be retried indefinitely * * @return integer */ public function allowedRetries($code, $errors = array()) { if (isset($this->retryMap[$code])) { return $this->retryMap[$code]; } if ( !empty($errors) && isset($errors[0]['reason'], $this->retryMap[$errors[0]['reason']]) ) { return $this->retryMap[$errors[0]['reason']]; } return 0; } public function setRetryMap($retryMap) { $this->retryMap = $retryMap; } } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ class Google_Task_Exception extends Google_Exception { } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Interface for checking how many times a given task can be retried following * a failure. */ interface Google_Task_Retryable { } # Pagination Most list API calls have a maximum limit of results they will return in a single response. To allow retrieving more than this number of results, responses may return a pagination token which can be passed with a request in order to access subsequent pages. The token for the page will normally be found on list response objects, normally `nextPageToken`. This can be passed in the optional params. ```php $token = $results->getNextPageToken(); $server->listActivities('me', 'public', array('pageToken' => $token)); ```# Standard Parameters Many API methods include support for certain optional parameters. In addition to these there are several standard parameters that can be applied to any API call. These are defined in the `Google_Service_Resource` class. ## Parameters - **alt**: Specify an alternative response type, for example csv. - **fields**: A comma separated list of fields that should be included in the response. Nested parameters can be specified with parens, e.g. key,parent(child/subsection). - **userIp**: The IP of the end-user making the request. This is used in per-user request quotas, as defined in the Google Developers Console - **quotaUser**: A user ID for the end user, an alternative to userIp for applying per-user request quotas - **data**: Used as part of [media](media.md) - **mimeType**: Used as part of [media](media.md) - **uploadType**: Used as part of [media](media.md) - **mediaUpload**: Used as part of [media](media.md)# Using OAuth 2.0 for Web Server Applications This document explains how web server applications use the Google API Client Library for PHP to implement OAuth 2.0 authorization to access Google APIs. OAuth 2.0 allows users to share specific data with an application while keeping their usernames, passwords, and other information private. For example, an application can use OAuth 2.0 to obtain permission from users to store files in their Google Drives. This OAuth 2.0 flow is specifically for user authorization. It is designed for applications that can store confidential information and maintain state. A properly authorized web server application can access an API while the user interacts with the application or after the user has left the application. Web server applications frequently also use [service accounts](oauth-server.md) to authorize API requests, particularly when calling Cloud APIs to access project-based data rather than user-specific data. Web server applications can use service accounts in conjunction with user authorization. ## Prerequisites ### Enable APIs for your project Any application that calls Google APIs needs to enable those APIs in the API Console. To enable the appropriate APIs for your project: 1. Open the [Library](https://console.developers.google.com/apis/library) page in the API Console. 2. Select the project associated with your application. Create a project if you do not have one already. 3. Use the **Library** page to find each API that your application will use. Click on each API and enable it for your project. ### Create authorization credentials Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project. 1. Open the [Credentials page](https://console.developers.google.com/apis/credentials) in the API Console. 2. Click **Create credentials > OAuth client ID**. 3. Complete the form. Set the application type to `Web application`. Applications that use languages and frameworks like PHP, Java, Python, Ruby, and .NET must specify authorized **redirect URIs**. The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses. For testing, you can specify URIs that refer to the local machine, such as `http://localhost:8080`. With that in mind, please note that all of the examples in this document use `http://localhost:8080` as the redirect URI. We recommend that you design your app's auth endpoints so that your application does not expose authorization codes to other resources on the page. After creating your credentials, download the **client_secret.json** file from the API Console. Securely store the file in a location that only your application can access. > **Important:** Do not store the **client_secret.json** file in a publicly-accessible location. In addition, if you share the source code to your application—for example, on GitHub—store the **client_secret.json** file outside of your source tree to avoid inadvertently sharing your client credentials. ### Identify access scopes Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there may be an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent. Before you start implementing OAuth 2.0 authorization, we recommend that you identify the scopes that your app will need permission to access. We also recommend that your application request access to authorization scopes via an [incremental authorization](#incremental-authorization) process, in which your application requests access to user data in context. This best practice helps users to more easily understand why your application needs the access it is requesting. The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document contains a full list of scopes that you might use to access Google APIs. > If your public application uses scopes that permit access to certain user data, it must pass review. If you see **unverified app** on the screen when testing your application, you must submit a verification request to remove it. Find out more about [unverified apps](https://support.google.com/cloud/answer/7454865) and get answers to [frequently asked questions about app verification](https://support.google.com/cloud/answer/9110914) in the Help Center. ### Language-specific requirements To run any of the code samples in this document, you'll need a Google account, access to the Internet, and a web browser. If you are using one of the API client libraries, also see the language-specific requirements below. To run the PHP code samples in this document, you'll need: * PHP 5.4 or greater with the command-line interface (CLI) and JSON extension installed. * The [Composer](https://getcomposer.org/) dependency management tool. * The Google APIs Client Library for PHP: ```sh php composer.phar require google/apiclient:^2.0 ``` ## Obtaining OAuth 2.0 access tokens The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's consent to perform an API request on the user's behalf. Your application must have that consent before it can execute a Google API request that requires user authorization. The list below quickly summarizes these steps: 1. Your application identifies the permissions it needs. 2. Your application redirects the user to Google along with the list of requested permissions. 3. The user decides whether to grant the permissions to your application. 4. Your application finds out what the user decided. 5. If the user granted the requested permissions, your application retrieves tokens needed to make API requests on the user's behalf. ### Step 1: Set authorization parameters Your first step is to create the authorization request. That request sets parameters that identify your application and define the permissions that the user will be asked to grant to your application. The code snippet below creates a `Google_Client()` object, which defines the parameters in the authorization request. That object uses information from your **client_secret.json** file to identify your application. The object also identifies the scopes that your application is requesting permission to access and the URL to your application's auth endpoint, which will handle the response from Google's OAuth 2.0 server. Finally, the code sets the optional access_type and include_granted_scopes parameters. For example, this code requests read-only, offline access to a user's Google Drive: ```php $client = new Google_Client(); $client->setAuthConfig('client_secret.json'); $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'); $client->setAccessType('offline'); // offline access $client->setIncludeGrantedScopes(true); // incremental auth ``` The request specifies the following information: #### Parameters ##### `client_id` **Required**. The client ID for your application. You can find this value in the [API Console](https://console.developers.google.com/). In PHP, call the `setAuthConfig` function to load authorization credentials from a **client_secret.json** file. ```php $client = new Google_Client(); $client->setAuthConfig('client_secret.json'); ``` ##### `redirect_uri` **Required**. Determines where the API server redirects the user after the user completes the authorization flow. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in the [API Console](https://console.developers.google.com/). If this value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch' error. Note that the `http` or `https` scheme, case, and trailing slash ('`/`') must all match. To set this value in PHP, call the `setRedirectUri` function. Note that you must specify a valid redirect URI for your API Console project. ```php $client->setRedirectUri('http://localhost:8080/oauth2callback.php'); ``` ##### `scope` **Required**. A space-delimited list of scopes that identify the resources that your application could access on the user's behalf. These values inform the consent screen that Google displays to the user. Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there is an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent. To set this value in PHP, call the `addScope` function: ```php $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); ``` The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document provides a full list of scopes that you might use to access Google APIs. We recommend that your application request access to authorization scopes in context whenever possible. By requesting access to user data in context, via [incremental authorization](#Incremental-authorization), you help users to more easily understand why your application needs the access it is requesting. ##### `access_type` **Recommended**. Indicates whether your application can refresh access tokens when the user is not present at the browser. Valid parameter values are `online`, which is the default value, and `offline`. Set the value to `offline` if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token _and_ an access token the first time that your application exchanges an authorization code for tokens. To set this value in PHP, call the `setAccessType` function: ```php $client->setAccessType('offline'); ``` ##### `state` **Recommended**. Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a `name=value` pair in the hash (`#`) fragment of the `redirect_uri` after the user consents to or denies your application's access request. You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your `redirect_uri` can be guessed, using a `state` value can increase your assurance that an incoming connection is the result of an authentication request. If you generate a random string or encode the hash of a cookie or another value that captures the client's state, you can validate the response to additionally ensure that the request and response originated in the same browser, providing protection against attacks such as cross-site request forgery. See the [OpenID Connect](https://developers.google.com/identity/protocols/OpenIDConnect#createxsrftoken) documentation for an example of how to create and confirm a `state` token. To set this value in PHP, call the `setState` function: ```php $client->setState($sample_passthrough_value); ``` ##### `include_granted_scopes` **Optional**. Enables applications to use incremental authorization to request access to additional scopes in context. If you set this parameter's value to `true` and the authorization request is granted, then the new access token will also cover any scopes to which the user previously granted the application access. See the [incremental authorization](#Incremental-authorization) section for examples. To set this value in PHP, call the `setIncludeGrantedScopes` function: ```php $client->setIncludeGrantedScopes(true); ``` ##### `login_hint` **Optional**. If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session. Set the parameter value to an email address or `sub` identifier, which is equivalent to the user's Google ID. To set this value in PHP, call the `setLoginHint` function: ```php $client->setLoginHint('timmerman@google.com'); ``` ##### `prompt` **Optional**. A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your app requests access. To set this value in PHP, call the `setApprovalPrompt` function: ```php $client->setApprovalPrompt('consent'); ``` Possible values are: `none` Do not display any authentication or consent screens. Must not be specified with other values. `consent` Prompt the user for consent. `select_account` Prompt the user to select an account. ### Step 2: Redirect to Google's OAuth 2.0 server Redirect the user to Google's OAuth 2.0 server to initiate the authentication and authorization process. Typically, this occurs when your application first needs to access the user's data. In the case of [incremental authorization](#incremental-authorization), this step also occurs when your application first needs to access additional resources that it does not yet have permission to access. 1. Generate a URL to request access from Google's OAuth 2.0 server: ```php $auth_url = $client->createAuthUrl(); ``` 2. Redirect the user to `$auth_url`: ```php header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); ``` Google's OAuth 2.0 server authenticates the user and obtains consent from the user for your application to access the requested scopes. The response is sent back to your application using the redirect URL you specified. ### Step 3: Google prompts user for consent In this step, the user decides whether to grant your application the requested access. At this stage, Google displays a consent window that shows the name of your application and the Google API services that it is requesting permission to access with the user's authorization credentials. The user can then consent or refuse to grant access to your application. Your application doesn't need to do anything at this stage as it waits for the response from Google's OAuth 2.0 server indicating whether the access was granted. That response is explained in the following step. ### Step 4: Handle the OAuth 2.0 server response The OAuth 2.0 server responds to your application's access request by using the URL specified in the request. If the user approves the access request, then the response contains an authorization code. If the user does not approve the request, the response contains an error message. The authorization code or error message that is returned to the web server appears on the query string, as shown below: An error response: https://oauth2.example.com/auth?error=access_denied An authorization code response: https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7 > **Important**: If your response endpoint renders an HTML page, any resources on that page will be able to see the authorization code in the URL. Scripts can read the URL directly, and the URL in the `Referer` HTTP header may be sent to any or all resources on the page. > > Carefully consider whether you want to send authorization credentials to all resources on that page (especially third-party scripts such as social plugins and analytics). To avoid this issue, we recommend that the server first handle the request, then redirect to another URL that doesn't include the response parameters. #### Sample OAuth 2.0 server response You can test this flow by clicking on the following sample URL, which requests read-only access to view metadata for files in your Google Drive: ``` https://accounts.google.com/o/oauth2/v2/auth?  scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&  access_type=offline&  include_granted_scopes=true&  state=state_parameter_passthrough_value&  redirect_uri=http%3A%2F%2Foauth2.example.com%2Fcallback&  response_type=code&  client_id=client_id ``` After completing the OAuth 2.0 flow, you should be redirected to `http://localhost/oauth2callback`, which will likely yield a `404 NOT FOUND` error unless your local machine serves a file at that address. The next step provides more detail about the information returned in the URI when the user is redirected back to your application. ### Step 5: Exchange authorization code for refresh and access tokens After the web server receives the authorization code, it can exchange the authorization code for an access token. To exchange an authorization code for an access token, use the `authenticate` method: ```php $client->authenticate($_GET['code']); ``` You can retrieve the access token with the `getAccessToken` method: ```php $access_token = $client->getAccessToken(); ``` [](#top_of_page)Calling Google APIs ----------------------------------- Use the access token to call Google APIs by completing the following steps: 1. If you need to apply an access token to a new `Google_Client` object—for example, if you stored the access token in a user session—use the `setAccessToken` method: ```php $client->setAccessToken($access_token); ``` 2. Build a service object for the API that you want to call. You build a a service object by providing an authorized `Google_Client` object to the constructor for the API you want to call. For example, to call the Drive API: ```php $drive = new Google_Service_Drive($client); ``` 3. Make requests to the API service using the [interface provided by the service object](start.md). For example, to list the files in the authenticated user's Google Drive: ```php $files = $drive->files->listFiles(array())->getItems(); ``` [](#top_of_page)Complete example -------------------------------- The following example prints a JSON-formatted list of files in a user's Google Drive after the user authenticates and gives consent for the application to access the user's Drive files. To run this example: 1. In the API Console, add the URL of the local machine to the list of redirect URLs. For example, add `http://localhost:8080`. 2. Create a new directory and change to it. For example: ```sh mkdir ~/php-oauth2-example cd ~/php-oauth2-example ``` 3. Install the [Google API Client Library](https://github.com/google/google-api-php-client) for PHP using [Composer](https://getcomposer.org): ```sh composer require google/apiclient:^2.0 ``` 4. Create the files `index.php` and `oauth2callback.php` with the content below. 5. Run the example with a web server configured to serve PHP. If you use PHP 5.4 or newer, you can use PHP's built-in test web server: ```sh php -S localhost:8080 ~/php-oauth2-example ``` #### index.php ```php <?php require_once __DIR__.'/vendor/autoload.php'; session_start(); $client = new Google_Client(); $client->setAuthConfig('client_secrets.json'); $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { $client->setAccessToken($_SESSION['access_token']); $drive = new Google_Service_Drive($client); $files = $drive->files->listFiles(array())->getItems(); echo json_encode($files); } else { $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'; header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); } ``` #### oauth2callback.php ```php <?php require_once __DIR__.'/vendor/autoload.php'; session_start(); $client = new Google_Client(); $client->setAuthConfigFile('client_secrets.json'); $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'); $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); if (! isset($_GET['code'])) { $auth_url = $client->createAuthUrl(); header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); } else { $client->authenticate($_GET['code']); $_SESSION['access_token'] = $client->getAccessToken(); $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/'; header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); } ``` ## Incremental authorization In the OAuth 2.0 protocol, your app requests authorization to access resources, which are identified by scopes. It is considered a best user-experience practice to request authorization for resources at the time you need them. To enable that practice, Google's authorization server supports incremental authorization. This feature lets you request scopes as they are needed and, if the user grants permission, add those scopes to your existing access token for that user. For example, an app that lets people sample music tracks and create mixes might need very few resources at sign-in time, perhaps nothing more than the name of the person signing in. However, saving a completed mix would require access to their Google Drive. Most people would find it natural if they only were asked for access to their Google Drive at the time the app actually needed it. In this case, at sign-in time the app might request the `profile` scope to perform basic sign-in, and then later request the `https://www.googleapis.com/auth/drive.file` scope at the time of the first request to save a mix. To implement incremental authorization, you complete the normal flow for requesting an access token but make sure that the authorization request includes previously granted scopes. This approach allows your app to avoid having to manage multiple access tokens. The following rules apply to an access token obtained from an incremental authorization: * The token can be used to access resources corresponding to any of the scopes rolled into the new, combined authorization. * When you use the refresh token for the combined authorization to obtain an access token, the access token represents the combined authorization and can be used for any of its scopes. * The combined authorization includes all scopes that the user granted to the API project even if the grants were requested from different clients. For example, if a user granted access to one scope using an application's desktop client and then granted another scope to the same application via a mobile client, the combined authorization would include both scopes. * If you revoke a token that represents a combined authorization, access to all of that authorization's scopes on behalf of the associated user are revoked simultaneously. The example for [setting authorization parameters](#Step-1-Set-authorization-parameters) demonstrates how to ensure authorization requests follow this best practice. The code snippet below also shows the code that you need to add to use incremental authorization. ```php $client->setIncludeGrantedScopes(true); ``` ## Refreshing an access token (offline access) Access tokens periodically expire. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token. If you use a Google API Client Library, the [client object](#Step-1-Set-authorization-parameters) refreshes the access token as needed as long as you configure that object for offline access. Requesting offline access is a requirement for any application that needs to access a Google API when the user is not present. For example, an app that performs backup services or executes actions at predetermined times needs to be able to refresh its access token when the user is not present. The default style of access is called `online`. Server-side web applications, installed applications, and devices all obtain refresh tokens during the authorization process. Refresh tokens are not typically used in client-side (JavaScript) web applications. If your application needs offline access to a Google API, set the API client's access type to `offline`: ```php $client->setAccessType("offline"); ``` After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed. ## Revoking a token In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting [Account Settings](https://security.google.com/settings/security/permissions). It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes or removes an application. In other words, part of the removal process can include an API request to ensure the permissions granted to the application are removed. To programmatically revoke a token, call `revokeToken()`: ```php $client->revokeToken(); ``` **Note:** Following a successful revocation response, it might take some time before the revocation has full effect. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see our [Site Policies](https://developers.google.com/terms/site-policies). Java is a registered trademark of Oracle and/or its affiliates.# Getting Started This document provides all the basic information you need to start using the library. It covers important library concepts, shows examples for various use cases, and gives links to more information. ## Setup There are a few setup steps you need to complete before you can use this library: 1. If you don't already have a Google account, [sign up](https://www.google.com/accounts). 2. If you have never created a Google API project, read the [Managing Projects page](https://developers.google.com/console/help/#managingprojects) and create a project in the [Google Developers Console](https://console.developers.google.com/) 3. [Install](install.md) the library. ## Authentication and authorization It is important to understand the basics of how API authentication and authorization are handled. All API calls must use either simple or authorized access (defined below). Many API methods require authorized access, but some can use either. Some API methods that can use either behave differently, depending on whether you use simple or authorized access. See the API's method documentation to determine the appropriate access type. ### 1. Simple API access (API keys) These API calls do not access any private user data. Your application must authenticate itself as an application belonging to your Google Cloud project. This is needed to measure project usage for accounting purposes. #### Important concepts * **API key**: To authenticate your application, use an [API key](https://cloud.google.com/docs/authentication/api-keys) for your Google Cloud Console project. Every simple access call your application makes must include this key. > **Warning**: Keep your API key private. If someone obtains your key, they could use it to consume your quota or incur charges against your Google Cloud project. ### 2. Authorized API access (OAuth 2.0) These API calls access private user data. Before you can call them, the user that has access to the private data must grant your application access. Therefore, your application must be authenticated, the user must grant access for your application, and the user must be authenticated in order to grant that access. All of this is accomplished with [OAuth 2.0](https://developers.google.com/identity/protocols/OAuth2) and libraries written for it. #### Important concepts * **Scope**: Each API defines one or more scopes that declare a set of operations permitted. For example, an API might have read-only and read-write scopes. When your application requests access to user data, the request must include one or more scopes. The user needs to approve the scope of access your application is requesting. * **Refresh and access tokens**: When a user grants your application access, the OAuth 2.0 authorization server provides your application with refresh and access tokens. These tokens are only valid for the scope requested. Your application uses access tokens to authorize API calls. Access tokens expire, but refresh tokens do not. Your application can use a refresh token to acquire a new access token. > **Warning**: Keep refresh and access tokens private. If someone obtains your tokens, they could use them to access private user data. * **Client ID and client secret**: These strings uniquely identify your application and are used to acquire tokens. They are created for your Google Cloud project on the [API Access pane](https://code.google.com/apis/console#:access) of the Google Cloud. There are three types of client IDs, so be sure to get the correct type for your application: * Web application client IDs * Installed application client IDs * [Service Account](https://developers.google.com/identity/protocols/OAuth2ServiceAccount) client IDs > **Warning**: Keep your client secret private. If someone obtains your client secret, they could use it to consume your quota, incur charges against your Google Cloud project, and request access to user data. ## Building and calling a service This section described how to build an API-specific service object, make calls to the service, and process the response. ### Build the client object The client object is the primary container for classes and configuration in the library. ```php $client = new Google_Client(); $client->setApplicationName("My Application"); $client->setDeveloperKey("MY_SIMPLE_API_KEY"); ``` ### Build the service object Services are called through queries to service specific objects. These are created by constructing the service object, and passing an instance of `Google_Client` to it. `Google_Client` contains the IO, authentication and other classes required by the service to function, and the service informs the client which scopes it uses to provide a default when authenticating a user. ```php $service = new Google_Service_Books($client); ``` ### Calling an API Each API provides resources and methods, usually in a chain. These can be accessed from the service object in the form `$service->resource->method(args)`. Most method require some arguments, then accept a final parameter of an array containing optional parameters. For example, with the Google Books API, we can make a call to list volumes matching a certain string, and add an optional _filter_ parameter. ```php $optParams = array('filter' => 'free-ebooks'); $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); ``` ### Handling the result There are two main types of response - items and collections of items. Each can be accessed either as an object or as an array. Collections implement the `Iterator` interface so can be used in foreach and other constructs. ```php foreach ($results as $item) { echo $item['volumeInfo']['title'], "<br /> \n"; } ``` ## Google App Engine support This library works well with Google App Engine applications. The Memcache class is automatically used for caching, and the file IO is implemented with the use of the Streams API.# Using OAuth 2.0 for Server to Server Applications The Google APIs Client Library for PHP supports using OAuth 2.0 for server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. This scenario is sometimes called "two-legged OAuth," or "2LO." (The related term "three-legged OAuth" refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.) Typically, an application uses a service account when the application uses Google APIs to work with its own data rather than a user's data. For example, an application that uses [Google Cloud Datastore](https://cloud.google.com/datastore/) for data persistence would use a service account to authenticate its calls to the Google Cloud Datastore API. If you have a G Suite domain—if you use [G Suite Business](https://gsuite.google.com), for example—an administrator of the G Suite domain can authorize an application to access user data on behalf of users in the G Suite domain. For example, an application that uses the [Google Calendar API](https://developers.google.com/google-apps/calendar/) to add events to the calendars of all users in a G Suite domain would use a service account to access the Google Calendar API on behalf of users. Authorizing a service account to access data on behalf of users in a domain is sometimes referred to as "delegating domain-wide authority" to a service account. > **Note:** When you use [G Suite Marketplace](https://www.google.com/enterprise/marketplace/) to install an application for your domain, the required permissions are automatically granted to the application. You do not need to manually authorize the service accounts that the application uses. > **Note:** Although you can use service accounts in applications that run from a G Suite domain, service accounts are not members of your G Suite account and aren't subject to domain policies set by G Suite administrators. For example, a policy set in the G Suite admin console to restrict the ability of Apps end users to share documents outside of the domain would not apply to service accounts. This document describes how an application can complete the server-to-server OAuth 2.0 flow by using the Google APIs Client Library for PHP. ## Overview To support server-to-server interactions, first create a service account for your project in the Developers Console. If you want to access user data for users in your G Suite domain, then delegate domain-wide access to the service account. Then, your application prepares to make authorized API calls by using the service account's credentials to request an access token from the OAuth 2.0 auth server. Finally, your application can use the access token to call Google APIs. ## Creating a service account A service account's credentials include a generated email address that is unique, a client ID, and at least one public/private key pair. If your application runs on Google App Engine, a service account is set up automatically when you create your project. If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google Developers Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following: If your application runs on Google App Engine, a service account is set up automatically when you create your project. If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google Developers Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following: 1. Open the [**Service accounts** section](https://console.developers.google.com/permissions/serviceaccounts?project=_) of the Developers Console's **Permissions** page. 2. Click **Create service account**. 3. In the **Create service account** window, type a name for the service account and select **Furnish a new private key**. If you want to [grant G Suite domain-wide authority](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority) to the service account, also select **Enable G Suite Domain-wide Delegation**. Then, click **Create**. Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely. You can return to the [Developers Console](https://console.developers.google.com/) at any time to view the client ID, email address, and public key fingerprints, or to generate additional public/private key pairs. For more details about service account credentials in the Developers Console, see [Service accounts](https://developers.google.com/console/help/service-accounts) in the Developers Console help file. Take note of the service account's email address and store the service account's private key file in a location accessible to your application. Your application needs them to make authorized API calls. **Note:** You must store and manage private keys securely in both development and production environments. Google does not keep a copy of your private keys, only your public keys. ### Delegating domain-wide authority to the service account If your application runs in a G Suite domain and accesses user data, the service account that you created needs to be granted access to the user data that you want to access. The following steps must be performed by an administrator of the G Suite domain: 1. Go to your G Suite domain’s [Admin console](http://admin.google.com). 2. Select **Security** from the list of controls. If you don't see **Security** listed, select **More controls** from the gray bar at the bottom of the page, then select **Security** from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain. 3. Select **Advanced settings** from the list of options. 4. Select **Manage third party OAuth Client access** in the **Authentication** section. 5. In the **Client name** field enter the service account's **Client ID**. 6. In the **One or More API Scopes** field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar. 7. Click **Authorize**. Your application now has the authority to make API calls as users in your domain (to "impersonate" users). When you prepare to make authorized API calls, you specify the user to impersonate. [](#top_of_page)Preparing to make an authorized API call -------------------------------------------------------- After you have obtained the client email address and private key from the Developers Console, set the path to these credentials in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable ( **Note:** This is not required in the App Engine environment): ```php putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); ``` Call the `useApplicationDefaultCredentials` to use your service account credentials to authenticate: ```php $client = new Google_Client(); $client->useApplicationDefaultCredentials(); ``` If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method `setSubject`: ```php $client->setSubject($user_to_impersonate); ``` Use the authorized `Google_Client` object to call Google APIs in your application. ## Calling Google APIs Use the authorized `Google_Client` object to call Google APIs by completing the following steps: 1. Build a service object for the API that you want to call, providing the authorized `Google_Client` object. For example, to call the Cloud SQL Administration API: ```php $sqladmin = new Google_Service_SQLAdmin($client); ``` 2. Make requests to the API service using the [interface provided by the service object](https://github.com/googleapis/google-api-php-client/blob/master/docs/start.md#build-the-service-object). For example, to list the instances of Cloud SQL databases in the examinable-example-123 project: ```php $response = $sqladmin->instances->listInstances('examinable-example-123')->getItems(); ``` ## Complete example The following example prints a JSON-formatted list of Cloud SQL instances in a project. To run this example: 1. Create a new directory and change to it. For example: ```sh mkdir ~/php-oauth2-example cd ~/php-oauth2-example ``` 2. Install the [Google API Client Library](https://github.com/google/google-api-php-client) for PHP using [Composer](https://getcomposer.org): ```sh composer require google/apiclient:^2.0 ``` 3. Create the file sqlinstances.php with the content below. 4. Run the example from the command line: ``` php ~/php-oauth2-example/sqlinstances.php ``` ### sqlinstances.php ```php <?php require_once __DIR__.'/vendor/autoload.php'; putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); $client = new Google_Client(); $client->useApplicationDefaultCredentials(); $sqladmin = new Google_Service_SQLAdmin($client); $response = $sqladmin->instances ->listInstances('examinable-example-123')->getItems(); echo json_encode($response) . "\n"; ``` # Installation This page contains information about installing the Google APIs Client Library for PHP. ## Requirements * PHP version 5.4 or greater. ## Obtaining the client library There are two options for obtaining the files for the client library. ### Using Composer You can install the library by adding it as a dependency to your composer.json. ```json "require": { "google/apiclient": "^2.0" } ``` ### Downloading from GitHub Follow [the instructions in the README](https://github.com/google/google-api-php-client#download-the-release) to download the package locally. ### What to do with the files After obtaining the files, include the autloader. If you used Composer, your require statement will look like this: ```php require_once '/path/to/your-project/vendor/autoload.php'; ``` If you downloaded the package separately, your require statement will look like this: ```php require_once '/path/to/google-api-php-client/vendor/autoload.php'; ```# API Keys When calling APIs that do not access private user data, you can use simple API keys. These keys are used to authenticate your application for accounting purposes. The Google Developers Console documentation also describes [API keys](https://developers.google.com/console/help/using-keys). > Note: If you do need to access private user data, you must use OAuth 2.0. See [Using OAuth 2.0 for Web Server Applications](/docs/oauth-web.md) and [Using OAuth 2.0 for Server to Server Applications](/docs/oauth-server.md) for more information. ## Using API Keys To use API keys, call the `setDeveloperKey()` method of the `Google_Client` object before making any API calls. For example: ```php $client->setDeveloperKey($api_key); ``` # Authorization The Google PHP Client Library supports several methods for making authenticated calls to the Google APIs. - [API Keys](api-keys.md) - [OAuth 2.0 For Webservers](oauth-web.md) - [OAuth 2.0 Service Accounts](oauth-server.md) In addition, it supports a method of identifying users without granting access to make Google API calls. - [ID Token Verification](id-token.md)# Media Upload The PHP client library allows for uploading large files for use with APIs such as Drive or YouTube. There are three different methods available. ## Simple Upload In the simple upload case, the data is passed as the body of the request made to the server. This limits the ability to specify metadata, but is very easy to use. ```php $file = new Google_Service_Drive_DriveFile(); $result = $service->files->insert($file, array( 'data' => file_get_contents("path/to/file"), 'mimeType' => 'application/octet-stream', 'uploadType' => 'media' )); ``` ## Multipart File Upload With multipart file uploads, the uploaded file is sent as one part of a multipart form post. This allows metadata about the file object to be sent as part of the post as well. This is triggered by specifying the _multipart_ uploadType. ```php $file = new Google_Service_Drive_DriveFile(); $file->setTitle("Hello World!"); $result = $service->files->insert($file, array( 'data' => file_get_contents("path/to/file"), 'mimeType' => 'application/octet-stream', 'uploadType' => 'multipart' )); ``` ## Resumable File Upload It is also possible to split the upload across multiple requests. This is convenient for larger files, and allows resumption of the upload if there is a problem. Resumable uploads can be sent with separate metadata. ```php $file = new Google_Service_Drive_DriveFile(); $file->title = "Big File"; $chunkSizeBytes = 1 * 1024 * 1024; // Call the API with the media upload, defer so it doesn't immediately return. $client->setDefer(true); $request = $service->files->insert($file); // Create a media file upload to represent our upload process. $media = new Google_Http_MediaFileUpload( $client, $request, 'text/plain', null, true, $chunkSizeBytes ); $media->setFileSize(filesize("path/to/file")); // Upload the various chunks. $status will be false until the process is // complete. $status = false; $handle = fopen("path/to/file", "rb"); while (!$status && !feof($handle)) { $chunk = fread($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk); } // The final value of $status will be the data from the API for the object // that has been uploaded. $result = false; if($status != false) { $result = $status; } fclose($handle); // Reset to the client to execute requests immediately in the future. $client->setDefer(false); ```# ID Token Authentication ID tokens allow authenticating a user securely without requiring a network call (in many cases), and without granting the server access to request user information from the Google APIs. > For a complete example, see the [idtoken.php](https://github.com/googleapis/google-api-php-client/blob/master/examples/idtoken.php) sample in the examples/ directory of the client library. This is accomplished because each ID token is a cryptographically signed, base64 encoded JSON structure. The token payload includes the Google user ID, the client ID of the application the user signed in to, and the issuer (in this case, Google). It also contains a cryptographic signature which can be verified with the public Google certificates to ensure that the token was created by Google. If the user has granted permission to view their email address to the application, the ID token will additionally include their email address. The token can be easily and securely verified with the PHP client library ```php function getUserFromToken($token) { $ticket = $client->verifyIdToken($token); if ($ticket) { $data = $ticket->getAttributes(); return $data['payload']['sub']; // user ID } return false } ``` The library will automatically download and cache the certificate required for verification, and refresh it if it has expired. # Google API Client LIbrary for PHP Docs The Google API Client Library for PHP offers simple, flexible access to many Google APIs. ## Documentation - [Getting Started](start.md) - [API Keys](api-keys.md) - [Auth](auth.md) - [Installation](install.md) - [Media](media.md) - [OAuth Server](oauth-server.md) - [OAuth Web](oauth-web.md) - [Pagination](pagination.md) - [Parameters](parameters.md){ "name": "google/apiclient", "type": "library", "description": "Client library for Google APIs", "keywords": ["google"], "homepage": "http://developers.google.com/api-client-library/php", "license": "Apache-2.0", "require": { "php": ">=5.4", "google/auth": "^1.0", "google/apiclient-services": "~0.13", "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", "monolog/monolog": "^1.17|^2.0", "phpseclib/phpseclib": "~0.3.10||~2.0", "guzzlehttp/guzzle": "~5.3.1||~6.0", "guzzlehttp/psr7": "^1.2" }, "require-dev": { "phpunit/phpunit": "~4.8.36", "squizlabs/php_codesniffer": "~2.3", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", "cache/filesystem-adapter": "^0.3.2", "phpcompatibility/php-compatibility": "^9.2", "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0" }, "suggest": { "cache/filesystem-adapter": "For caching certs and tokens (using Google_Client::setCache)" }, "autoload": { "psr-0": { "Google_": "src/" }, "classmap": [ "src/Google/Service/" ] }, "extra": { "branch-alias": { "dev-master": "2.x-dev" } } } # Contributor Code of Conduct As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery * Personal attacks * Trolling or insulting/derogatory comments * Public or private harassment * Publishing other's private information, such as physical or electronic addresses, without explicit permission * Other unethical or unprofessional conduct. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) [](https://travis-ci.org/googleapis/google-api-php-client) # Google APIs Client Library for PHP # The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server. These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features. **NOTE** The actively maintained (v2) version of this client requires PHP 5.4 or above. If you require support for PHP 5.2 or 5.3, use the v1 branch. ## Google Cloud Platform For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we recommend using [GoogleCloudPlatform/google-cloud-php](https://github.com/googleapis/google-cloud-php) which is under active development. ## Requirements ## * [PHP 5.4.0 or higher](https://www.php.net/) ## Developer Documentation ## The [docs folder](docs/) provides detailed guides for using this library. ## Installation ## You can use **Composer** or simply **Download the Release** ### Composer The preferred method is via [composer](https://getcomposer.org/). Follow the [installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have composer installed. Once composer is installed, execute the following command in your project root to install this library: ```sh composer require google/apiclient:"^2.0" ``` Finally, be sure to include the autoloader: ```php require_once '/path/to/your-project/vendor/autoload.php'; ``` ### Download the Release If you prefer not to use composer, you can download the package in its entirety. The [Releases](https://github.com/googleapis/google-api-php-client/releases) page lists all stable versions. Download any file with the name `google-api-php-client-[RELEASE_NAME].zip` for a package including this library and its dependencies. Uncompress the zip file you download, and include the autoloader in your project: ```php require_once '/path/to/google-api-php-client/vendor/autoload.php'; ``` For additional installation and setup instructions, see [the documentation](docs/). ## Examples ## See the [`examples/`](examples) directory for examples of the key client features. You can view them in your browser by running the php built-in web server. ``` $ php -S localhost:8000 -t examples/ ``` And then browsing to the host and port you specified (in the above example, `http://localhost:8000`). ### Basic Example ### ```php // include your composer dependencies require_once 'vendor/autoload.php'; $client = new Google_Client(); $client->setApplicationName("Client_Library_Examples"); $client->setDeveloperKey("YOUR_APP_KEY"); $service = new Google_Service_Books($client); $optParams = array('filter' => 'free-ebooks'); $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); foreach ($results as $item) { echo $item['volumeInfo']['title'], "<br /> \n"; } ``` ### Authentication with OAuth ### > An example of this can be seen in [`examples/simple-file-upload.php`](examples/simple-file-upload.php). 1. Follow the instructions to [Create Web Application Credentials](docs/oauth-web.md#create-authorization-credentials) 1. Download the JSON credentials 1. Set the path to these credentials using `Google_Client::setAuthConfig`: ```php $client = new Google_Client(); $client->setAuthConfig('/path/to/client_credentials.json'); ``` 1. Set the scopes required for the API you are going to call ```php $client->addScope(Google_Service_Drive::DRIVE); ``` 1. Set your application's redirect URI ```php // Your redirect URI can be any registered URI, but in this example // we redirect back to this same page $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; $client->setRedirectUri($redirect_uri); ``` 1. In the script handling the redirect URI, exchange the authorization code for an access token: ```php if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); } ``` ### Authentication with Service Accounts ### > An example of this can be seen in [`examples/service-account.php`](examples/service-account.php). Some APIs (such as the [YouTube Data API](https://developers.google.com/youtube/v3/)) do not support service accounts. Check with the specific API documentation if API calls return unexpected 401 or 403 errors. 1. Follow the instructions to [Create a Service Account](docs/oauth-server.md#creating-a-service-account) 1. Download the JSON credentials 1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable: ```php putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); ``` 1. Tell the Google client to use your service account credentials to authenticate: ```php $client = new Google_Client(); $client->useApplicationDefaultCredentials(); ``` 1. Set the scopes required for the API you are going to call ```php $client->addScope(Google_Service_Drive::DRIVE); ``` 1. If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method setSubject: ```php $client->setSubject($user_to_impersonate); ``` ### Making Requests ### The classes used to call the API in [google-api-php-client-services](https://github.com/googleapis/google-api-php-client-services) are autogenerated. They map directly to the JSON requests and responses found in the [APIs Explorer](https://developers.google.com/apis-explorer/#p/). A JSON request to the [Datastore API](https://developers.google.com/apis-explorer/#p/datastore/v1beta3/datastore.projects.runQuery) would look like this: ```json POST https://datastore.googleapis.com/v1beta3/projects/YOUR_PROJECT_ID:runQuery?key=YOUR_API_KEY { "query": { "kind": [{ "name": "Book" }], "order": [{ "property": { "name": "title" }, "direction": "descending" }], "limit": 10 } } ``` Using this library, the same call would look something like this: ```php // create the datastore service class $datastore = new Google_Service_Datastore($client); // build the query - this maps directly to the JSON $query = new Google_Service_Datastore_Query([ 'kind' => [ [ 'name' => 'Book', ], ], 'order' => [ 'property' => [ 'name' => 'title', ], 'direction' => 'descending', ], 'limit' => 10, ]); // build the request and response $request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]); $response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request); ``` However, as each property of the JSON API has a corresponding generated class, the above code could also be written like this: ```php // create the datastore service class $datastore = new Google_Service_Datastore($client); // build the query $request = new Google_Service_Datastore_RunQueryRequest(); $query = new Google_Service_Datastore_Query(); // - set the order $order = new Google_Service_Datastore_PropertyOrder(); $order->setDirection('descending'); $property = new Google_Service_Datastore_PropertyReference(); $property->setName('title'); $order->setProperty($property); $query->setOrder([$order]); // - set the kinds $kind = new Google_Service_Datastore_KindExpression(); $kind->setName('Book'); $query->setKinds([$kind]); // - set the limit $query->setLimit(10); // add the query to the request and make the request $request->setQuery($query); $response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request); ``` The method used is a matter of preference, but *it will be very difficult to use this library without first understanding the JSON syntax for the API*, so it is recommended to look at the [APIs Explorer](https://developers.google.com/apis-explorer/#p/) before using any of the services here. ### Making HTTP Requests Directly ### If Google Authentication is desired for external applications, or a Google API is not available yet in this library, HTTP requests can be made directly. The `authorize` method returns an authorized [Guzzle Client](http://docs.guzzlephp.org/), so any request made using the client will contain the corresponding authorization. ```php // create the Google client $client = new Google_Client(); /** * Set your method for authentication. Depending on the API, This could be * directly with an access token, API key, or (recommended) using * Application Default Credentials. */ $client->useApplicationDefaultCredentials(); $client->addScope(Google_Service_Plus::PLUS_ME); // returns a Guzzle HTTP Client $httpClient = $client->authorize(); // make an HTTP request $response = $httpClient->get('https://www.googleapis.com/plus/v1/people/me'); ``` ### Caching ### It is recommended to use another caching library to improve performance. This can be done by passing a [PSR-6](https://www.php-fig.org/psr/psr-6/) compatible library to the client: ```php use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; use Cache\Adapter\Filesystem\FilesystemCachePool; $filesystemAdapter = new Local(__DIR__.'/'); $filesystem = new Filesystem($filesystemAdapter); $cache = new FilesystemCachePool($filesystem); $client->setCache($cache); ``` In this example we use [PHP Cache](http://www.php-cache.com/). Add this to your project with composer: ``` composer require cache/filesystem-adapter ``` ### Updating Tokens ### When using [Refresh Tokens](https://developers.google.com/identity/protocols/OAuth2InstalledApp#offline) or [Service Account Credentials](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#overview), it may be useful to perform some action when a new access token is granted. To do this, pass a callable to the `setTokenCallback` method on the client: ```php $logger = new Monolog\Logger; $tokenCallback = function ($cacheKey, $accessToken) use ($logger) { $logger->debug(sprintf('new access token received at cache key %s', $cacheKey)); }; $client->setTokenCallback($tokenCallback); ``` ### Debugging Your HTTP Request using Charles ### It is often very useful to debug your API calls by viewing the raw HTTP request. This library supports the use of [Charles Web Proxy](https://www.charlesproxy.com/documentation/getting-started/). Download and run Charles, and then capture all HTTP traffic through Charles with the following code: ```php // FOR DEBUGGING ONLY $httpClient = new GuzzleHttp\Client([ 'proxy' => 'localhost:8888', // by default, Charles runs on localhost port 8888 'verify' => false, // otherwise HTTPS requests will fail. ]); $client = new Google_Client(); $client->setHttpClient($httpClient); ``` Now all calls made by this library will appear in the Charles UI. One additional step is required in Charles to view SSL requests. Go to **Charles > Proxy > SSL Proxying Settings** and add the domain you'd like captured. In the case of the Google APIs, this is usually `*.googleapis.com`. ### Controlling HTTP Client Configuration Directly Google API Client uses [Guzzle](http://docs.guzzlephp.org/) as its default HTTP client. That means that you can control your HTTP requests in the same manner you would for any application using Guzzle. Let's say, for instance, we wished to apply a referrer to each request. ```php use GuzzleHttp\Client; $httpClient = new Client([ 'headers' => [ 'referer' => 'mysite.com' ] ]); $client = new Google_Client(); $client->setHttpClient($httpClient); ``` Other Guzzle features such as [Handlers and Middleware](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html) offer even more control. ### Service Specific Examples ### YouTube: https://github.com/youtube/api-samples/tree/master/php ## How Do I Contribute? ## Please see the [contributing](.github/CONTRIBUTING.md) page for more information. In particular, we love pull requests - but please make sure to sign the contributor license agreement. ## Frequently Asked Questions ## ### What do I do if something isn't working? ### For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: https://stackoverflow.com/questions/tagged/google-api-php-client If there is a specific bug with the library, please [file an issue](https://github.com/googleapis/google-api-php-client/issues) in the GitHub issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address. ### I want an example of X! ### If X is a feature of the library, file away! If X is an example of using a specific service, the best place to go is to the teams for those specific APIs - our preference is to link to their examples rather than add them to the library, as they can then pin to specific versions of the library. If you have any examples for other APIs, let us know and we will happily add a link to the README above! ### Why does Google_..._Service have weird names? ### The _Service classes are generally automatically generated from the API discovery documents: https://developers.google.com/discovery/. Sometimes new features are added to APIs with unusual names, which can cause some unexpected or non-standard style naming in the PHP classes. ### How do I deal with non-JSON response types? ### Some services return XML or similar by default, rather than JSON, which is what the library supports. You can request a JSON response by adding an 'alt' argument to optional params that is normally the last argument to a method call: ``` $opt_params = array( 'alt' => "json" ); ``` ### How do I set a field to null? ### The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialized properties. To work around this, set the field you want to null to `Google_Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire. ## Code Quality ## Run the PHPUnit tests with PHPUnit. You can configure an API key and token in BaseTest.php to run all calls, but this will require some setup on the Google Developer Console. phpunit tests/ ### Coding Style To check for coding style violations, run ``` vendor/bin/phpcs src --standard=style/ruleset.xml -np ``` To automatically fix (fixable) coding style violations, run ``` vendor/bin/phpcbf src --standard=style/ruleset.xml ``` <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth; trait CacheTrait { private $maxKeyLength = 64; /** * Gets the cached value if it is present in the cache when that is * available. */ private function getCachedValue($k) { if (is_null($this->cache)) { return; } $key = $this->getFullCacheKey($k); if (is_null($key)) { return; } $cacheItem = $this->cache->getItem($key); if ($cacheItem->isHit()) { return $cacheItem->get(); } } /** * Saves the value in the cache when that is available. */ private function setCachedValue($k, $v) { if (is_null($this->cache)) { return; } $key = $this->getFullCacheKey($k); if (is_null($key)) { return; } $cacheItem = $this->cache->getItem($key); $cacheItem->set($v); $cacheItem->expiresAfter($this->cacheConfig['lifetime']); return $this->cache->save($cacheItem); } private function getFullCacheKey($key) { if (is_null($key)) { return; } $key = $this->cacheConfig['prefix'] . $key; // ensure we do not have illegal characters $key = preg_replace('|[^a-zA-Z0-9_\.!]|', '', $key); // Hash keys if they exceed $maxKeyLength (defaults to 64) if ($this->maxKeyLength && strlen($key) > $this->maxKeyLength) { $key = substr(hash('sha256', $key), 0, $this->maxKeyLength); } return $key; } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth; /** * An interface implemented by objects that can fetch auth tokens. */ interface FetchAuthTokenInterface { /** * Fetches the auth tokens based on the current state. * * @param callable $httpHandler callback which delivers psr7 request * * @return array a hash of auth tokens */ public function fetchAuthToken(callable $httpHandler = null); /** * Obtains a key that can used to cache the results of #fetchAuthToken. * * If the value is empty, the auth token is not cached. * * @return string a key that may be used to cache the auth token. */ public function getCacheKey(); /** * Returns an associative array with the token and * expiration time. * * @return null|array { * The last received access token. * * @var string $access_token The access token string. * @var int $expires_at The time the token expires as a UNIX timestamp. * } */ public function getLastReceivedToken(); } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Middleware; use Google\Auth\FetchAuthTokenInterface; use Psr\Http\Message\RequestInterface; /** * AuthTokenMiddleware is a Guzzle Middleware that adds an Authorization header * provided by an object implementing FetchAuthTokenInterface. * * The FetchAuthTokenInterface#fetchAuthToken is used to obtain a hash; one of * the values value in that hash is added as the authorization header. * * Requests will be accessed with the authorization header: * * 'authorization' 'Bearer <value of auth_token>' */ class AuthTokenMiddleware { /** * @var callback */ private $httpHandler; /** * @var FetchAuthTokenInterface */ private $fetcher; /** * @var callable */ private $tokenCallback; /** * Creates a new AuthTokenMiddleware. * * @param FetchAuthTokenInterface $fetcher is used to fetch the auth token * @param callable $httpHandler (optional) callback which delivers psr7 request * @param callable $tokenCallback (optional) function to be called when a new token is fetched. */ public function __construct( FetchAuthTokenInterface $fetcher, callable $httpHandler = null, callable $tokenCallback = null ) { $this->fetcher = $fetcher; $this->httpHandler = $httpHandler; $this->tokenCallback = $tokenCallback; } /** * Updates the request with an Authorization header when auth is 'google_auth'. * * use Google\Auth\Middleware\AuthTokenMiddleware; * use Google\Auth\OAuth2; * use GuzzleHttp\Client; * use GuzzleHttp\HandlerStack; * * $config = [..<oauth config param>.]; * $oauth2 = new OAuth2($config) * $middleware = new AuthTokenMiddleware($oauth2); * $stack = HandlerStack::create(); * $stack->push($middleware); * * $client = new Client([ * 'handler' => $stack, * 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', * 'auth' => 'google_auth' // authorize all requests * ]); * * $res = $client->get('myproject/taskqueues/myqueue'); * * @param callable $handler * * @return \Closure */ public function __invoke(callable $handler) { return function (RequestInterface $request, array $options) use ($handler) { // Requests using "auth"="google_auth" will be authorized. if (!isset($options['auth']) || $options['auth'] !== 'google_auth') { return $handler($request, $options); } $request = $request->withHeader('authorization', 'Bearer ' . $this->fetchToken()); return $handler($request, $options); }; } /** * Call fetcher to fetch the token. * * @return string */ private function fetchToken() { $auth_tokens = $this->fetcher->fetchAuthToken($this->httpHandler); if (array_key_exists('access_token', $auth_tokens)) { // notify the callback if applicable if ($this->tokenCallback) { call_user_func($this->tokenCallback, $this->fetcher->getCacheKey(), $auth_tokens['access_token']); } return $auth_tokens['access_token']; } } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Middleware; use GuzzleHttp\Psr7; use Psr\Http\Message\RequestInterface; /** * SimpleMiddleware is a Guzzle Middleware that implements Google's Simple API * access. * * Requests are accessed using the Simple API access developer key. */ class SimpleMiddleware { /** * @var array */ private $config; /** * Create a new Simple plugin. * * The configuration array expects one option * - key: required, otherwise InvalidArgumentException is thrown * * @param array $config Configuration array */ public function __construct(array $config) { if (!isset($config['key'])) { throw new \InvalidArgumentException('requires a key to have been set'); } $this->config = array_merge(['key' => null], $config); } /** * Updates the request query with the developer key if auth is set to simple. * * use Google\Auth\Middleware\SimpleMiddleware; * use GuzzleHttp\Client; * use GuzzleHttp\HandlerStack; * * $my_key = 'is not the same as yours'; * $middleware = new SimpleMiddleware(['key' => $my_key]); * $stack = HandlerStack::create(); * $stack->push($middleware); * * $client = new Client([ * 'handler' => $stack, * 'base_uri' => 'https://www.googleapis.com/discovery/v1/', * 'auth' => 'simple' * ]); * * $res = $client->get('drive/v2/rest'); * * @param callable $handler * * @return \Closure */ public function __invoke(callable $handler) { return function (RequestInterface $request, array $options) use ($handler) { // Requests using "auth"="scoped" will be authorized. if (!isset($options['auth']) || $options['auth'] !== 'simple') { return $handler($request, $options); } $query = Psr7\parse_query($request->getUri()->getQuery()); $params = array_merge($query, $this->config); $uri = $request->getUri()->withQuery(Psr7\build_query($params)); $request = $request->withUri($uri); return $handler($request, $options); }; } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Middleware; use Google\Auth\CacheTrait; use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Message\RequestInterface; /** * ScopedAccessTokenMiddleware is a Guzzle Middleware that adds an Authorization * header provided by a closure. * * The closure returns an access token, taking the scope, either a single * string or an array of strings, as its value. If provided, a cache will be * used to preserve the access token for a given lifetime. * * Requests will be accessed with the authorization header: * * 'authorization' 'Bearer <value of auth_token>' */ class ScopedAccessTokenMiddleware { use CacheTrait; const DEFAULT_CACHE_LIFETIME = 1500; /** * @var CacheItemPoolInterface */ private $cache; /** * @var array configuration */ private $cacheConfig; /** * @var callable */ private $tokenFunc; /** * @var array|string */ private $scopes; /** * Creates a new ScopedAccessTokenMiddleware. * * @param callable $tokenFunc a token generator function * @param array|string $scopes the token authentication scopes * @param array $cacheConfig configuration for the cache when it's present * @param CacheItemPoolInterface $cache an implementation of CacheItemPoolInterface */ public function __construct( callable $tokenFunc, $scopes, array $cacheConfig = null, CacheItemPoolInterface $cache = null ) { $this->tokenFunc = $tokenFunc; if (!(is_string($scopes) || is_array($scopes))) { throw new \InvalidArgumentException( 'wants scope should be string or array'); } $this->scopes = $scopes; if (!is_null($cache)) { $this->cache = $cache; $this->cacheConfig = array_merge([ 'lifetime' => self::DEFAULT_CACHE_LIFETIME, 'prefix' => '', ], $cacheConfig); } } /** * Updates the request with an Authorization header when auth is 'scoped'. * * E.g this could be used to authenticate using the AppEngine * AppIdentityService. * * use google\appengine\api\app_identity\AppIdentityService; * use Google\Auth\Middleware\ScopedAccessTokenMiddleware; * use GuzzleHttp\Client; * use GuzzleHttp\HandlerStack; * * $scope = 'https://www.googleapis.com/auth/taskqueue' * $middleware = new ScopedAccessTokenMiddleware( * 'AppIdentityService::getAccessToken', * $scope, * [ 'prefix' => 'Google\Auth\ScopedAccessToken::' ], * $cache = new Memcache() * ); * $stack = HandlerStack::create(); * $stack->push($middleware); * * $client = new Client([ * 'handler' => $stack, * 'base_url' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', * 'auth' => 'scoped' // authorize all requests * ]); * * $res = $client->get('myproject/taskqueues/myqueue'); * * @param callable $handler * * @return \Closure */ public function __invoke(callable $handler) { return function (RequestInterface $request, array $options) use ($handler) { // Requests using "auth"="scoped" will be authorized. if (!isset($options['auth']) || $options['auth'] !== 'scoped') { return $handler($request, $options); } $request = $request->withHeader('authorization', 'Bearer ' . $this->fetchToken()); return $handler($request, $options); }; } /** * @return string */ private function getCacheKey() { $key = null; if (is_string($this->scopes)) { $key .= $this->scopes; } elseif (is_array($this->scopes)) { $key .= implode(':', $this->scopes); } return $key; } /** * Determine if token is available in the cache, if not call tokenFunc to * fetch it. * * @return string */ private function fetchToken() { $cacheKey = $this->getCacheKey(); $cached = $this->getCachedValue($cacheKey); if (!empty($cached)) { return $cached; } $token = call_user_func($this->tokenFunc, $this->scopes); $this->setCachedValue($cacheKey, $token); return $token; } } <?php /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Cache; use Psr\Cache\CacheItemInterface; /** * A cache item. */ final class Item implements CacheItemInterface { /** * @var string */ private $key; /** * @var mixed */ private $value; /** * @var \DateTime|null */ private $expiration; /** * @var bool */ private $isHit = false; /** * @param string $key */ public function __construct($key) { $this->key = $key; } /** * {@inheritdoc} */ public function getKey() { return $this->key; } /** * {@inheritdoc} */ public function get() { return $this->isHit() ? $this->value : null; } /** * {@inheritdoc} */ public function isHit() { if (!$this->isHit) { return false; } if ($this->expiration === null) { return true; } return $this->currentTime()->getTimestamp() < $this->expiration->getTimestamp(); } /** * {@inheritdoc} */ public function set($value) { $this->isHit = true; $this->value = $value; return $this; } /** * {@inheritdoc} */ public function expiresAt($expiration) { if ($this->isValidExpiration($expiration)) { $this->expiration = $expiration; return $this; } $implementationMessage = interface_exists('DateTimeInterface') ? 'implement interface DateTimeInterface' : 'be an instance of DateTime'; $error = sprintf( 'Argument 1 passed to %s::expiresAt() must %s, %s given', get_class($this), $implementationMessage, gettype($expiration) ); $this->handleError($error); } /** * {@inheritdoc} */ public function expiresAfter($time) { if (is_int($time)) { $this->expiration = $this->currentTime()->add(new \DateInterval("PT{$time}S")); } elseif ($time instanceof \DateInterval) { $this->expiration = $this->currentTime()->add($time); } elseif ($time === null) { $this->expiration = $time; } else { $message = 'Argument 1 passed to %s::expiresAfter() must be an ' . 'instance of DateInterval or of the type integer, %s given'; $error = sprintf($message, get_class($this), gettype($time)); $this->handleError($error); } return $this; } /** * Handles an error. * * @param string $error * @throws \TypeError */ private function handleError($error) { if (class_exists('TypeError')) { throw new \TypeError($error); } trigger_error($error, E_USER_ERROR); } /** * Determines if an expiration is valid based on the rules defined by PSR6. * * @param mixed $expiration * @return bool */ private function isValidExpiration($expiration) { if ($expiration === null) { return true; } // We test for two types here due to the fact the DateTimeInterface // was not introduced until PHP 5.5. Checking for the DateTime type as // well allows us to support 5.4. if ($expiration instanceof \DateTimeInterface) { return true; } if ($expiration instanceof \DateTime) { return true; } return false; } protected function currentTime() { return new \DateTime('now', new \DateTimeZone('UTC')); } } <?php /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Cache; use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; /** * Simple in-memory cache implementation. */ final class MemoryCacheItemPool implements CacheItemPoolInterface { /** * @var CacheItemInterface[] */ private $items; /** * @var CacheItemInterface[] */ private $deferredItems; /** * {@inheritdoc} */ public function getItem($key) { return current($this->getItems([$key])); } /** * {@inheritdoc} */ public function getItems(array $keys = []) { $items = []; foreach ($keys as $key) { $items[$key] = $this->hasItem($key) ? clone $this->items[$key] : new Item($key); } return $items; } /** * {@inheritdoc} */ public function hasItem($key) { $this->isValidKey($key); return isset($this->items[$key]) && $this->items[$key]->isHit(); } /** * {@inheritdoc} */ public function clear() { $this->items = []; $this->deferredItems = []; return true; } /** * {@inheritdoc} */ public function deleteItem($key) { return $this->deleteItems([$key]); } /** * {@inheritdoc} */ public function deleteItems(array $keys) { array_walk($keys, [$this, 'isValidKey']); foreach ($keys as $key) { unset($this->items[$key]); } return true; } /** * {@inheritdoc} */ public function save(CacheItemInterface $item) { $this->items[$item->getKey()] = $item; return true; } /** * {@inheritdoc} */ public function saveDeferred(CacheItemInterface $item) { $this->deferredItems[$item->getKey()] = $item; return true; } /** * {@inheritdoc} */ public function commit() { foreach ($this->deferredItems as $item) { $this->save($item); } $this->deferredItems = []; return true; } /** * Determines if the provided key is valid. * * @param string $key * @return bool * @throws InvalidArgumentException */ private function isValidKey($key) { $invalidCharacters = '{}()/\\\\@:'; if (!is_string($key) || preg_match("#[$invalidCharacters]#", $key)) { throw new InvalidArgumentException('The provided key is not valid: ' . var_export($key, true)); } return true; } } <?php /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Cache; use Psr\Cache\InvalidArgumentException as PsrInvalidArgumentException; class InvalidArgumentException extends \InvalidArgumentException implements PsrInvalidArgumentException { } <?php /** * Copyright 2018 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Cache; use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; /** * SystemV shared memory based CacheItemPool implementation. * * This CacheItemPool implementation can be used among multiple processes, but * it doesn't provide any locking mechanism. If multiple processes write to * this ItemPool, you have to avoid race condition manually in your code. */ class SysVCacheItemPool implements CacheItemPoolInterface { const VAR_KEY = 1; const DEFAULT_PROJ = 'A'; const DEFAULT_MEMSIZE = 10000; const DEFAULT_PERM = 0600; /** @var int */ private $sysvKey; /** * @var CacheItemInterface[] */ private $items; /** * @var CacheItemInterface[] */ private $deferredItems; /** * @var array */ private $options; /* * @var bool */ private $hasLoadedItems = false; /** * Create a SystemV shared memory based CacheItemPool. * * @param array $options [optional] { * Configuration options. * * @type int $variableKey The variable key for getting the data from * the shared memory. **Defaults to** 1. * @type string $proj The project identifier for ftok. This needs to * be a one character string. **Defaults to** 'A'. * @type int $memsize The memory size in bytes for shm_attach. * **Defaults to** 10000. * @type int $perm The permission for shm_attach. **Defaults to** 0600. */ public function __construct($options = []) { if (! extension_loaded('sysvshm')) { throw \RuntimeException( 'sysvshm extension is required to use this ItemPool'); } $this->options = $options + [ 'variableKey' => self::VAR_KEY, 'proj' => self::DEFAULT_PROJ, 'memsize' => self::DEFAULT_MEMSIZE, 'perm' => self::DEFAULT_PERM ]; $this->items = []; $this->deferredItems = []; $this->sysvKey = ftok(__FILE__, $this->options['proj']); } /** * {@inheritdoc} */ public function getItem($key) { $this->loadItems(); return current($this->getItems([$key])); } /** * {@inheritdoc} */ public function getItems(array $keys = []) { $this->loadItems(); $items = []; foreach ($keys as $key) { $items[$key] = $this->hasItem($key) ? clone $this->items[$key] : new Item($key); } return $items; } /** * {@inheritdoc} */ public function hasItem($key) { $this->loadItems(); return isset($this->items[$key]) && $this->items[$key]->isHit(); } /** * {@inheritdoc} */ public function clear() { $this->items = []; $this->deferredItems = []; return $this->saveCurrentItems(); } /** * {@inheritdoc} */ public function deleteItem($key) { return $this->deleteItems([$key]); } /** * {@inheritdoc} */ public function deleteItems(array $keys) { if (!$this->hasLoadedItems) { $this->loadItems(); } foreach ($keys as $key) { unset($this->items[$key]); } return $this->saveCurrentItems(); } /** * {@inheritdoc} */ public function save(CacheItemInterface $item) { if (!$this->hasLoadedItems) { $this->loadItems(); } $this->items[$item->getKey()] = $item; return $this->saveCurrentItems(); } /** * {@inheritdoc} */ public function saveDeferred(CacheItemInterface $item) { $this->deferredItems[$item->getKey()] = $item; return true; } /** * {@inheritdoc} */ public function commit() { foreach ($this->deferredItems as $item) { if ($this->save($item) === false) { return false; } } $this->deferredItems = []; return true; } /** * Save the current items. * * @return bool true when success, false upon failure */ private function saveCurrentItems() { $shmid = shm_attach( $this->sysvKey, $this->options['memsize'], $this->options['perm'] ); if ($shmid !== false) { $ret = shm_put_var( $shmid, $this->options['variableKey'], $this->items ); shm_detach($shmid); return $ret; } return false; } /** * Load the items from the shared memory. * * @return bool true when success, false upon failure */ private function loadItems() { $shmid = shm_attach( $this->sysvKey, $this->options['memsize'], $this->options['perm'] ); if ($shmid !== false) { $data = @shm_get_var($shmid, $this->options['variableKey']); if (!empty($data)) { $this->items = $data; } else { $this->items = []; } shm_detach($shmid); $this->hasLoadedItems = true; return true; } return false; } } <?php /** * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\HttpHandler; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; class HttpHandlerFactory { /** * Builds out a default http handler for the installed version of guzzle. * * @param ClientInterface $client * @return Guzzle5HttpHandler|Guzzle6HttpHandler * @throws \Exception */ public static function build(ClientInterface $client = null) { $version = ClientInterface::VERSION; $client = $client ?: new Client(); switch ($version[0]) { case '5': return new Guzzle5HttpHandler($client); case '6': return new Guzzle6HttpHandler($client); default: throw new \Exception('Version not supported'); } } } <?php /* * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\HttpHandler; use GuzzleHttp\ClientInterface; /** * Stores an HTTP Client in order to prevent multiple instantiations. */ class HttpClientCache { /** * @var ClientInterface|null */ private static $httpClient; /** * Cache an HTTP Client for later calls. * * Passing null will unset the cached client. * * @param ClientInterface|null $client * @return void */ public static function setHttpClient(ClientInterface $client = null) { self::$httpClient = $client; } /** * Get the stored HTTP Client, or null. * * @return ClientInterface|null */ public static function getHttpClient() { return self::$httpClient; } } <?php /** * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\HttpHandler; use Exception; use GuzzleHttp\ClientInterface; use GuzzleHttp\Message\ResponseInterface as Guzzle5ResponseInterface; use GuzzleHttp\Promise\Promise; use GuzzleHttp\Promise\RejectedPromise; use GuzzleHttp\Psr7\Response; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; class Guzzle5HttpHandler { /** * @var ClientInterface */ private $client; /** * @param ClientInterface $client */ public function __construct(ClientInterface $client) { $this->client = $client; } /** * Accepts a PSR-7 Request and an array of options and returns a PSR-7 response. * * @param RequestInterface $request * @param array $options * * @return ResponseInterface */ public function __invoke(RequestInterface $request, array $options = []) { $response = $this->client->send( $this->createGuzzle5Request($request, $options) ); return $this->createPsr7Response($response); } /** * Accepts a PSR-7 request and an array of options and returns a PromiseInterface * * @param RequestInterface $request * @param array $options * * @return Promise */ public function async(RequestInterface $request, array $options = []) { if (!class_exists('GuzzleHttp\Promise\Promise')) { throw new Exception('Install guzzlehttp/promises to use async with Guzzle 5'); } $futureResponse = $this->client->send( $this->createGuzzle5Request( $request, ['future' => true] + $options ) ); $promise = new Promise( function () use ($futureResponse) { try { $futureResponse->wait(); } catch (Exception $e) { // The promise is already delivered when the exception is // thrown, so don't rethrow it. } }, [$futureResponse, 'cancel'] ); $futureResponse->then([$promise, 'resolve'], [$promise, 'reject']); return $promise->then( function (Guzzle5ResponseInterface $response) { // Adapt the Guzzle 5 Response to a PSR-7 Response. return $this->createPsr7Response($response); }, function (Exception $e) { return new RejectedPromise($e); } ); } private function createGuzzle5Request(RequestInterface $request, array $options) { return $this->client->createRequest( $request->getMethod(), $request->getUri(), array_merge_recursive([ 'headers' => $request->getHeaders(), 'body' => $request->getBody(), ], $options) ); } private function createPsr7Response(Guzzle5ResponseInterface $response) { return new Response( $response->getStatusCode(), $response->getHeaders() ?: [], $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase() ); } } <?php namespace Google\Auth\HttpHandler; use GuzzleHttp\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; class Guzzle6HttpHandler { /** * @var ClientInterface */ private $client; /** * @param ClientInterface $client */ public function __construct(ClientInterface $client) { $this->client = $client; } /** * Accepts a PSR-7 request and an array of options and returns a PSR-7 response. * * @param RequestInterface $request * @param array $options * * @return ResponseInterface */ public function __invoke(RequestInterface $request, array $options = []) { return $this->client->send($request, $options); } /** * Accepts a PSR-7 request and an array of options and returns a PromiseInterface * * @param RequestInterface $request * @param array $options * * @return \GuzzleHttp\Promise\Promise */ public function async(RequestInterface $request, array $options = []) { return $this->client->sendAsync($request, $options); } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth; use Google\Auth\Credentials\InsecureCredentials; use Google\Auth\Credentials\ServiceAccountCredentials; use Google\Auth\Credentials\UserRefreshCredentials; /** * CredentialsLoader contains the behaviour used to locate and find default * credentials files on the file system. */ abstract class CredentialsLoader implements FetchAuthTokenInterface { const TOKEN_CREDENTIAL_URI = 'https://oauth2.googleapis.com/token'; const ENV_VAR = 'GOOGLE_APPLICATION_CREDENTIALS'; const WELL_KNOWN_PATH = 'gcloud/application_default_credentials.json'; const NON_WINDOWS_WELL_KNOWN_PATH_BASE = '.config'; const AUTH_METADATA_KEY = 'authorization'; /** * @param string $cause * @return string */ private static function unableToReadEnv($cause) { $msg = 'Unable to read the credential file specified by '; $msg .= ' GOOGLE_APPLICATION_CREDENTIALS: '; $msg .= $cause; return $msg; } /** * @return bool */ private static function isOnWindows() { return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; } /** * Load a JSON key from the path specified in the environment. * * Load a JSON key from the path specified in the environment * variable GOOGLE_APPLICATION_CREDENTIALS. Return null if * GOOGLE_APPLICATION_CREDENTIALS is not specified. * * @return array JSON key | null */ public static function fromEnv() { $path = getenv(self::ENV_VAR); if (empty($path)) { return; } if (!file_exists($path)) { $cause = 'file ' . $path . ' does not exist'; throw new \DomainException(self::unableToReadEnv($cause)); } $jsonKey = file_get_contents($path); return json_decode($jsonKey, true); } /** * Load a JSON key from a well known path. * * The well known path is OS dependent: * - windows: %APPDATA%/gcloud/application_default_credentials.json * - others: $HOME/.config/gcloud/application_default_credentials.json * * If the file does not exists, this returns null. * * @return array JSON key | null */ public static function fromWellKnownFile() { $rootEnv = self::isOnWindows() ? 'APPDATA' : 'HOME'; $path = [getenv($rootEnv)]; if (!self::isOnWindows()) { $path[] = self::NON_WINDOWS_WELL_KNOWN_PATH_BASE; } $path[] = self::WELL_KNOWN_PATH; $path = implode(DIRECTORY_SEPARATOR, $path); if (!file_exists($path)) { return; } $jsonKey = file_get_contents($path); return json_decode($jsonKey, true); } /** * Create a new Credentials instance. * * @param string|array $scope the scope of the access request, expressed * either as an Array or as a space-delimited String. * @param array $jsonKey the JSON credentials. * * @return ServiceAccountCredentials|UserRefreshCredentials */ public static function makeCredentials($scope, array $jsonKey) { if (!array_key_exists('type', $jsonKey)) { throw new \InvalidArgumentException('json key is missing the type field'); } if ($jsonKey['type'] == 'service_account') { return new ServiceAccountCredentials($scope, $jsonKey); } if ($jsonKey['type'] == 'authorized_user') { return new UserRefreshCredentials($scope, $jsonKey); } throw new \InvalidArgumentException('invalid value in the type field'); } /** * Create an authorized HTTP Client from an instance of FetchAuthTokenInterface. * * @param FetchAuthTokenInterface $fetcher is used to fetch the auth token * @param array $httpClientOptoins (optional) Array of request options to apply. * @param callable $httpHandler (optional) http client to fetch the token. * @param callable $tokenCallback (optional) function to be called when a new token is fetched. * * @return \GuzzleHttp\Client */ public static function makeHttpClient( FetchAuthTokenInterface $fetcher, array $httpClientOptions = [], callable $httpHandler = null, callable $tokenCallback = null ) { $version = \GuzzleHttp\ClientInterface::VERSION; switch ($version[0]) { case '5': $client = new \GuzzleHttp\Client($httpClientOptions); $client->setDefaultOption('auth', 'google_auth'); $subscriber = new Subscriber\AuthTokenSubscriber( $fetcher, $httpHandler, $tokenCallback ); $client->getEmitter()->attach($subscriber); return $client; case '6': $middleware = new Middleware\AuthTokenMiddleware( $fetcher, $httpHandler, $tokenCallback ); $stack = \GuzzleHttp\HandlerStack::create(); $stack->push($middleware); return new \GuzzleHttp\Client([ 'handler' => $stack, 'auth' => 'google_auth', ] + $httpClientOptions); default: throw new \Exception('Version not supported'); } } /** * Create a new instance of InsecureCredentials. * * @return InsecureCredentials */ public static function makeInsecureCredentials() { return new InsecureCredentials(); } /** * export a callback function which updates runtime metadata. * * @return array updateMetadata function */ public function getUpdateMetadataFunc() { return array($this, 'updateMetadata'); } /** * Updates metadata with the authorization token. * * @param array $metadata metadata hashmap * @param string $authUri optional auth uri * @param callable $httpHandler callback which delivers psr7 request * * @return array updated metadata hashmap */ public function updateMetadata( $metadata, $authUri = null, callable $httpHandler = null ) { $result = $this->fetchAuthToken($httpHandler); if (!isset($result['access_token'])) { return $metadata; } $metadata_copy = $metadata; $metadata_copy[self::AUTH_METADATA_KEY] = array('Bearer ' . $result['access_token']); return $metadata_copy; } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Credentials; use Google\Auth\CredentialsLoader; use Google\Auth\OAuth2; use Google\Auth\ServiceAccountSignerTrait; use Google\Auth\SignBlobInterface; /** * ServiceAccountCredentials supports authorization using a Google service * account. * * (cf https://developers.google.com/accounts/docs/OAuth2ServiceAccount) * * It's initialized using the json key file that's downloadable from developer * console, which should contain a private_key and client_email fields that it * uses. * * Use it with AuthTokenMiddleware to authorize http requests: * * use Google\Auth\Credentials\ServiceAccountCredentials; * use Google\Auth\Middleware\AuthTokenMiddleware; * use GuzzleHttp\Client; * use GuzzleHttp\HandlerStack; * * $sa = new ServiceAccountCredentials( * 'https://www.googleapis.com/auth/taskqueue', * '/path/to/your/json/key_file.json' * ); * $middleware = new AuthTokenMiddleware($sa); * $stack = HandlerStack::create(); * $stack->push($middleware); * * $client = new Client([ * 'handler' => $stack, * 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', * 'auth' => 'google_auth' // authorize all requests * ]); * * $res = $client->get('myproject/taskqueues/myqueue'); */ class ServiceAccountCredentials extends CredentialsLoader implements SignBlobInterface { use ServiceAccountSignerTrait; /** * The OAuth2 instance used to conduct authorization. * * @var OAuth2 */ protected $auth; /** * Create a new ServiceAccountCredentials. * * @param string|array $scope the scope of the access request, expressed * either as an Array or as a space-delimited String. * @param string|array $jsonKey JSON credential file path or JSON credentials * as an associative array * @param string $sub an email address account to impersonate, in situations when * the service account has been delegated domain wide access. */ public function __construct( $scope, $jsonKey, $sub = null ) { if (is_string($jsonKey)) { if (!file_exists($jsonKey)) { throw new \InvalidArgumentException('file does not exist'); } $jsonKeyStream = file_get_contents($jsonKey); if (!$jsonKey = json_decode($jsonKeyStream, true)) { throw new \LogicException('invalid json for auth config'); } } if (!array_key_exists('client_email', $jsonKey)) { throw new \InvalidArgumentException( 'json key is missing the client_email field'); } if (!array_key_exists('private_key', $jsonKey)) { throw new \InvalidArgumentException( 'json key is missing the private_key field'); } $this->auth = new OAuth2([ 'audience' => self::TOKEN_CREDENTIAL_URI, 'issuer' => $jsonKey['client_email'], 'scope' => $scope, 'signingAlgorithm' => 'RS256', 'signingKey' => $jsonKey['private_key'], 'sub' => $sub, 'tokenCredentialUri' => self::TOKEN_CREDENTIAL_URI, ]); } /** * @param callable $httpHandler * * @return array A set of auth related metadata, containing the following * keys: * - access_token (string) * - expires_in (int) * - token_type (string) */ public function fetchAuthToken(callable $httpHandler = null) { return $this->auth->fetchAuthToken($httpHandler); } /** * @return string */ public function getCacheKey() { $key = $this->auth->getIssuer() . ':' . $this->auth->getCacheKey(); if ($sub = $this->auth->getSub()) { $key .= ':' . $sub; } return $key; } /** * @return array */ public function getLastReceivedToken() { return $this->auth->getLastReceivedToken(); } /** * Updates metadata with the authorization token. * * @param array $metadata metadata hashmap * @param string $authUri optional auth uri * @param callable $httpHandler callback which delivers psr7 request * * @return array updated metadata hashmap */ public function updateMetadata( $metadata, $authUri = null, callable $httpHandler = null ) { // scope exists. use oauth implementation $scope = $this->auth->getScope(); if (!is_null($scope)) { return parent::updateMetadata($metadata, $authUri, $httpHandler); } // no scope found. create jwt with the auth uri $credJson = array( 'private_key' => $this->auth->getSigningKey(), 'client_email' => $this->auth->getIssuer(), ); $jwtCreds = new ServiceAccountJwtAccessCredentials($credJson); return $jwtCreds->updateMetadata($metadata, $authUri, $httpHandler); } /** * @param string $sub an email address account to impersonate, in situations when * the service account has been delegated domain wide access. */ public function setSub($sub) { $this->auth->setSub($sub); } /** * Get the client name from the keyfile. * * In this case, it returns the keyfile's client_email key. * * @param callable $httpHandler Not used by this credentials type. * @return string */ public function getClientName(callable $httpHandler = null) { return $this->auth->getIssuer(); } } <?php /* * Copyright 2018 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Credentials; use Google\Auth\FetchAuthTokenInterface; /** * Provides a set of credentials that will always return an empty access token. * This is useful for APIs which do not require authentication, for local * service emulators, and for testing. */ class InsecureCredentials implements FetchAuthTokenInterface { /** * @var array */ private $token = [ 'access_token' => '' ]; /** * Fetches the auth token. In this case it returns an empty string. * * @param callable $httpHandler * @return array A set of auth related metadata, containing the following * keys: * - access_token (string) */ public function fetchAuthToken(callable $httpHandler = null) { return $this->token; } /** * Returns the cache key. In this case it returns a null value, disabling * caching. * * @return string|null */ public function getCacheKey() { return null; } /** * Fetches the last received token. In this case, it returns the same empty string * auth token. * * @return array */ public function getLastReceivedToken() { return $this->token; } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Credentials; /* * The AppIdentityService class is automatically defined on App Engine, * so including this dependency is not necessary, and will result in a * PHP fatal error in the App Engine environment. */ use google\appengine\api\app_identity\AppIdentityService; use Google\Auth\CredentialsLoader; use Google\Auth\SignBlobInterface; /** * AppIdentityCredentials supports authorization on Google App Engine. * * It can be used to authorize requests using the AuthTokenMiddleware or * AuthTokenSubscriber, but will only succeed if being run on App Engine: * * use Google\Auth\Credentials\AppIdentityCredentials; * use Google\Auth\Middleware\AuthTokenMiddleware; * use GuzzleHttp\Client; * use GuzzleHttp\HandlerStack; * * $gae = new AppIdentityCredentials('https://www.googleapis.com/auth/books'); * $middleware = new AuthTokenMiddleware($gae); * $stack = HandlerStack::create(); * $stack->push($middleware); * * $client = new Client([ * 'handler' => $stack, * 'base_uri' => 'https://www.googleapis.com/books/v1', * 'auth' => 'google_auth' * ]); * * $res = $client->get('volumes?q=Henry+David+Thoreau&country=US'); */ class AppIdentityCredentials extends CredentialsLoader implements SignBlobInterface { /** * Result of fetchAuthToken. * * @array */ protected $lastReceivedToken; /** * Array of OAuth2 scopes to be requested. */ private $scope; /** * @var string */ private $clientName; public function __construct($scope = array()) { $this->scope = $scope; } /** * Determines if this an App Engine instance, by accessing the * SERVER_SOFTWARE environment variable (prod) or the APPENGINE_RUNTIME * environment variable (dev). * * @return true if this an App Engine Instance, false otherwise */ public static function onAppEngine() { $appEngineProduction = isset($_SERVER['SERVER_SOFTWARE']) && 0 === strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine'); if ($appEngineProduction) { return true; } $appEngineDevAppServer = isset($_SERVER['APPENGINE_RUNTIME']) && $_SERVER['APPENGINE_RUNTIME'] == 'php'; if ($appEngineDevAppServer) { return true; } return false; } /** * Implements FetchAuthTokenInterface#fetchAuthToken. * * Fetches the auth tokens using the AppIdentityService if available. * As the AppIdentityService uses protobufs to fetch the access token, * the GuzzleHttp\ClientInterface instance passed in will not be used. * * @param callable $httpHandler callback which delivers psr7 request * * @return array A set of auth related metadata, containing the following * keys: * - access_token (string) * - expiration_time (string) */ public function fetchAuthToken(callable $httpHandler = null) { try { $this->checkAppEngineContext(); } catch (\Exception $e) { return []; } // AppIdentityService expects an array when multiple scopes are supplied $scope = is_array($this->scope) ? $this->scope : explode(' ', $this->scope); $token = AppIdentityService::getAccessToken($scope); $this->lastReceivedToken = $token; return $token; } /** * Sign a string using AppIdentityService. * * @param string $stringToSign The string to sign. * @param bool $forceOpenSsl [optional] Does not apply to this credentials * type. * @return string The signature, base64-encoded. * @throws \Exception If AppEngine SDK or mock is not available. */ public function signBlob($stringToSign, $forceOpenSsl = false) { $this->checkAppEngineContext(); return base64_encode(AppIdentityService::signForApp($stringToSign)['signature']); } /** * Get the client name from AppIdentityService. * * Subsequent calls to this method will return a cached value. * * @param callable $httpHandler Not used in this implementation. * @return string * @throws \Exception If AppEngine SDK or mock is not available. */ public function getClientName(callable $httpHandler = null) { $this->checkAppEngineContext(); if (!$this->clientName) { $this->clientName = AppIdentityService::getServiceAccountName(); } return $this->clientName; } /** * @return array|null */ public function getLastReceivedToken() { if ($this->lastReceivedToken) { return [ 'access_token' => $this->lastReceivedToken['access_token'], 'expires_at' => $this->lastReceivedToken['expiration_time'], ]; } return null; } /** * Caching is handled by the underlying AppIdentityService, return empty string * to prevent caching. * * @return string */ public function getCacheKey() { return ''; } private function checkAppEngineContext() { if (!self::onAppEngine() || !class_exists('google\appengine\api\app_identity\AppIdentityService')) { throw new \Exception( 'This class must be run in App Engine, or you must include the AppIdentityService ' . 'mock class defined in tests/mocks/AppIdentityService.php' ); } } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Credentials; use Google\Auth\CredentialsLoader; use Google\Auth\OAuth2; use Google\Auth\ServiceAccountSignerTrait; use Google\Auth\SignBlobInterface; /** * Authenticates requests using Google's Service Account credentials via * JWT Access. * * This class allows authorizing requests for service accounts directly * from credentials from a json key file downloaded from the developer * console (via 'Generate new Json Key'). It is not part of any OAuth2 * flow, rather it creates a JWT and sends that as a credential. */ class ServiceAccountJwtAccessCredentials extends CredentialsLoader implements SignBlobInterface { use ServiceAccountSignerTrait; /** * The OAuth2 instance used to conduct authorization. * * @var OAuth2 */ protected $auth; /** * Create a new ServiceAccountJwtAccessCredentials. * * @param string|array $jsonKey JSON credential file path or JSON credentials * as an associative array */ public function __construct($jsonKey) { if (is_string($jsonKey)) { if (!file_exists($jsonKey)) { throw new \InvalidArgumentException('file does not exist'); } $jsonKeyStream = file_get_contents($jsonKey); if (!$jsonKey = json_decode($jsonKeyStream, true)) { throw new \LogicException('invalid json for auth config'); } } if (!array_key_exists('client_email', $jsonKey)) { throw new \InvalidArgumentException( 'json key is missing the client_email field'); } if (!array_key_exists('private_key', $jsonKey)) { throw new \InvalidArgumentException( 'json key is missing the private_key field'); } $this->auth = new OAuth2([ 'issuer' => $jsonKey['client_email'], 'sub' => $jsonKey['client_email'], 'signingAlgorithm' => 'RS256', 'signingKey' => $jsonKey['private_key'], ]); } /** * Updates metadata with the authorization token. * * @param array $metadata metadata hashmap * @param string $authUri optional auth uri * @param callable $httpHandler callback which delivers psr7 request * * @return array updated metadata hashmap */ public function updateMetadata( $metadata, $authUri = null, callable $httpHandler = null ) { if (empty($authUri)) { return $metadata; } $this->auth->setAudience($authUri); return parent::updateMetadata($metadata, $authUri, $httpHandler); } /** * Implements FetchAuthTokenInterface#fetchAuthToken. * * @param callable $httpHandler * * @return array|void A set of auth related metadata, containing the * following keys: * - access_token (string) */ public function fetchAuthToken(callable $httpHandler = null) { $audience = $this->auth->getAudience(); if (empty($audience)) { return null; } $access_token = $this->auth->toJwt(); return array('access_token' => $access_token); } /** * @return string */ public function getCacheKey() { return $this->auth->getCacheKey(); } /** * @return array */ public function getLastReceivedToken() { return $this->auth->getLastReceivedToken(); } /** * Get the client name from the keyfile. * * In this case, it returns the keyfile's client_email key. * * @param callable $httpHandler Not used by this credentials type. * @return string */ public function getClientName(callable $httpHandler = null) { return $this->auth->getIssuer(); } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Credentials; /** * Authenticates requests using IAM credentials. */ class IAMCredentials { const SELECTOR_KEY = 'x-goog-iam-authority-selector'; const TOKEN_KEY = 'x-goog-iam-authorization-token'; /** * @var string */ private $selector; /** * @var string */ private $token; /** * @param $selector string the IAM selector * @param $token string the IAM token */ public function __construct($selector, $token) { if (!is_string($selector)) { throw new \InvalidArgumentException( 'selector must be a string'); } if (!is_string($token)) { throw new \InvalidArgumentException( 'token must be a string'); } $this->selector = $selector; $this->token = $token; } /** * export a callback function which updates runtime metadata. * * @return array updateMetadata function */ public function getUpdateMetadataFunc() { return array($this, 'updateMetadata'); } /** * Updates metadata with the appropriate header metadata. * * @param array $metadata metadata hashmap * @param string $unusedAuthUri optional auth uri * @param callable $httpHandler callback which delivers psr7 request * Note: this param is unused here, only included here for * consistency with other credentials class * * @return array updated metadata hashmap */ public function updateMetadata( $metadata, $unusedAuthUri = null, callable $httpHandler = null ) { $metadata_copy = $metadata; $metadata_copy[self::SELECTOR_KEY] = $this->selector; $metadata_copy[self::TOKEN_KEY] = $this->token; return $metadata_copy; } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Credentials; use Google\Auth\CredentialsLoader; use Google\Auth\OAuth2; /** * Authenticates requests using User Refresh credentials. * * This class allows authorizing requests from user refresh tokens. * * This the end of the result of a 3LO flow. E.g, the end result of * 'gcloud auth login' saves a file with these contents in well known * location * * @see [Application Default Credentials](http://goo.gl/mkAHpZ) */ class UserRefreshCredentials extends CredentialsLoader { const CLOUD_SDK_CLIENT_ID = '764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com'; const SUPPRESS_CLOUD_SDK_CREDS_WARNING_ENV = 'SUPPRESS_GCLOUD_CREDS_WARNING'; /** * The OAuth2 instance used to conduct authorization. * * @var OAuth2 */ protected $auth; /** * Create a new UserRefreshCredentials. * * @param string|array $scope the scope of the access request, expressed * either as an Array or as a space-delimited String. * @param string|array $jsonKey JSON credential file path or JSON credentials * as an associative array */ public function __construct( $scope, $jsonKey ) { if (is_string($jsonKey)) { if (!file_exists($jsonKey)) { throw new \InvalidArgumentException('file does not exist'); } $jsonKeyStream = file_get_contents($jsonKey); if (!$jsonKey = json_decode($jsonKeyStream, true)) { throw new \LogicException('invalid json for auth config'); } } if (!array_key_exists('client_id', $jsonKey)) { throw new \InvalidArgumentException( 'json key is missing the client_id field'); } if (!array_key_exists('client_secret', $jsonKey)) { throw new \InvalidArgumentException( 'json key is missing the client_secret field'); } if (!array_key_exists('refresh_token', $jsonKey)) { throw new \InvalidArgumentException( 'json key is missing the refresh_token field'); } $this->auth = new OAuth2([ 'clientId' => $jsonKey['client_id'], 'clientSecret' => $jsonKey['client_secret'], 'refresh_token' => $jsonKey['refresh_token'], 'scope' => $scope, 'tokenCredentialUri' => self::TOKEN_CREDENTIAL_URI, ]); if ($jsonKey['client_id'] === self::CLOUD_SDK_CLIENT_ID && getenv(self::SUPPRESS_CLOUD_SDK_CREDS_WARNING_ENV) !== 'true') { trigger_error( 'Your application has authenticated using end user credentials ' . 'from Google Cloud SDK. We recommend that most server ' . 'applications use service accounts instead. If your ' . 'application continues to use end user credentials ' . 'from Cloud SDK, you might receive a "quota exceeded" ' . 'or "API not enabled" error. For more information about ' . 'service accounts, see ' . 'https://cloud.google.com/docs/authentication/. ' . 'To disable this warning, set ' . self::SUPPRESS_CLOUD_SDK_CREDS_WARNING_ENV . ' environment variable to "true".', E_USER_WARNING); } } /** * @param callable $httpHandler * * @return array A set of auth related metadata, containing the following * keys: * - access_token (string) * - expires_in (int) * - scope (string) * - token_type (string) * - id_token (string) */ public function fetchAuthToken(callable $httpHandler = null) { return $this->auth->fetchAuthToken($httpHandler); } /** * @return string */ public function getCacheKey() { return $this->auth->getClientId() . ':' . $this->auth->getCacheKey(); } /** * @return array */ public function getLastReceivedToken() { return $this->auth->getLastReceivedToken(); } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Credentials; use Google\Auth\CredentialsLoader; use Google\Auth\HttpHandler\HttpClientCache; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\Iam; use Google\Auth\SignBlobInterface; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\ServerException; use GuzzleHttp\Psr7\Request; /** * GCECredentials supports authorization on Google Compute Engine. * * It can be used to authorize requests using the AuthTokenMiddleware, but will * only succeed if being run on GCE: * * use Google\Auth\Credentials\GCECredentials; * use Google\Auth\Middleware\AuthTokenMiddleware; * use GuzzleHttp\Client; * use GuzzleHttp\HandlerStack; * * $gce = new GCECredentials(); * $middleware = new AuthTokenMiddleware($gce); * $stack = HandlerStack::create(); * $stack->push($middleware); * * $client = new Client([ * 'handler' => $stack, * 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', * 'auth' => 'google_auth' * ]); * * $res = $client->get('myproject/taskqueues/myqueue'); */ class GCECredentials extends CredentialsLoader implements SignBlobInterface { const cacheKey = 'GOOGLE_AUTH_PHP_GCE'; /** * The metadata IP address on appengine instances. * * The IP is used instead of the domain 'metadata' to avoid slow responses * when not on Compute Engine. */ const METADATA_IP = '169.254.169.254'; /** * The metadata path of the default token. */ const TOKEN_URI_PATH = 'v1/instance/service-accounts/default/token'; /** * The metadata path of the client ID. */ const CLIENT_ID_URI_PATH = 'v1/instance/service-accounts/default/email'; /** * The header whose presence indicates GCE presence. */ const FLAVOR_HEADER = 'Metadata-Flavor'; /** * Note: the explicit `timeout` and `tries` below is a workaround. The underlying * issue is that resolving an unknown host on some networks will take * 20-30 seconds; making this timeout short fixes the issue, but * could lead to false negatives in the event that we are on GCE, but * the metadata resolution was particularly slow. The latter case is * "unlikely" since the expected 4-nines time is about 0.5 seconds. * This allows us to limit the total ping maximum timeout to 1.5 seconds * for developer desktop scenarios. */ const MAX_COMPUTE_PING_TRIES = 3; const COMPUTE_PING_CONNECTION_TIMEOUT_S = 0.5; /** * Flag used to ensure that the onGCE test is only done once;. * * @var bool */ private $hasCheckedOnGce = false; /** * Flag that stores the value of the onGCE check. * * @var bool */ private $isOnGce = false; /** * Result of fetchAuthToken. */ protected $lastReceivedToken; /** * @var string */ private $clientName; /** * @var Iam|null */ private $iam; /** * @var string */ private $tokenUri; /** * @param Iam $iam [optional] An IAM instance. * @param string|array $scope [optional] the scope of the access request, * expressed either as an array or as a space-delimited string. */ public function __construct(Iam $iam = null, $scope = null) { $this->iam = $iam; $tokenUri = self::getTokenUri(); if ($scope) { if (is_string($scope)) { $scope = explode(' ', $scope); } $scope = implode(',', $scope); $tokenUri = $tokenUri . '?scopes='. $scope; } $this->tokenUri = $tokenUri; } /** * The full uri for accessing the default token. * * @return string */ public static function getTokenUri() { $base = 'http://' . self::METADATA_IP . '/computeMetadata/'; return $base . self::TOKEN_URI_PATH; } /** * The full uri for accessing the default service account. * * @return string */ public static function getClientNameUri() { $base = 'http://' . self::METADATA_IP . '/computeMetadata/'; return $base . self::CLIENT_ID_URI_PATH; } /** * Determines if this an App Engine Flexible instance, by accessing the * GAE_INSTANCE environment variable. * * @return true if this an App Engine Flexible Instance, false otherwise */ public static function onAppEngineFlexible() { return substr(getenv('GAE_INSTANCE'), 0, 4) === 'aef-'; } /** * Determines if this a GCE instance, by accessing the expected metadata * host. * If $httpHandler is not specified a the default HttpHandler is used. * * @param callable $httpHandler callback which delivers psr7 request * * @return true if this a GCEInstance false otherwise */ public static function onGce(callable $httpHandler = null) { $httpHandler = $httpHandler ?: HttpHandlerFactory::build(HttpClientCache::getHttpClient()); $checkUri = 'http://' . self::METADATA_IP; for ($i = 1; $i <= self::MAX_COMPUTE_PING_TRIES; $i++) { try { // Comment from: oauth2client/client.py // // Note: the explicit `timeout` below is a workaround. The underlying // issue is that resolving an unknown host on some networks will take // 20-30 seconds; making this timeout short fixes the issue, but // could lead to false negatives in the event that we are on GCE, but // the metadata resolution was particularly slow. The latter case is // "unlikely". $resp = $httpHandler( new Request( 'GET', $checkUri, [self::FLAVOR_HEADER => 'Google'] ), ['timeout' => self::COMPUTE_PING_CONNECTION_TIMEOUT_S] ); return $resp->getHeaderLine(self::FLAVOR_HEADER) == 'Google'; } catch (ClientException $e) { } catch (ServerException $e) { } catch (RequestException $e) { } } return false; } /** * Implements FetchAuthTokenInterface#fetchAuthToken. * * Fetches the auth tokens from the GCE metadata host if it is available. * If $httpHandler is not specified a the default HttpHandler is used. * * @param callable $httpHandler callback which delivers psr7 request * * @return array A set of auth related metadata, containing the following * keys: * - access_token (string) * - expires_in (int) * - token_type (string) * * @throws \Exception */ public function fetchAuthToken(callable $httpHandler = null) { $httpHandler = $httpHandler ?: HttpHandlerFactory::build(HttpClientCache::getHttpClient()); if (!$this->hasCheckedOnGce) { $this->isOnGce = self::onGce($httpHandler); $this->hasCheckedOnGce = true; } if (!$this->isOnGce) { return array(); // return an empty array with no access token } $json = $this->getFromMetadata($httpHandler, $this->tokenUri); if (null === $json = json_decode($json, true)) { throw new \Exception('Invalid JSON response'); } // store this so we can retrieve it later $this->lastReceivedToken = $json; $this->lastReceivedToken['expires_at'] = time() + $json['expires_in']; return $json; } /** * @return string */ public function getCacheKey() { return self::cacheKey; } /** * @return array|null */ public function getLastReceivedToken() { if ($this->lastReceivedToken) { return [ 'access_token' => $this->lastReceivedToken['access_token'], 'expires_at' => $this->lastReceivedToken['expires_at'], ]; } return null; } /** * Get the client name from GCE metadata. * * Subsequent calls will return a cached value. * * @param callable $httpHandler callback which delivers psr7 request * @return string */ public function getClientName(callable $httpHandler = null) { if ($this->clientName) { return $this->clientName; } $httpHandler = $httpHandler ?: HttpHandlerFactory::build(HttpClientCache::getHttpClient()); if (!$this->hasCheckedOnGce) { $this->isOnGce = self::onGce($httpHandler); $this->hasCheckedOnGce = true; } if (!$this->isOnGce) { return ''; } $this->clientName = $this->getFromMetadata($httpHandler, self::getClientNameUri()); return $this->clientName; } /** * Sign a string using the default service account private key. * * This implementation uses IAM's signBlob API. * * @see https://cloud.google.com/iam/credentials/reference/rest/v1/projects.serviceAccounts/signBlob SignBlob * * @param string $stringToSign The string to sign. * @param bool $forceOpenSsl [optional] Does not apply to this credentials * type. * @return string */ public function signBlob($stringToSign, $forceOpenSsl = false) { $httpHandler = HttpHandlerFactory::build(HttpClientCache::getHttpClient()); // Providing a signer is useful for testing, but it's undocumented // because it's not something a user would generally need to do. $signer = $this->iam ?: new Iam($httpHandler); $email = $this->getClientName($httpHandler); $previousToken = $this->getLastReceivedToken(); $accessToken = $previousToken ? $previousToken['access_token'] : $this->fetchAuthToken($httpHandler)['access_token']; return $signer->signBlob($email, $accessToken, $stringToSign); } /** * Fetch the value of a GCE metadata server URI. * * @param callable $httpHandler An HTTP Handler to deliver PSR7 requests. * @param string $uri The metadata URI. * @return string */ private function getFromMetadata(callable $httpHandler, $uri) { $resp = $httpHandler( new Request( 'GET', $uri, [self::FLAVOR_HEADER => 'Google'] ) ); return (string) $resp->getBody(); } } <?php /* * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth; use Firebase\JWT\ExpiredException; use Firebase\JWT\JWT; use Firebase\JWT\SignatureInvalidException; use Google\Auth\Cache\MemoryCacheItemPool; use Google\Auth\HttpHandler\HttpClientCache; use Google\Auth\HttpHandler\HttpHandlerFactory; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use phpseclib\Crypt\RSA; use phpseclib\Math\BigInteger; use Psr\Cache\CacheItemPoolInterface; /** * Wrapper around Google Access Tokens which provides convenience functions. * * @experimental */ class AccessToken { const FEDERATED_SIGNON_CERT_URL = 'https://www.googleapis.com/oauth2/v3/certs'; const OAUTH2_ISSUER = 'accounts.google.com'; const OAUTH2_ISSUER_HTTPS = 'https://accounts.google.com'; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; /** * @var callable */ private $httpHandler; /** * @var CacheItemPoolInterface */ private $cache; /** * @param callable $httpHandler [optional] An HTTP Handler to deliver PSR-7 requests. * @param CacheItemPoolInterface $cache [optional] A PSR-6 compatible cache implementation. */ public function __construct( callable $httpHandler = null, CacheItemPoolInterface $cache = null ) { // @codeCoverageIgnoreStart if (!class_exists('phpseclib\Crypt\RSA')) { throw new \RuntimeException('Please require phpseclib/phpseclib v2 to use this utility.'); } // @codeCoverageIgnoreEnd $this->httpHandler = $httpHandler ?: HttpHandlerFactory::build(HttpClientCache::getHttpClient()); $this->cache = $cache ?: new MemoryCacheItemPool(); $this->configureJwtService(); // set phpseclib constants if applicable $this->setPhpsecConstants(); } /** * Verifies an id token and returns the authenticated apiLoginTicket. * Throws an exception if the id token is not valid. * The audience parameter can be used to control which id tokens are * accepted. By default, the id token must have been issued to this OAuth2 client. * * @param string $token The JSON Web Token to be verified. * @param array $options [optional] { * Configuration options. * * @type string $audience The indended recipient of the token. * @type string $certsLocation The location (remote or local) from which * to retrieve certificates, if not cached. This value should only be * provided in limited circumstances in which you are sure of the * behavior. * } * @return array|bool the token payload, if successful, or false if not. * @throws \InvalidArgumentException If certs could not be retrieved from a local file. * @throws \InvalidArgumentException If received certs are in an invalid format. * @throws \RuntimeException If certs could not be retrieved from a remote location. */ public function verify($token, array $options = []) { $audience = isset($options['audience']) ? $options['audience'] : null; $certsLocation = isset($options['certsLocation']) ? $options['certsLocation'] : self::FEDERATED_SIGNON_CERT_URL; unset($options['audience'], $options['certsLocation']); // Check signature against each available cert. // allow the loop to complete unless a known bad result is encountered. $certs = $this->getFederatedSignOnCerts($certsLocation, $options); foreach ($certs as $cert) { $rsa = new RSA(); $rsa->loadKey([ 'n' => new BigInteger($this->callJwtStatic('urlsafeB64Decode', [ $cert['n'] ]), 256), 'e' => new BigInteger($this->callJwtStatic('urlsafeB64Decode', [ $cert['e'] ]), 256) ]); try { $pubkey = $rsa->getPublicKey(); $payload = $this->callJwtStatic('decode', [ $token, $pubkey, ['RS256'] ]); if (property_exists($payload, 'aud')) { if ($audience && $payload->aud != $audience) { return false; } } // support HTTP and HTTPS issuers // @see https://developers.google.com/identity/sign-in/web/backend-auth $issuers = [self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS]; if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) { return false; } return (array) $payload; } catch (ExpiredException $e) { return false; } catch (\ExpiredException $e) { // (firebase/php-jwt 2) return false; } catch (SignatureInvalidException $e) { // continue } catch (\SignatureInvalidException $e) { // continue (firebase/php-jwt 2) } catch (\DomainException $e) { // continue } } return false; } /** * Revoke an OAuth2 access token or refresh token. This method will revoke the current access * token, if a token isn't provided. * * @param string|array $token The token (access token or a refresh token) that should be revoked. * @param array $options [optional] Configuration options. * @return boolean Returns True if the revocation was successful, otherwise False. */ public function revoke($token, array $options = []) { if (is_array($token)) { if (isset($token['refresh_token'])) { $token = $token['refresh_token']; } else { $token = $token['access_token']; } } $body = Psr7\stream_for(http_build_query(['token' => $token])); $request = new Request('POST', self::OAUTH2_REVOKE_URI, [ 'Cache-Control' => 'no-store', 'Content-Type' => 'application/x-www-form-urlencoded', ], $body); $httpHandler = $this->httpHandler; $response = $httpHandler($request, $options); return $response->getStatusCode() == 200; } /** * Gets federated sign-on certificates to use for verifying identity tokens. * Returns certs as array structure, where keys are key ids, and values * are PEM encoded certificates. * * @param string $location The location from which to retrieve certs. * @param array $options [optional] Configuration options. * @return array * @throws \InvalidArgumentException If received certs are in an invalid format. */ private function getFederatedSignOnCerts($location, array $options = []) { $cacheItem = $this->cache->getItem('federated_signon_certs_v3'); $certs = $cacheItem ? $cacheItem->get() : null; $gotNewCerts = false; if (!$certs) { $certs = $this->retrieveCertsFromLocation($location, $options); $gotNewCerts = true; } if (!isset($certs['keys'])) { throw new \InvalidArgumentException( 'federated sign-on certs expects "keys" to be set' ); } // Push caching off until after verifying certs are in a valid format. // Don't want to cache bad data. if ($gotNewCerts) { $cacheItem->expiresAt(new \DateTime('+1 hour')); $cacheItem->set($certs); $this->cache->save($cacheItem); } return $certs['keys']; } /** * Retrieve and cache a certificates file. * * @param $url string location * @param array $options [optional] Configuration options. * @throws \RuntimeException * @return array certificates * @throws \InvalidArgumentException If certs could not be retrieved from a local file. * @throws \RuntimeException If certs could not be retrieved from a remote location. */ private function retrieveCertsFromLocation($url, array $options = []) { // If we're retrieving a local file, just grab it. if (strpos($url, 'http') !== 0) { if (!file_exists($url)) { throw new \InvalidArgumentException(sprintf( 'Failed to retrieve verification certificates from path: %s.', $url )); } return json_decode(file_get_contents($url), true); } $httpHandler = $this->httpHandler; $response = $httpHandler(new Request('GET', $url), $options); if ($response->getStatusCode() == 200) { return json_decode((string) $response->getBody(), true); } throw new \RuntimeException(sprintf( 'Failed to retrieve verification certificates: "%s".', $response->getBody()->getContents() ), $response->getStatusCode()); } /** * Set required defaults for JWT. */ private function configureJwtService() { $class = class_exists('Firebase\JWT\JWT') ? 'Firebase\JWT\JWT' : '\JWT'; if (property_exists($class, 'leeway') && $class::$leeway < 1) { // Ensures JWT leeway is at least 1 // @see https://github.com/google/google-api-php-client/issues/827 $class::$leeway = 1; } } /** * phpseclib calls "phpinfo" by default, which requires special * whitelisting in the AppEngine VM environment. This function * sets constants to bypass the need for phpseclib to check phpinfo * * @see phpseclib/Math/BigInteger * @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85 * @codeCoverageIgnore */ private function setPhpsecConstants() { if (filter_var(getenv('GAE_VM'), FILTER_VALIDATE_BOOLEAN)) { if (!defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); } if (!defined('CRYPT_RSA_MODE')) { define('CRYPT_RSA_MODE', RSA::MODE_OPENSSL); } } } /** * Provide a hook to mock calls to the JWT static methods. * * @param string $method * @param array $args * @return mixed */ protected function callJwtStatic($method, array $args = []) { $class = class_exists('Firebase\JWT\JWT') ? 'Firebase\JWT\JWT' : 'JWT'; return call_user_func_array([$class, $method], $args); } } <?php /* * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth; /** * Describes a class which supports signing arbitrary strings. */ interface SignBlobInterface extends FetchAuthTokenInterface { /** * Sign a string using the method which is best for a given credentials type. * * @param string $stringToSign The string to sign. * @param bool $forceOpenssl Require use of OpenSSL for local signing. Does * not apply to signing done using external services. **Defaults to** * `false`. * @return string The resulting signature. Value should be base64-encoded. */ public function signBlob($stringToSign, $forceOpenssl = false); /** * Returns the current Client Name. * * @param callable $httpHandler callback which delivers psr7 request, if * one is required to obtain a client name. * @return string */ public function getClientName(callable $httpHandler = null); } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Subscriber; use GuzzleHttp\Event\BeforeEvent; use GuzzleHttp\Event\RequestEvents; use GuzzleHttp\Event\SubscriberInterface; /** * SimpleSubscriber is a Guzzle Subscriber that implements Google's Simple API * access. * * Requests are accessed using the Simple API access developer key. */ class SimpleSubscriber implements SubscriberInterface { /** * @var array */ private $config; /** * Create a new Simple plugin. * * The configuration array expects one option * - key: required, otherwise InvalidArgumentException is thrown * * @param array $config Configuration array */ public function __construct(array $config) { if (!isset($config['key'])) { throw new \InvalidArgumentException('requires a key to have been set'); } $this->config = array_merge([], $config); } /** * @return array */ public function getEvents() { return ['before' => ['onBefore', RequestEvents::SIGN_REQUEST]]; } /** * Updates the request query with the developer key if auth is set to simple. * * use Google\Auth\Subscriber\SimpleSubscriber; * use GuzzleHttp\Client; * * $my_key = 'is not the same as yours'; * $subscriber = new SimpleSubscriber(['key' => $my_key]); * * $client = new Client([ * 'base_url' => 'https://www.googleapis.com/discovery/v1/', * 'defaults' => ['auth' => 'simple'] * ]); * $client->getEmitter()->attach($subscriber); * * $res = $client->get('drive/v2/rest'); * * @param BeforeEvent $event */ public function onBefore(BeforeEvent $event) { // Requests using "auth"="simple" with the developer key. $request = $event->getRequest(); if ($request->getConfig()['auth'] != 'simple') { return; } $request->getQuery()->overwriteWith($this->config); } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Subscriber; use Google\Auth\CacheTrait; use GuzzleHttp\Event\BeforeEvent; use GuzzleHttp\Event\RequestEvents; use GuzzleHttp\Event\SubscriberInterface; use Psr\Cache\CacheItemPoolInterface; /** * ScopedAccessTokenSubscriber is a Guzzle Subscriber that adds an Authorization * header provided by a closure. * * The closure returns an access token, taking the scope, either a single * string or an array of strings, as its value. If provided, a cache will be * used to preserve the access token for a given lifetime. * * Requests will be accessed with the authorization header: * * 'authorization' 'Bearer <access token obtained from the closure>' */ class ScopedAccessTokenSubscriber implements SubscriberInterface { use CacheTrait; const DEFAULT_CACHE_LIFETIME = 1500; /** * @var CacheItemPoolInterface */ private $cache; /** * @var callable The access token generator function */ private $tokenFunc; /** * @var array|string The scopes used to generate the token */ private $scopes; /** * @var array */ private $cacheConfig; /** * Creates a new ScopedAccessTokenSubscriber. * * @param callable $tokenFunc a token generator function * @param array|string $scopes the token authentication scopes * @param array $cacheConfig configuration for the cache when it's present * @param CacheItemPoolInterface $cache an implementation of CacheItemPoolInterface */ public function __construct( callable $tokenFunc, $scopes, array $cacheConfig = null, CacheItemPoolInterface $cache = null ) { $this->tokenFunc = $tokenFunc; if (!(is_string($scopes) || is_array($scopes))) { throw new \InvalidArgumentException( 'wants scope should be string or array'); } $this->scopes = $scopes; if (!is_null($cache)) { $this->cache = $cache; $this->cacheConfig = array_merge([ 'lifetime' => self::DEFAULT_CACHE_LIFETIME, 'prefix' => '', ], $cacheConfig); } } /** * @return array */ public function getEvents() { return ['before' => ['onBefore', RequestEvents::SIGN_REQUEST]]; } /** * Updates the request with an Authorization header when auth is 'scoped'. * * E.g this could be used to authenticate using the AppEngine * AppIdentityService. * * use google\appengine\api\app_identity\AppIdentityService; * use Google\Auth\Subscriber\ScopedAccessTokenSubscriber; * use GuzzleHttp\Client; * * $scope = 'https://www.googleapis.com/auth/taskqueue' * $subscriber = new ScopedAccessToken( * 'AppIdentityService::getAccessToken', * $scope, * ['prefix' => 'Google\Auth\ScopedAccessToken::'], * $cache = new Memcache() * ); * * $client = new Client([ * 'base_url' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', * 'defaults' => ['auth' => 'scoped'] * ]); * $client->getEmitter()->attach($subscriber); * * $res = $client->get('myproject/taskqueues/myqueue'); * * @param BeforeEvent $event */ public function onBefore(BeforeEvent $event) { // Requests using "auth"="scoped" will be authorized. $request = $event->getRequest(); if ($request->getConfig()['auth'] != 'scoped') { return; } $auth_header = 'Bearer ' . $this->fetchToken(); $request->setHeader('authorization', $auth_header); } /** * @return string */ private function getCacheKey() { $key = null; if (is_string($this->scopes)) { $key .= $this->scopes; } elseif (is_array($this->scopes)) { $key .= implode(':', $this->scopes); } return $key; } /** * Determine if token is available in the cache, if not call tokenFunc to * fetch it. * * @return string */ private function fetchToken() { $cacheKey = $this->getCacheKey(); $cached = $this->getCachedValue($cacheKey); if (!empty($cached)) { return $cached; } $token = call_user_func($this->tokenFunc, $this->scopes); $this->setCachedValue($cacheKey, $token); return $token; } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth\Subscriber; use Google\Auth\FetchAuthTokenInterface; use GuzzleHttp\Event\BeforeEvent; use GuzzleHttp\Event\RequestEvents; use GuzzleHttp\Event\SubscriberInterface; /** * AuthTokenSubscriber is a Guzzle Subscriber that adds an Authorization header * provided by an object implementing FetchAuthTokenInterface. * * The FetchAuthTokenInterface#fetchAuthToken is used to obtain a hash; one of * the values value in that hash is added as the authorization header. * * Requests will be accessed with the authorization header: * * 'authorization' 'Bearer <value of auth_token>' */ class AuthTokenSubscriber implements SubscriberInterface { /** * @var callable */ private $httpHandler; /** * @var FetchAuthTokenInterface */ private $fetcher; /** * @var callable */ private $tokenCallback; /** * Creates a new AuthTokenSubscriber. * * @param FetchAuthTokenInterface $fetcher is used to fetch the auth token * @param callable $httpHandler (optional) http client to fetch the token. * @param callable $tokenCallback (optional) function to be called when a new token is fetched. */ public function __construct( FetchAuthTokenInterface $fetcher, callable $httpHandler = null, callable $tokenCallback = null ) { $this->fetcher = $fetcher; $this->httpHandler = $httpHandler; $this->tokenCallback = $tokenCallback; } /** * @return array */ public function getEvents() { return ['before' => ['onBefore', RequestEvents::SIGN_REQUEST]]; } /** * Updates the request with an Authorization header when auth is 'fetched_auth_token'. * * use GuzzleHttp\Client; * use Google\Auth\OAuth2; * use Google\Auth\Subscriber\AuthTokenSubscriber; * * $config = [..<oauth config param>.]; * $oauth2 = new OAuth2($config) * $subscriber = new AuthTokenSubscriber($oauth2); * * $client = new Client([ * 'base_url' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', * 'defaults' => ['auth' => 'google_auth'] * ]); * $client->getEmitter()->attach($subscriber); * * $res = $client->get('myproject/taskqueues/myqueue'); * * @param BeforeEvent $event */ public function onBefore(BeforeEvent $event) { // Requests using "auth"="google_auth" will be authorized. $request = $event->getRequest(); if ($request->getConfig()['auth'] != 'google_auth') { return; } // Fetch the auth token. $auth_tokens = $this->fetcher->fetchAuthToken($this->httpHandler); if (array_key_exists('access_token', $auth_tokens)) { $request->setHeader('authorization', 'Bearer ' . $auth_tokens['access_token']); // notify the callback if applicable if ($this->tokenCallback) { call_user_func($this->tokenCallback, $this->fetcher->getCacheKey(), $auth_tokens['access_token']); } } } } <?php /* * Copyright 2010 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth; use Psr\Cache\CacheItemPoolInterface; /** * A class to implement caching for any object implementing * FetchAuthTokenInterface */ class FetchAuthTokenCache implements FetchAuthTokenInterface, SignBlobInterface { use CacheTrait; /** * @var FetchAuthTokenInterface */ private $fetcher; /** * @var array */ private $cacheConfig; /** * @var CacheItemPoolInterface */ private $cache; public function __construct( FetchAuthTokenInterface $fetcher, array $cacheConfig = null, CacheItemPoolInterface $cache ) { $this->fetcher = $fetcher; $this->cache = $cache; $this->cacheConfig = array_merge([ 'lifetime' => 1500, 'prefix' => '', ], (array) $cacheConfig); } /** * Implements FetchAuthTokenInterface#fetchAuthToken. * * Checks the cache for a valid auth token and fetches the auth tokens * from the supplied fetcher. * * @param callable $httpHandler callback which delivers psr7 request * * @return array the response * * @throws \Exception */ public function fetchAuthToken(callable $httpHandler = null) { // Use the cached value if its available. // // TODO: correct caching; update the call to setCachedValue to set the expiry // to the value returned with the auth token. // // TODO: correct caching; enable the cache to be cleared. $cacheKey = $this->fetcher->getCacheKey(); $cached = $this->getCachedValue($cacheKey); if (!empty($cached)) { return ['access_token' => $cached]; } $auth_token = $this->fetcher->fetchAuthToken($httpHandler); if (isset($auth_token['access_token'])) { $this->setCachedValue($cacheKey, $auth_token['access_token']); } return $auth_token; } /** * @return string */ public function getCacheKey() { return $this->getFullCacheKey($this->fetcher->getCacheKey()); } /** * @return array|null */ public function getLastReceivedToken() { return $this->fetcher->getLastReceivedToken(); } /** * Get the client name from the fetcher. * * @param callable $httpHandler An HTTP handler to deliver PSR7 requests. * @return string */ public function getClientName(callable $httpHandler = null) { return $this->fetcher->getClientName($httpHandler); } /** * Sign a blob using the fetcher. * * @param string $stringToSign The string to sign. * @param bool $forceOpenssl Require use of OpenSSL for local signing. Does * not apply to signing done using external services. **Defaults to** * `false`. * @return string The resulting signature. * @throws \RuntimeException If the fetcher does not implement * `Google\Auth\SignBlobInterface`. */ public function signBlob($stringToSign, $forceOpenSsl = false) { if (!$this->fetcher instanceof SignBlobInterface) { throw new \RuntimeException( 'Credentials fetcher does not implement ' . 'Google\Auth\SignBlobInterface' ); } return $this->fetcher->signBlob($stringToSign, $forceOpenSsl); } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth; use DomainException; use Google\Auth\Credentials\AppIdentityCredentials; use Google\Auth\Credentials\GCECredentials; use Google\Auth\HttpHandler\HttpClientCache; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\Middleware\AuthTokenMiddleware; use Google\Auth\Subscriber\AuthTokenSubscriber; use GuzzleHttp\Client; use Psr\Cache\CacheItemPoolInterface; /** * ApplicationDefaultCredentials obtains the default credentials for * authorizing a request to a Google service. * * Application Default Credentials are described here: * https://developers.google.com/accounts/docs/application-default-credentials * * This class implements the search for the application default credentials as * described in the link. * * It provides three factory methods: * - #get returns the computed credentials object * - #getSubscriber returns an AuthTokenSubscriber built from the credentials object * - #getMiddleware returns an AuthTokenMiddleware built from the credentials object * * This allows it to be used as follows with GuzzleHttp\Client: * * use Google\Auth\ApplicationDefaultCredentials; * use GuzzleHttp\Client; * use GuzzleHttp\HandlerStack; * * $middleware = ApplicationDefaultCredentials::getMiddleware( * 'https://www.googleapis.com/auth/taskqueue' * ); * $stack = HandlerStack::create(); * $stack->push($middleware); * * $client = new Client([ * 'handler' => $stack, * 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', * 'auth' => 'google_auth' // authorize all requests * ]); * * $res = $client->get('myproject/taskqueues/myqueue'); */ class ApplicationDefaultCredentials { /** * Obtains an AuthTokenSubscriber that uses the default FetchAuthTokenInterface * implementation to use in this environment. * * If supplied, $scope is used to in creating the credentials instance if * this does not fallback to the compute engine defaults. * * @param string|array scope the scope of the access request, expressed * either as an Array or as a space-delimited String. * @param callable $httpHandler callback which delivers psr7 request * @param array $cacheConfig configuration for the cache when it's present * @param CacheItemPoolInterface $cache an implementation of CacheItemPoolInterface * * @return AuthTokenSubscriber * * @throws DomainException if no implementation can be obtained. */ public static function getSubscriber( $scope = null, callable $httpHandler = null, array $cacheConfig = null, CacheItemPoolInterface $cache = null ) { $creds = self::getCredentials($scope, $httpHandler, $cacheConfig, $cache); return new AuthTokenSubscriber($creds, $httpHandler); } /** * Obtains an AuthTokenMiddleware that uses the default FetchAuthTokenInterface * implementation to use in this environment. * * If supplied, $scope is used to in creating the credentials instance if * this does not fallback to the compute engine defaults. * * @param string|array scope the scope of the access request, expressed * either as an Array or as a space-delimited String. * @param callable $httpHandler callback which delivers psr7 request * @param array $cacheConfig configuration for the cache when it's present * @param CacheItemPoolInterface $cache * * @return AuthTokenMiddleware * * @throws DomainException if no implementation can be obtained. */ public static function getMiddleware( $scope = null, callable $httpHandler = null, array $cacheConfig = null, CacheItemPoolInterface $cache = null ) { $creds = self::getCredentials($scope, $httpHandler, $cacheConfig, $cache); return new AuthTokenMiddleware($creds, $httpHandler); } /** * Obtains the default FetchAuthTokenInterface implementation to use * in this environment. * * If supplied, $scope is used to in creating the credentials instance if * this does not fallback to the Compute Engine defaults. * * @param string|array scope the scope of the access request, expressed * either as an Array or as a space-delimited String. * @param callable $httpHandler callback which delivers psr7 request * @param array $cacheConfig configuration for the cache when it's present * @param CacheItemPoolInterface $cache * * @return CredentialsLoader * * @throws DomainException if no implementation can be obtained. */ public static function getCredentials( $scope = null, callable $httpHandler = null, array $cacheConfig = null, CacheItemPoolInterface $cache = null ) { $creds = null; $jsonKey = CredentialsLoader::fromEnv() ?: CredentialsLoader::fromWellKnownFile(); if (!$httpHandler) { if (!($client = HttpClientCache::getHttpClient())) { $client = new Client(); HttpClientCache::setHttpClient($client); } $httpHandler = HttpHandlerFactory::build($client); } if (!is_null($jsonKey)) { $creds = CredentialsLoader::makeCredentials($scope, $jsonKey); } elseif (AppIdentityCredentials::onAppEngine() && !GCECredentials::onAppEngineFlexible()) { $creds = new AppIdentityCredentials($scope); } elseif (GCECredentials::onGce($httpHandler)) { $creds = new GCECredentials(null, $scope); } if (is_null($creds)) { throw new \DomainException(self::notFound()); } if (!is_null($cache)) { $creds = new FetchAuthTokenCache($creds, $cacheConfig, $cache); } return $creds; } private static function notFound() { $msg = 'Could not load the default credentials. Browse to '; $msg .= 'https://developers.google.com'; $msg .= '/accounts/docs/application-default-credentials'; $msg .= ' for more information'; return $msg; } } <?php /* * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth; use Google\Auth\HttpHandler\HttpClientCache; use Google\Auth\HttpHandler\HttpHandlerFactory; use GuzzleHttp\Psr7; /** * Tools for using the IAM API. * * @see https://cloud.google.com/iam/docs IAM Documentation */ class Iam { const IAM_API_ROOT = 'https://iamcredentials.googleapis.com/v1'; const SIGN_BLOB_PATH = '%s:signBlob?alt=json'; const SERVICE_ACCOUNT_NAME = 'projects/-/serviceAccounts/%s'; /** * @var callable */ private $httpHandler; /** * @param callable $httpHandler [optional] The HTTP Handler to send requests. */ public function __construct(callable $httpHandler = null) { $this->httpHandler = $httpHandler ?: HttpHandlerFactory::build(HttpClientCache::getHttpClient()); } /** * Sign a string using the IAM signBlob API. * * Note that signing using IAM requires your service account to have the * `iam.serviceAccounts.signBlob` permission, part of the "Service Account * Token Creator" IAM role. * * @param string $email The service account email. * @param string $accessToken An access token from the service account. * @param string $stringToSign The string to be signed. * @param array $delegates [optional] A list of service account emails to * add to the delegate chain. If omitted, the value of `$email` will * be used. * @return string The signed string, base64-encoded. */ public function signBlob($email, $accessToken, $stringToSign, array $delegates = []) { $httpHandler = $this->httpHandler; $name = sprintf(self::SERVICE_ACCOUNT_NAME, $email); $uri = self::IAM_API_ROOT . '/' . sprintf(self::SIGN_BLOB_PATH, $name); if ($delegates) { foreach ($delegates as &$delegate) { $delegate = sprintf(self::SERVICE_ACCOUNT_NAME, $delegate); } } else { $delegates = [$name]; } $body = [ 'delegates' => $delegates, 'payload' => base64_encode($stringToSign), ]; $headers = [ 'Authorization' => 'Bearer ' . $accessToken ]; $request = new Psr7\Request( 'POST', $uri, $headers, Psr7\stream_for(json_encode($body)) ); $res = $httpHandler($request); $body = json_decode((string) $res->getBody(), true); return $body['signedBlob']; } } <?php /* * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth; use Google\Auth\HttpHandler\HttpClientCache; use Google\Auth\HttpHandler\HttpHandlerFactory; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use InvalidArgumentException; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; /** * OAuth2 supports authentication by OAuth2 2-legged flows. * * It primary supports * - service account authorization * - authorization where a user already has an access token */ class OAuth2 implements FetchAuthTokenInterface { const DEFAULT_EXPIRY_SECONDS = 3600; // 1 hour const DEFAULT_SKEW_SECONDS = 60; // 1 minute const JWT_URN = 'urn:ietf:params:oauth:grant-type:jwt-bearer'; /** * TODO: determine known methods from the keys of JWT::methods. */ public static $knownSigningAlgorithms = array( 'HS256', 'HS512', 'HS384', 'RS256', ); /** * The well known grant types. * * @var array */ public static $knownGrantTypes = array( 'authorization_code', 'refresh_token', 'password', 'client_credentials', ); /** * - authorizationUri * The authorization server's HTTP endpoint capable of * authenticating the end-user and obtaining authorization. * * @var UriInterface */ private $authorizationUri; /** * - tokenCredentialUri * The authorization server's HTTP endpoint capable of issuing * tokens and refreshing expired tokens. * * @var UriInterface */ private $tokenCredentialUri; /** * The redirection URI used in the initial request. * * @var string */ private $redirectUri; /** * A unique identifier issued to the client to identify itself to the * authorization server. * * @var string */ private $clientId; /** * A shared symmetric secret issued by the authorization server, which is * used to authenticate the client. * * @var string */ private $clientSecret; /** * The resource owner's username. * * @var string */ private $username; /** * The resource owner's password. * * @var string */ private $password; /** * The scope of the access request, expressed either as an Array or as a * space-delimited string. * * @var string */ private $scope; /** * An arbitrary string designed to allow the client to maintain state. * * @var string */ private $state; /** * The authorization code issued to this client. * * Only used by the authorization code access grant type. * * @var string */ private $code; /** * The issuer ID when using assertion profile. * * @var string */ private $issuer; /** * The target audience for assertions. * * @var string */ private $audience; /** * The target sub when issuing assertions. * * @var string */ private $sub; /** * The number of seconds assertions are valid for. * * @var int */ private $expiry; /** * The signing key when using assertion profile. * * @var string */ private $signingKey; /** * The signing algorithm when using an assertion profile. * * @var string */ private $signingAlgorithm; /** * The refresh token associated with the access token to be refreshed. * * @var string */ private $refreshToken; /** * The current access token. * * @var string */ private $accessToken; /** * The current ID token. * * @var string */ private $idToken; /** * The lifetime in seconds of the current access token. * * @var int */ private $expiresIn; /** * The expiration time of the access token as a number of seconds since the * unix epoch. * * @var int */ private $expiresAt; /** * The issue time of the access token as a number of seconds since the unix * epoch. * * @var int */ private $issuedAt; /** * The current grant type. * * @var string */ private $grantType; /** * When using an extension grant type, this is the set of parameters used by * that extension. */ private $extensionParams; /** * When using the toJwt function, these claims will be added to the JWT * payload. */ private $additionalClaims; /** * Create a new OAuthCredentials. * * The configuration array accepts various options * * - authorizationUri * The authorization server's HTTP endpoint capable of * authenticating the end-user and obtaining authorization. * * - tokenCredentialUri * The authorization server's HTTP endpoint capable of issuing * tokens and refreshing expired tokens. * * - clientId * A unique identifier issued to the client to identify itself to the * authorization server. * * - clientSecret * A shared symmetric secret issued by the authorization server, * which is used to authenticate the client. * * - scope * The scope of the access request, expressed either as an Array * or as a space-delimited String. * * - state * An arbitrary string designed to allow the client to maintain state. * * - redirectUri * The redirection URI used in the initial request. * * - username * The resource owner's username. * * - password * The resource owner's password. * * - issuer * Issuer ID when using assertion profile * * - audience * Target audience for assertions * * - expiry * Number of seconds assertions are valid for * * - signingKey * Signing key when using assertion profile * * - refreshToken * The refresh token associated with the access token * to be refreshed. * * - accessToken * The current access token for this client. * * - idToken * The current ID token for this client. * * - extensionParams * When using an extension grant type, this is the set of parameters used * by that extension. * * @param array $config Configuration array */ public function __construct(array $config) { $opts = array_merge([ 'expiry' => self::DEFAULT_EXPIRY_SECONDS, 'extensionParams' => [], 'authorizationUri' => null, 'redirectUri' => null, 'tokenCredentialUri' => null, 'state' => null, 'username' => null, 'password' => null, 'clientId' => null, 'clientSecret' => null, 'issuer' => null, 'sub' => null, 'audience' => null, 'signingKey' => null, 'signingAlgorithm' => null, 'scope' => null, 'additionalClaims' => [], ], $config); $this->setAuthorizationUri($opts['authorizationUri']); $this->setRedirectUri($opts['redirectUri']); $this->setTokenCredentialUri($opts['tokenCredentialUri']); $this->setState($opts['state']); $this->setUsername($opts['username']); $this->setPassword($opts['password']); $this->setClientId($opts['clientId']); $this->setClientSecret($opts['clientSecret']); $this->setIssuer($opts['issuer']); $this->setSub($opts['sub']); $this->setExpiry($opts['expiry']); $this->setAudience($opts['audience']); $this->setSigningKey($opts['signingKey']); $this->setSigningAlgorithm($opts['signingAlgorithm']); $this->setScope($opts['scope']); $this->setExtensionParams($opts['extensionParams']); $this->setAdditionalClaims($opts['additionalClaims']); $this->updateToken($opts); } /** * Verifies the idToken if present. * * - if none is present, return null * - if present, but invalid, raises DomainException. * - otherwise returns the payload in the idtoken as a PHP object. * * if $publicKey is null, the key is decoded without being verified. * * @param string $publicKey The public key to use to authenticate the token * @param array $allowed_algs List of supported verification algorithms * * @return null|object */ public function verifyIdToken($publicKey = null, $allowed_algs = array()) { $idToken = $this->getIdToken(); if (is_null($idToken)) { return null; } $resp = $this->jwtDecode($idToken, $publicKey, $allowed_algs); if (!property_exists($resp, 'aud')) { throw new \DomainException('No audience found the id token'); } if ($resp->aud != $this->getAudience()) { throw new \DomainException('Wrong audience present in the id token'); } return $resp; } /** * Obtains the encoded jwt from the instance data. * * @param array $config array optional configuration parameters * * @return string */ public function toJwt(array $config = []) { if (is_null($this->getSigningKey())) { throw new \DomainException('No signing key available'); } if (is_null($this->getSigningAlgorithm())) { throw new \DomainException('No signing algorithm specified'); } $now = time(); $opts = array_merge([ 'skew' => self::DEFAULT_SKEW_SECONDS, ], $config); $assertion = [ 'iss' => $this->getIssuer(), 'aud' => $this->getAudience(), 'exp' => ($now + $this->getExpiry()), 'iat' => ($now - $opts['skew']), ]; foreach ($assertion as $k => $v) { if (is_null($v)) { throw new \DomainException($k . ' should not be null'); } } if (!(is_null($this->getScope()))) { $assertion['scope'] = $this->getScope(); } if (!(is_null($this->getSub()))) { $assertion['sub'] = $this->getSub(); } $assertion += $this->getAdditionalClaims(); return $this->jwtEncode($assertion, $this->getSigningKey(), $this->getSigningAlgorithm()); } /** * Generates a request for token credentials. * * @return RequestInterface the authorization Url. */ public function generateCredentialsRequest() { $uri = $this->getTokenCredentialUri(); if (is_null($uri)) { throw new \DomainException('No token credential URI was set.'); } $grantType = $this->getGrantType(); $params = array('grant_type' => $grantType); switch ($grantType) { case 'authorization_code': $params['code'] = $this->getCode(); $params['redirect_uri'] = $this->getRedirectUri(); $this->addClientCredentials($params); break; case 'password': $params['username'] = $this->getUsername(); $params['password'] = $this->getPassword(); $this->addClientCredentials($params); break; case 'refresh_token': $params['refresh_token'] = $this->getRefreshToken(); $this->addClientCredentials($params); break; case self::JWT_URN: $params['assertion'] = $this->toJwt(); break; default: if (!is_null($this->getRedirectUri())) { # Grant type was supposed to be 'authorization_code', as there # is a redirect URI. throw new \DomainException('Missing authorization code'); } unset($params['grant_type']); if (!is_null($grantType)) { $params['grant_type'] = $grantType; } $params = array_merge($params, $this->getExtensionParams()); } $headers = [ 'Cache-Control' => 'no-store', 'Content-Type' => 'application/x-www-form-urlencoded', ]; return new Request( 'POST', $uri, $headers, Psr7\build_query($params) ); } /** * Fetches the auth tokens based on the current state. * * @param callable $httpHandler callback which delivers psr7 request * * @return array the response */ public function fetchAuthToken(callable $httpHandler = null) { if (is_null($httpHandler)) { $httpHandler = HttpHandlerFactory::build(HttpClientCache::getHttpClient()); } $response = $httpHandler($this->generateCredentialsRequest()); $credentials = $this->parseTokenResponse($response); $this->updateToken($credentials); return $credentials; } /** * Obtains a key that can used to cache the results of #fetchAuthToken. * * The key is derived from the scopes. * * @return string a key that may be used to cache the auth token. */ public function getCacheKey() { if (is_string($this->scope)) { return $this->scope; } if (is_array($this->scope)) { return implode(':', $this->scope); } // If scope has not set, return null to indicate no caching. return null; } /** * Parses the fetched tokens. * * @param ResponseInterface $resp the response. * * @return array the tokens parsed from the response body. * * @throws \Exception */ public function parseTokenResponse(ResponseInterface $resp) { $body = (string)$resp->getBody(); if ($resp->hasHeader('Content-Type') && $resp->getHeaderLine('Content-Type') == 'application/x-www-form-urlencoded' ) { $res = array(); parse_str($body, $res); return $res; } // Assume it's JSON; if it's not throw an exception if (null === $res = json_decode($body, true)) { throw new \Exception('Invalid JSON response'); } return $res; } /** * Updates an OAuth 2.0 client. * * @example * client.updateToken([ * 'refresh_token' => 'n4E9O119d', * 'access_token' => 'FJQbwq9', * 'expires_in' => 3600 * ]) * * @param array $config * The configuration parameters related to the token. * * - refresh_token * The refresh token associated with the access token * to be refreshed. * * - access_token * The current access token for this client. * * - id_token * The current ID token for this client. * * - expires_in * The time in seconds until access token expiration. * * - expires_at * The time as an integer number of seconds since the Epoch * * - issued_at * The timestamp that the token was issued at. */ public function updateToken(array $config) { $opts = array_merge([ 'extensionParams' => [], 'access_token' => null, 'id_token' => null, 'expires_in' => null, 'expires_at' => null, 'issued_at' => null, ], $config); $this->setExpiresAt($opts['expires_at']); $this->setExpiresIn($opts['expires_in']); // By default, the token is issued at `Time.now` when `expiresIn` is set, // but this can be used to supply a more precise time. if (!is_null($opts['issued_at'])) { $this->setIssuedAt($opts['issued_at']); } $this->setAccessToken($opts['access_token']); $this->setIdToken($opts['id_token']); // The refresh token should only be updated if a value is explicitly // passed in, as some access token responses do not include a refresh // token. if (array_key_exists('refresh_token', $opts)) { $this->setRefreshToken($opts['refresh_token']); } } /** * Builds the authorization Uri that the user should be redirected to. * * @param array $config configuration options that customize the return url * * @return UriInterface the authorization Url. * * @throws InvalidArgumentException */ public function buildFullAuthorizationUri(array $config = []) { if (is_null($this->getAuthorizationUri())) { throw new InvalidArgumentException( 'requires an authorizationUri to have been set'); } $params = array_merge([ 'response_type' => 'code', 'access_type' => 'offline', 'client_id' => $this->clientId, 'redirect_uri' => $this->redirectUri, 'state' => $this->state, 'scope' => $this->getScope(), ], $config); // Validate the auth_params if (is_null($params['client_id'])) { throw new InvalidArgumentException( 'missing the required client identifier'); } if (is_null($params['redirect_uri'])) { throw new InvalidArgumentException('missing the required redirect URI'); } if (!empty($params['prompt']) && !empty($params['approval_prompt'])) { throw new InvalidArgumentException( 'prompt and approval_prompt are mutually exclusive'); } // Construct the uri object; return it if it is valid. $result = clone $this->authorizationUri; $existingParams = Psr7\parse_query($result->getQuery()); $result = $result->withQuery( Psr7\build_query(array_merge($existingParams, $params)) ); if ($result->getScheme() != 'https') { throw new InvalidArgumentException( 'Authorization endpoint must be protected by TLS'); } return $result; } /** * Sets the authorization server's HTTP endpoint capable of authenticating * the end-user and obtaining authorization. * * @param string $uri */ public function setAuthorizationUri($uri) { $this->authorizationUri = $this->coerceUri($uri); } /** * Gets the authorization server's HTTP endpoint capable of authenticating * the end-user and obtaining authorization. * * @return UriInterface */ public function getAuthorizationUri() { return $this->authorizationUri; } /** * Gets the authorization server's HTTP endpoint capable of issuing tokens * and refreshing expired tokens. * * @return string */ public function getTokenCredentialUri() { return $this->tokenCredentialUri; } /** * Sets the authorization server's HTTP endpoint capable of issuing tokens * and refreshing expired tokens. * * @param string $uri */ public function setTokenCredentialUri($uri) { $this->tokenCredentialUri = $this->coerceUri($uri); } /** * Gets the redirection URI used in the initial request. * * @return string */ public function getRedirectUri() { return $this->redirectUri; } /** * Sets the redirection URI used in the initial request. * * @param string $uri */ public function setRedirectUri($uri) { if (is_null($uri)) { $this->redirectUri = null; return; } // redirect URI must be absolute if (!$this->isAbsoluteUri($uri)) { // "postmessage" is a reserved URI string in Google-land // @see https://developers.google.com/identity/sign-in/web/server-side-flow if ('postmessage' !== (string)$uri) { throw new InvalidArgumentException( 'Redirect URI must be absolute'); } } $this->redirectUri = (string)$uri; } /** * Gets the scope of the access requests as a space-delimited String. * * @return string */ public function getScope() { if (is_null($this->scope)) { return $this->scope; } return implode(' ', $this->scope); } /** * Sets the scope of the access request, expressed either as an Array or as * a space-delimited String. * * @param string|array $scope * * @throws InvalidArgumentException */ public function setScope($scope) { if (is_null($scope)) { $this->scope = null; } elseif (is_string($scope)) { $this->scope = explode(' ', $scope); } elseif (is_array($scope)) { foreach ($scope as $s) { $pos = strpos($s, ' '); if ($pos !== false) { throw new InvalidArgumentException( 'array scope values should not contain spaces'); } } $this->scope = $scope; } else { throw new InvalidArgumentException( 'scopes should be a string or array of strings'); } } /** * Gets the current grant type. * * @return string */ public function getGrantType() { if (!is_null($this->grantType)) { return $this->grantType; } // Returns the inferred grant type, based on the current object instance // state. if (!is_null($this->code)) { return 'authorization_code'; } if (!is_null($this->refreshToken)) { return 'refresh_token'; } if (!is_null($this->username) && !is_null($this->password)) { return 'password'; } if (!is_null($this->issuer) && !is_null($this->signingKey)) { return self::JWT_URN; } return null; } /** * Sets the current grant type. * * @param $grantType * * @throws InvalidArgumentException */ public function setGrantType($grantType) { if (in_array($grantType, self::$knownGrantTypes)) { $this->grantType = $grantType; } else { // validate URI if (!$this->isAbsoluteUri($grantType)) { throw new InvalidArgumentException( 'invalid grant type'); } $this->grantType = (string)$grantType; } } /** * Gets an arbitrary string designed to allow the client to maintain state. * * @return string */ public function getState() { return $this->state; } /** * Sets an arbitrary string designed to allow the client to maintain state. * * @param string $state */ public function setState($state) { $this->state = $state; } /** * Gets the authorization code issued to this client. */ public function getCode() { return $this->code; } /** * Sets the authorization code issued to this client. * * @param string $code */ public function setCode($code) { $this->code = $code; } /** * Gets the resource owner's username. */ public function getUsername() { return $this->username; } /** * Sets the resource owner's username. * * @param string $username */ public function setUsername($username) { $this->username = $username; } /** * Gets the resource owner's password. */ public function getPassword() { return $this->password; } /** * Sets the resource owner's password. * * @param $password */ public function setPassword($password) { $this->password = $password; } /** * Sets a unique identifier issued to the client to identify itself to the * authorization server. */ public function getClientId() { return $this->clientId; } /** * Sets a unique identifier issued to the client to identify itself to the * authorization server. * * @param $clientId */ public function setClientId($clientId) { $this->clientId = $clientId; } /** * Gets a shared symmetric secret issued by the authorization server, which * is used to authenticate the client. */ public function getClientSecret() { return $this->clientSecret; } /** * Sets a shared symmetric secret issued by the authorization server, which * is used to authenticate the client. * * @param $clientSecret */ public function setClientSecret($clientSecret) { $this->clientSecret = $clientSecret; } /** * Gets the Issuer ID when using assertion profile. */ public function getIssuer() { return $this->issuer; } /** * Sets the Issuer ID when using assertion profile. * * @param string $issuer */ public function setIssuer($issuer) { $this->issuer = $issuer; } /** * Gets the target sub when issuing assertions. */ public function getSub() { return $this->sub; } /** * Sets the target sub when issuing assertions. * * @param string $sub */ public function setSub($sub) { $this->sub = $sub; } /** * Gets the target audience when issuing assertions. */ public function getAudience() { return $this->audience; } /** * Sets the target audience when issuing assertions. * * @param string $audience */ public function setAudience($audience) { $this->audience = $audience; } /** * Gets the signing key when using an assertion profile. */ public function getSigningKey() { return $this->signingKey; } /** * Sets the signing key when using an assertion profile. * * @param string $signingKey */ public function setSigningKey($signingKey) { $this->signingKey = $signingKey; } /** * Gets the signing algorithm when using an assertion profile. * * @return string */ public function getSigningAlgorithm() { return $this->signingAlgorithm; } /** * Sets the signing algorithm when using an assertion profile. * * @param string $signingAlgorithm */ public function setSigningAlgorithm($signingAlgorithm) { if (is_null($signingAlgorithm)) { $this->signingAlgorithm = null; } elseif (!in_array($signingAlgorithm, self::$knownSigningAlgorithms)) { throw new InvalidArgumentException('unknown signing algorithm'); } else { $this->signingAlgorithm = $signingAlgorithm; } } /** * Gets the set of parameters used by extension when using an extension * grant type. */ public function getExtensionParams() { return $this->extensionParams; } /** * Sets the set of parameters used by extension when using an extension * grant type. * * @param $extensionParams */ public function setExtensionParams($extensionParams) { $this->extensionParams = $extensionParams; } /** * Gets the number of seconds assertions are valid for. */ public function getExpiry() { return $this->expiry; } /** * Sets the number of seconds assertions are valid for. * * @param int $expiry */ public function setExpiry($expiry) { $this->expiry = $expiry; } /** * Gets the lifetime of the access token in seconds. */ public function getExpiresIn() { return $this->expiresIn; } /** * Sets the lifetime of the access token in seconds. * * @param int $expiresIn */ public function setExpiresIn($expiresIn) { if (is_null($expiresIn)) { $this->expiresIn = null; $this->issuedAt = null; } else { $this->issuedAt = time(); $this->expiresIn = (int)$expiresIn; } } /** * Gets the time the current access token expires at. * * @return int */ public function getExpiresAt() { if (!is_null($this->expiresAt)) { return $this->expiresAt; } if (!is_null($this->issuedAt) && !is_null($this->expiresIn)) { return $this->issuedAt + $this->expiresIn; } return null; } /** * Returns true if the acccess token has expired. * * @return bool */ public function isExpired() { $expiration = $this->getExpiresAt(); $now = time(); return !is_null($expiration) && $now >= $expiration; } /** * Sets the time the current access token expires at. * * @param int $expiresAt */ public function setExpiresAt($expiresAt) { $this->expiresAt = $expiresAt; } /** * Gets the time the current access token was issued at. */ public function getIssuedAt() { return $this->issuedAt; } /** * Sets the time the current access token was issued at. * * @param int $issuedAt */ public function setIssuedAt($issuedAt) { $this->issuedAt = $issuedAt; } /** * Gets the current access token. */ public function getAccessToken() { return $this->accessToken; } /** * Sets the current access token. * * @param string $accessToken */ public function setAccessToken($accessToken) { $this->accessToken = $accessToken; } /** * Gets the current ID token. */ public function getIdToken() { return $this->idToken; } /** * Sets the current ID token. * * @param $idToken */ public function setIdToken($idToken) { $this->idToken = $idToken; } /** * Gets the refresh token associated with the current access token. */ public function getRefreshToken() { return $this->refreshToken; } /** * Sets the refresh token associated with the current access token. * * @param $refreshToken */ public function setRefreshToken($refreshToken) { $this->refreshToken = $refreshToken; } /** * Sets additional claims to be included in the JWT token * * @param array $additionalClaims */ public function setAdditionalClaims(array $additionalClaims) { $this->additionalClaims = $additionalClaims; } /** * Gets the additional claims to be included in the JWT token. * * @return array */ public function getAdditionalClaims() { return $this->additionalClaims; } /** * The expiration of the last received token. * * @return array */ public function getLastReceivedToken() { if ($token = $this->getAccessToken()) { return [ 'access_token' => $token, 'expires_at' => $this->getExpiresAt(), ]; } return null; } /** * Get the client ID. * * Alias of {@see Google\Auth\OAuth2::getClientId()}. * * @param callable $httpHandler * @return string * @access private */ public function getClientName(callable $httpHandler = null) { return $this->getClientId(); } /** * @todo handle uri as array * * @param string $uri * * @return null|UriInterface */ private function coerceUri($uri) { if (is_null($uri)) { return; } return Psr7\uri_for($uri); } /** * @param string $idToken * @param string|array|null $publicKey * @param array $allowedAlgs * * @return object */ private function jwtDecode($idToken, $publicKey, $allowedAlgs) { if (class_exists('Firebase\JWT\JWT')) { return \Firebase\JWT\JWT::decode($idToken, $publicKey, $allowedAlgs); } return \JWT::decode($idToken, $publicKey, $allowedAlgs); } private function jwtEncode($assertion, $signingKey, $signingAlgorithm) { if (class_exists('Firebase\JWT\JWT')) { return \Firebase\JWT\JWT::encode($assertion, $signingKey, $signingAlgorithm); } return \JWT::encode($assertion, $signingKey, $signingAlgorithm); } /** * Determines if the URI is absolute based on its scheme and host or path * (RFC 3986). * * @param string $uri * * @return bool */ private function isAbsoluteUri($uri) { $uri = $this->coerceUri($uri); return $uri->getScheme() && ($uri->getHost() || $uri->getPath()); } /** * @param array $params * * @return array */ private function addClientCredentials(&$params) { $clientId = $this->getClientId(); $clientSecret = $this->getClientSecret(); if ($clientId && $clientSecret) { $params['client_id'] = $clientId; $params['client_secret'] = $clientSecret; } return $params; } } <?php /* * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Auth; use phpseclib\Crypt\RSA; /** * Sign a string using a Service Account private key. */ trait ServiceAccountSignerTrait { /** * Sign a string using the service account private key. * * @param string $stringToSign * @param bool $forceOpenssl Whether to use OpenSSL regardless of * whether phpseclib is installed. **Defaults to** `false`. * @return string */ public function signBlob($stringToSign, $forceOpenssl = false) { $privateKey = $this->auth->getSigningKey(); $signedString = ''; if (class_exists('\\phpseclib\\Crypt\\RSA') && !$forceOpenssl) { $rsa = new RSA; $rsa->loadKey($privateKey); $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); $rsa->setHash('sha256'); $signedString = $rsa->sign($stringToSign); } elseif (extension_loaded('openssl')) { openssl_sign($stringToSign, $signedString, $privateKey, 'sha256WithRSAEncryption'); } else { // @codeCoverageIgnoreStart throw new \RuntimeException('OpenSSL is not installed.'); } // @codeCoverageIgnoreEnd return base64_encode($signedString); } } ## 1.6.1 (10/29/2019) * [fix] Handle DST correctly for cache item expirations. (#246) ## 1.6.0 (10/01/2019) * [feat] Add utility for verifying and revoking access tokens. (#243) * [docs] Fix README console terminology. (#242) * [feat] Support custom scopes with GCECredentials. (#239) * [fix] Fix phpseclib existence check. (#237) ## 1.5.2 (07/22/2019) * [fix] Move loadItems call out of `SysVCacheItemPool` constructor. (#229) * [fix] Add `Metadata-Flavor` header to initial GCE metadata call. (#232) ## 1.5.1 (04/16/2019) * [fix] Moved `getClientName()` from `Google\Auth\FetchAuthTokenInterface` to `Google\Auth\SignBlobInterface`, and removed `getClientName()` from `InsecureCredentials` and `UserRefreshCredentials`. (#223) ## 1.5.0 (04/15/2019) ### Changes * Add support for signing strings with a Credentials instance. (#221) * [Docs] Describe the arrays returned by fetchAuthToken. (#216) * [Testing] Fix failing tests (#217) * Update GitHub issue templates (#214, #213) ## 1.4.0 (09/17/2018) ### Changes * Add support for insecure credentials (#208) ## 1.3.3 (08/27/2018) ### Changes * Add retry and increase timeout for GCE credentials (#195) * [Docs] Fix spelling (#204) * Update token url (#206) ## 1.3.2 (07/23/2018) ### Changes * Only emits a warning for gcloud credentials (#202) ## 1.3.1 (07/19/2018) ### Changes * Added a warning for 3 legged OAuth credentials (#199) * [Code cleanup] Removed useless else after return (#193) ## 1.3.0 (06/04/2018) ### Changes * Fixes usage of deprecated env var for GAE Flex (#189) * fix - guzzlehttp/psr7 dependency version definition (#190) * Added SystemV shared memory based CacheItemPool (#191) ## 1.2.1 (24/01/2018) ### Changes * Fixes array merging bug in Guzzle5HttpHandler (#186) * Fixes constructor argument bug in Subscriber & Middleware (#184) ## 1.2.0 (6/12/2017) ### Changes * Adds async method to HTTP handlers (#176) * Misc bug fixes and improvements (#177, #175, #178) ## 1.1.0 (10/10/2017) ### Changes * Supports additional claims in JWT tokens (#171) * Adds makeHttpClient for creating authorized Guzzle clients (#162) * Misc bug fixes/improvements (#168, #161, #167, #170, #143) ## 1.0.1 (31/07/2017) ### Changes * Adds support for Firebase 5.0 (#159) ## 1.0.0 (12/06/2017) ### Changes * Adds hashing and shortening to enforce max key length ([@bshaffer]) * Fix for better PSR-6 compliance - verifies a hit before getting the cache item ([@bshaffer]) * README fixes ([@bshaffer]) * Change authorization header key to lowercase ([@stanley-cheung]) ## 0.4.0 (23/04/2015) ### Changes * Export callback function to update auth metadata ([@stanley-cheung][]) * Adds an implementation of User Refresh Token auth ([@stanley-cheung][]) [@bshaffer]: https://github.com/bshaffer [@stanley-cheung]: https://github.com/stanley-cheung { "name": "google/auth", "type": "library", "description": "Google Auth Library for PHP", "keywords": ["google", "oauth2", "authentication"], "homepage": "http://github.com/google/google-auth-library-php", "license": "Apache-2.0", "require": { "php": ">=5.4", "firebase/php-jwt": "~2.0|~3.0|~4.0|~5.0", "guzzlehttp/guzzle": "~5.3.1|~6.0", "guzzlehttp/psr7": "^1.2", "psr/http-message": "^1.0", "psr/cache": "^1.0" }, "require-dev": { "guzzlehttp/promises": "0.1.1|^1.3", "friendsofphp/php-cs-fixer": "^1.11", "phpunit/phpunit": "^4.8.36|^5.7", "sebastian/comparator": ">=1.2.3", "phpseclib/phpseclib": "^2" }, "suggest": { "phpseclib/phpseclib": "May be used in place of OpenSSL for signing strings or for token management. Please require version ^2." }, "autoload": { "psr-4": { "Google\\Auth\\": "src" } } } # Contributor Code of Conduct As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery * Personal attacks * Trolling or insulting/derogatory comments * Public or private harassment * Publishing other's private information, such as physical or electronic addresses, without explicit permission * Other unethical or unprofessional conduct. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ function oauth2client_php_autoload($className) { $classPath = explode('_', $className); if ($classPath[0] != 'Google') { return; } if (count($classPath) > 3) { // Maximum class file path depth in this project is 3. $classPath = array_slice($classPath, 0, 3); } $filePath = dirname(__FILE__) . '/src/' . implode('/', $classPath) . '.php'; if (file_exists($filePath)) { require_once $filePath; } } spl_autoload_register('oauth2client_php_autoload'); # Google Auth Library for PHP <dl> <dt>Homepage</dt><dd><a href="http://www.github.com/google/google-auth-library-php">http://www.github.com/google/google-auth-library-php</a></dd> <dt>Authors</dt> <dd><a href="mailto:temiola@google.com">Tim Emiola</a></dd> <dd><a href="mailto:stanleycheung@google.com">Stanley Cheung</a></dd> <dd><a href="mailto:betterbrent@google.com">Brent Shaffer</a></dd> <dt>Copyright</dt><dd>Copyright © 2015 Google, Inc.</dd> <dt>License</dt><dd>Apache 2.0</dd> </dl> ## Description This is Google's officially supported PHP client library for using OAuth 2.0 authorization and authentication with Google APIs. ### Installing via Composer The recommended way to install the google auth library is through [Composer](http://getcomposer.org). ```bash # Install Composer curl -sS https://getcomposer.org/installer | php ``` Next, run the Composer command to install the latest stable version: ```bash composer.phar require google/auth ``` ## Application Default Credentials This library provides an implementation of [application default credentials][application default credentials] for PHP. The Application Default Credentials provide a simple way to get authorization credentials for use in calling Google APIs. They are best suited for cases when the call needs to have the same identity and authorization level for the application independent of the user. This is the recommended approach to authorize calls to Cloud APIs, particularly when you're building an application that uses Google Compute Engine. #### Download your Service Account Credentials JSON file To use `Application Default Credentials`, You first need to download a set of JSON credentials for your project. Go to **APIs & Services** > **Credentials** in the [Google Developers Console][developer console] and select **Service account** from the **Add credentials** dropdown. > This file is your *only copy* of these credentials. It should never be > committed with your source code, and should be stored securely. Once downloaded, store the path to this file in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable. ```php putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json'); ``` > PHP's `putenv` function is just one way to set an environment variable. > Consider using `.htaccess` or apache configuration files as well. #### Enable the API you want to use Before making your API call, you must be sure the API you're calling has been enabled. Go to **APIs & Auth** > **APIs** in the [Google Developers Console][developer console] and enable the APIs you'd like to call. For the example below, you must enable the `Drive API`. #### Call the APIs As long as you update the environment variable below to point to *your* JSON credentials file, the following code should output a list of your Drive files. ```php use Google\Auth\ApplicationDefaultCredentials; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; // specify the path to your application credentials putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json'); // define the scopes for your API call $scopes = ['https://www.googleapis.com/auth/drive.readonly']; // create middleware $middleware = ApplicationDefaultCredentials::getMiddleware($scopes); $stack = HandlerStack::create(); $stack->push($middleware); // create the HTTP client $client = new Client([ 'handler' => $stack, 'base_uri' => 'https://www.googleapis.com', 'auth' => 'google_auth' // authorize all requests ]); // make the request $response = $client->get('drive/v2/files'); // show the result! print_r((string) $response->getBody()); ``` ##### Guzzle 5 Compatibility If you are using [Guzzle 5][Guzzle 5], replace the `create middleware` and `create the HTTP Client` steps with the following: ```php // create the HTTP client $client = new Client([ 'base_url' => 'https://www.googleapis.com', 'auth' => 'google_auth' // authorize all requests ]); // create subscriber $subscriber = ApplicationDefaultCredentials::getSubscriber($scopes); $client->getEmitter()->attach($subscriber); ``` ## License This library is licensed under Apache 2.0. Full license text is available in [COPYING][copying]. ## Contributing See [CONTRIBUTING][contributing]. ## Support Please [report bugs at the project on Github](https://github.com/google/google-auth-library-php/issues). Don't hesitate to [ask questions](http://stackoverflow.com/questions/tagged/google-auth-library-php) about the client or APIs on [StackOverflow](http://stackoverflow.com). [google-apis-php-client]: https://github.com/google/google-api-php-client [application default credentials]: https://developers.google.com/accounts/docs/application-default-credentials [contributing]: https://github.com/google/google-auth-library-php/tree/master/.github/CONTRIBUTING.md [copying]: https://github.com/google/google-auth-library-php/tree/master/COPYING [Guzzle]: https://github.com/guzzle/guzzle [Guzzle 5]: http://docs.guzzlephp.org/en/5.3 [developer console]: https://console.developers.google.com Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2015 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. <?php /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ class Google_Service_ServiceTest extends PHPUnit_Framework_TestCase { public function setUp() { // ensure dependent classes exist $this->getMock('Google_Service'); $this->getMock('Google_Model'); $this->getMock('Google_Collection'); $this->getMock('Google_Service_Resource'); } /** * @dataProvider serviceProvider */ public function testIncludes($class) { $this->assertTrue( class_exists($class), sprintf('Failed asserting class %s exists.', $class) ); } public function testCaseConflicts() { $apis = $this->apiProvider(); $classes = array_unique(array_map('strtolower', $apis)); $this->assertCount(count($apis), $classes); } public function serviceProvider() { $classes = array(); $path = __DIR__ . '/../src/Google/Service/'; foreach (glob($path . "*.php") as $file) { $service = basename($file, '.php'); $classes[] = array('Google_Service_' . $service); foreach (glob($path . "{$service}/*.php") as $file) { $classes[] = array("Google_Service_{$service}_" . basename($file, '.php')); } foreach (glob($path . "{$service}/Resource/*.php") as $file) { $classes[] = array("Google_Service_{$service}_Resource_" . basename($file, '.php')); } } return $classes; } public function apiProvider() { $path = __DIR__ . '/../src/Google/Service/*'; return array_filter(glob($path), 'is_dir'); } } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ class Google_Service_Oauth2_Tokeninfo extends Google_Model { protected $internal_gapi_mappings = array( "accessType" => "access_type", "expiresIn" => "expires_in", "issuedTo" => "issued_to", "tokenHandle" => "token_handle", "userId" => "user_id", "verifiedEmail" => "verified_email", ); public $accessType; public $audience; public $email; public $expiresIn; public $issuedTo; public $scope; public $tokenHandle; public $userId; public $verifiedEmail; public function setAccessType($accessType) { $this->accessType = $accessType; } public function getAccessType() { return $this->accessType; } public function setAudience($audience) { $this->audience = $audience; } public function getAudience() { return $this->audience; } public function setEmail($email) { $this->email = $email; } public function getEmail() { return $this->email; } public function setExpiresIn($expiresIn) { $this->expiresIn = $expiresIn; } public function getExpiresIn() { return $this->expiresIn; } public function setIssuedTo($issuedTo) { $this->issuedTo = $issuedTo; } public function getIssuedTo() { return $this->issuedTo; } public function setScope($scope) { $this->scope = $scope; } public function getScope() { return $this->scope; } public function setTokenHandle($tokenHandle) { $this->tokenHandle = $tokenHandle; } public function getTokenHandle() { return $this->tokenHandle; } public function setUserId($userId) { $this->userId = $userId; } public function getUserId() { return $this->userId; } public function setVerifiedEmail($verifiedEmail) { $this->verifiedEmail = $verifiedEmail; } public function getVerifiedEmail() { return $this->verifiedEmail; } } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ class Google_Service_Oauth2_JwkKeys extends Google_Model { public $alg; public $e; public $kid; public $kty; public $n; public $use; public function setAlg($alg) { $this->alg = $alg; } public function getAlg() { return $this->alg; } public function setE($e) { $this->e = $e; } public function getE() { return $this->e; } public function setKid($kid) { $this->kid = $kid; } public function getKid() { return $this->kid; } public function setKty($kty) { $this->kty = $kty; } public function getKty() { return $this->kty; } public function setN($n) { $this->n = $n; } public function getN() { return $this->n; } public function setUse($use) { $this->use = $use; } public function getUse() { return $this->use; } } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ class Google_Service_Oauth2_Userinfoplus extends Google_Model { protected $internal_gapi_mappings = array( "familyName" => "family_name", "givenName" => "given_name", "verifiedEmail" => "verified_email", ); public $email; public $familyName; public $gender; public $givenName; public $hd; public $id; public $link; public $locale; public $name; public $picture; public $verifiedEmail; public function setEmail($email) { $this->email = $email; } public function getEmail() { return $this->email; } public function setFamilyName($familyName) { $this->familyName = $familyName; } public function getFamilyName() { return $this->familyName; } public function setGender($gender) { $this->gender = $gender; } public function getGender() { return $this->gender; } public function setGivenName($givenName) { $this->givenName = $givenName; } public function getGivenName() { return $this->givenName; } public function setHd($hd) { $this->hd = $hd; } public function getHd() { return $this->hd; } public function setId($id) { $this->id = $id; } public function getId() { return $this->id; } public function setLink($link) { $this->link = $link; } public function getLink() { return $this->link; } public function setLocale($locale) { $this->locale = $locale; } public function getLocale() { return $this->locale; } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function setPicture($picture) { $this->picture = $picture; } public function getPicture() { return $this->picture; } public function setVerifiedEmail($verifiedEmail) { $this->verifiedEmail = $verifiedEmail; } public function getVerifiedEmail() { return $this->verifiedEmail; } } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ class Google_Service_Oauth2_Jwk extends Google_Collection { protected $collection_key = 'keys'; protected $keysType = 'Google_Service_Oauth2_JwkKeys'; protected $keysDataType = 'array'; /** * @param Google_Service_Oauth2_JwkKeys */ public function setKeys($keys) { $this->keys = $keys; } /** * @return Google_Service_Oauth2_JwkKeys */ public function getKeys() { return $this->keys; } } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ /** * The "v2" collection of methods. * Typical usage is: * <code> * $oauth2Service = new Google_Service_Oauth2(...); * $v2 = $oauth2Service->v2; * </code> */ class Google_Service_Oauth2_Resource_UserinfoV2 extends Google_Service_Resource { } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ /** * The "userinfo" collection of methods. * Typical usage is: * <code> * $oauth2Service = new Google_Service_Oauth2(...); * $userinfo = $oauth2Service->userinfo; * </code> */ class Google_Service_Oauth2_Resource_Userinfo extends Google_Service_Resource { /** * (userinfo.get) * * @param array $optParams Optional parameters. * @return Google_Service_Oauth2_Userinfoplus */ public function get($optParams = array()) { $params = array(); $params = array_merge($params, $optParams); return $this->call('get', array($params), "Google_Service_Oauth2_Userinfoplus"); } } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ /** * The "me" collection of methods. * Typical usage is: * <code> * $oauth2Service = new Google_Service_Oauth2(...); * $me = $oauth2Service->me; * </code> */ class Google_Service_Oauth2_Resource_UserinfoV2Me extends Google_Service_Resource { /** * (me.get) * * @param array $optParams Optional parameters. * @return Google_Service_Oauth2_Userinfoplus */ public function get($optParams = array()) { $params = array(); $params = array_merge($params, $optParams); return $this->call('get', array($params), "Google_Service_Oauth2_Userinfoplus"); } } <?php /* * Copyright 2014 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ /** * Service definition for Oauth2 (v2). * * <p> * Obtains end-user authorization grants for use with other Google APIs.</p> * * <p> * For more information about this service, see the API * <a href="https://developers.google.com/accounts/docs/OAuth2" target="_blank">Documentation</a> * </p> * * @author Google, Inc. */ class Google_Service_Oauth2 extends Google_Service { /** Associate you with your personal info on Google. */ const PLUS_ME = "https://www.googleapis.com/auth/plus.me"; /** View your email address. */ const USERINFO_EMAIL = "https://www.googleapis.com/auth/userinfo.email"; /** See your personal info, including any personal info you've made publicly available. */ const USERINFO_PROFILE = "https://www.googleapis.com/auth/userinfo.profile"; public $userinfo; public $userinfo_v2_me; private $base_methods; /** * Constructs the internal representation of the Oauth2 service. * * @param Google_Client $client The client used to deliver requests. * @param string $rootUrl The root URL used for requests to the service. */ public function __construct(Google_Client $client, $rootUrl = null) { parent::__construct($client); $this->rootUrl = $rootUrl ?: 'https://www.googleapis.com/'; $this->servicePath = ''; $this->batchPath = 'batch/oauth2/v2'; $this->version = 'v2'; $this->serviceName = 'oauth2'; $this->userinfo = new Google_Service_Oauth2_Resource_Userinfo( $this, $this->serviceName, 'userinfo', array( 'methods' => array( 'get' => array( 'path' => 'oauth2/v2/userinfo', 'httpMethod' => 'GET', 'parameters' => array(), ), ) ) ); $this->userinfo_v2_me = new Google_Service_Oauth2_Resource_UserinfoV2Me( $this, $this->serviceName, 'me', array( 'methods' => array( 'get' => array( 'path' => 'userinfo/v2/me', 'httpMethod' => 'GET', 'parameters' => array(), ), ) ) ); $this->base_methods = new Google_Service_Resource( $this, $this->serviceName, '', array( 'methods' => array( 'getCertForOpenIdConnect' => array( 'path' => 'oauth2/v2/certs', 'httpMethod' => 'GET', 'parameters' => array(), ),'tokeninfo' => array( 'path' => 'oauth2/v2/tokeninfo', 'httpMethod' => 'POST', 'parameters' => array( 'access_token' => array( 'location' => 'query', 'type' => 'string', ), 'id_token' => array( 'location' => 'query', 'type' => 'string', ), 'token_handle' => array( 'location' => 'query', 'type' => 'string', ), ), ), ) ) ); } /** * (getCertForOpenIdConnect) * * @param array $optParams Optional parameters. * @return Google_Service_Oauth2_Jwk */ public function getCertForOpenIdConnect($optParams = array()) { $params = array(); $params = array_merge($params, $optParams); return $this->base_methods->call('getCertForOpenIdConnect', array($params), "Google_Service_Oauth2_Jwk"); } /** * (tokeninfo) * * @param array $optParams Optional parameters. * * @opt_param string access_token * @opt_param string id_token * @opt_param string token_handle * @return Google_Service_Oauth2_Tokeninfo */ public function tokeninfo($optParams = array()) { $params = array(); $params = array_merge($params, $optParams); return $this->base_methods->call('tokeninfo', array($params), "Google_Service_Oauth2_Tokeninfo"); } } { "name": "google/apiclient-services", "type": "library", "description": "Client library for Google APIs", "keywords": ["google"], "homepage": "http://developers.google.com/api-client-library/php", "license": "Apache-2.0", "require": { "php": ">=5.4" }, "require-dev": { "phpunit/phpunit": "~4.8" }, "autoload": { "psr-0": { "Google_Service_": "src" } } } <?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd" colors="true" bootstrap="vendor/autoload.php"> <testsuites> <testsuite name="Google PHP Client Unit Services Test Suite"> <directory>tests</directory> </testsuite> </testsuites> </phpunit> # Contributor Code of Conduct As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery * Personal attacks * Trolling or insulting/derogatory comments * Public or private harassment * Publishing other's private information, such as physical or electronic addresses, without explicit permission * Other unethical or unprofessional conduct. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) Google PHP API Client Services ============================== ## Requirements [Google API PHP Client](https://github.com/googleapis/google-api-php-client/releases) ## Usage in v2 of Google API PHP Client This library is automatically updated daily with new API changes, and tagged weekly. It is installed as part of the [Google API PHP Client](https://github.com/googleapis/google-api-php-client/releases) library via Composer, which will pull down the most recent tag. ## Usage in v1 If you are currently using the [`v1-master`](https://github.com/googleapis/google-api-php-client/tree/v1-master) branch of the client library, but want to use the latest API services, you can do so by requiring this library directly into your project via the same composer command: ```sh composer require google/apiclient-services:dev-master ``` <?php // autoload.php @generated by Composer require_once __DIR__ . '/composer/autoload_real.php'; return ComposerAutoloaderInitc53c06361dfa4f223f81ea4ac493255a::getLoader(); <?php if (!function_exists('getallheaders')) { /** * Get all HTTP header key/values as an associative array for the current request. * * @return string[string] The HTTP header key/value pairs. */ function getallheaders() { $headers = array(); $copy_server = array( 'CONTENT_TYPE' => 'Content-Type', 'CONTENT_LENGTH' => 'Content-Length', 'CONTENT_MD5' => 'Content-Md5', ); foreach ($_SERVER as $key => $value) { if (substr($key, 0, 5) === 'HTTP_') { $key = substr($key, 5); if (!isset($copy_server[$key]) || !isset($_SERVER[$key])) { $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $key)))); $headers[$key] = $value; } } elseif (isset($copy_server[$key])) { $headers[$copy_server[$key]] = $value; } } if (!isset($headers['Authorization'])) { if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) { $headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; } elseif (isset($_SERVER['PHP_AUTH_USER'])) { $basic_pass = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : ''; $headers['Authorization'] = 'Basic ' . base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $basic_pass); } elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) { $headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST']; } } return $headers; } } { "name": "ralouphie/getallheaders", "description": "A polyfill for getallheaders.", "license": "MIT", "authors": [ { "name": "Ralph Khattar", "email": "ralph.khattar@gmail.com" } ], "require": { "php": ">=5.6" }, "require-dev": { "phpunit/phpunit": "^5 || ^6.5", "php-coveralls/php-coveralls": "^2.1" }, "autoload": { "files": ["src/getallheaders.php"] }, "autoload-dev": { "psr-4": { "getallheaders\\Tests\\": "tests/" } } } getallheaders ============= PHP `getallheaders()` polyfill. Compatible with PHP >= 5.3. [](https://travis-ci.org/ralouphie/getallheaders) [](https://coveralls.io/r/ralouphie/getallheaders?branch=master) [](https://packagist.org/packages/ralouphie/getallheaders) [](https://packagist.org/packages/ralouphie/getallheaders) [](https://packagist.org/packages/ralouphie/getallheaders) This is a simple polyfill for [`getallheaders()`](http://www.php.net/manual/en/function.getallheaders.php). ## Install For PHP version **`>= 5.6`**: ``` composer require ralouphie/getallheaders ``` For PHP version **`< 5.6`**: ``` composer require ralouphie/getallheaders "^2" ``` <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog; /** * Handler or Processor implementing this interface will be reset when Logger::reset() is called. * * Resetting ends a log cycle gets them back to their initial state. * * Resetting a Handler or a Processor means flushing/cleaning all buffers, resetting internal * state, and getting it back to a state in which it can receive log records again. * * This is useful in case you want to avoid logs leaking between two requests or jobs when you * have a long running process like a worker or an application server serving multiple requests * in one process. * * @author Grégoire Pineau <lyrixx@lyrixx.info> */ interface ResettableInterface { public function reset(); } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; use Monolog\ResettableInterface; /** * Adds a unique identifier into records * * @author Simon Mönch <sm@webfactory.de> */ class UidProcessor implements ProcessorInterface, ResettableInterface { private $uid; public function __construct($length = 7) { if (!is_int($length) || $length > 32 || $length < 1) { throw new \InvalidArgumentException('The uid length must be an integer between 1 and 32'); } $this->uid = $this->generateUid($length); } public function __invoke(array $record) { $record['extra']['uid'] = $this->uid; return $record; } /** * @return string */ public function getUid() { return $this->uid; } public function reset() { $this->uid = $this->generateUid(strlen($this->uid)); } private function generateUid($length) { return substr(hash('md5', uniqid('', true)), 0, $length); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; /** * An optional interface to allow labelling Monolog processors. * * @author Nicolas Grekas <p@tchwork.com> */ interface ProcessorInterface { /** * @return array The processed records */ public function __invoke(array $records); } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; /** * Injects memory_get_peak_usage in all records * * @see Monolog\Processor\MemoryProcessor::__construct() for options * @author Rob Jensen */ class MemoryPeakUsageProcessor extends MemoryProcessor { /** * @param array $record * @return array */ public function __invoke(array $record) { $bytes = memory_get_peak_usage($this->realUsage); $formatted = $this->formatBytes($bytes); $record['extra']['memory_peak_usage'] = $formatted; return $record; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; use Monolog\Logger; /** * Injects Git branch and Git commit SHA in all records * * @author Nick Otter * @author Jordi Boggiano <j.boggiano@seld.be> */ class GitProcessor implements ProcessorInterface { private $level; private static $cache; public function __construct($level = Logger::DEBUG) { $this->level = Logger::toMonologLevel($level); } /** * @param array $record * @return array */ public function __invoke(array $record) { // return if the level is not high enough if ($record['level'] < $this->level) { return $record; } $record['extra']['git'] = self::getGitInfo(); return $record; } private static function getGitInfo() { if (self::$cache) { return self::$cache; } $branches = `git branch -v --no-abbrev`; if (preg_match('{^\* (.+?)\s+([a-f0-9]{40})(?:\s|$)}m', $branches, $matches)) { return self::$cache = array( 'branch' => $matches[1], 'commit' => $matches[2], ); } return self::$cache = array(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; /** * Some methods that are common for all memory processors * * @author Rob Jensen */ abstract class MemoryProcessor implements ProcessorInterface { /** * @var bool If true, get the real size of memory allocated from system. Else, only the memory used by emalloc() is reported. */ protected $realUsage; /** * @var bool If true, then format memory size to human readable string (MB, KB, B depending on size) */ protected $useFormatting; /** * @param bool $realUsage Set this to true to get the real size of memory allocated from system. * @param bool $useFormatting If true, then format memory size to human readable string (MB, KB, B depending on size) */ public function __construct($realUsage = true, $useFormatting = true) { $this->realUsage = (bool) $realUsage; $this->useFormatting = (bool) $useFormatting; } /** * Formats bytes into a human readable string if $this->useFormatting is true, otherwise return $bytes as is * * @param int $bytes * @return string|int Formatted string if $this->useFormatting is true, otherwise return $bytes as is */ protected function formatBytes($bytes) { $bytes = (int) $bytes; if (!$this->useFormatting) { return $bytes; } if ($bytes > 1024 * 1024) { return round($bytes / 1024 / 1024, 2).' MB'; } elseif ($bytes > 1024) { return round($bytes / 1024, 2).' KB'; } return $bytes . ' B'; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; use Monolog\Logger; /** * Injects line/file:class/function where the log message came from * * Warning: This only works if the handler processes the logs directly. * If you put the processor on a handler that is behind a FingersCrossedHandler * for example, the processor will only be called once the trigger level is reached, * and all the log records will have the same file/line/.. data from the call that * triggered the FingersCrossedHandler. * * @author Jordi Boggiano <j.boggiano@seld.be> */ class IntrospectionProcessor implements ProcessorInterface { private $level; private $skipClassesPartials; private $skipStackFramesCount; private $skipFunctions = array( 'call_user_func', 'call_user_func_array', ); public function __construct($level = Logger::DEBUG, array $skipClassesPartials = array(), $skipStackFramesCount = 0) { $this->level = Logger::toMonologLevel($level); $this->skipClassesPartials = array_merge(array('Monolog\\'), $skipClassesPartials); $this->skipStackFramesCount = $skipStackFramesCount; } /** * @param array $record * @return array */ public function __invoke(array $record) { // return if the level is not high enough if ($record['level'] < $this->level) { return $record; } /* * http://php.net/manual/en/function.debug-backtrace.php * As of 5.3.6, DEBUG_BACKTRACE_IGNORE_ARGS option was added. * Any version less than 5.3.6 must use the DEBUG_BACKTRACE_IGNORE_ARGS constant value '2'. */ $trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS); // skip first since it's always the current method array_shift($trace); // the call_user_func call is also skipped array_shift($trace); $i = 0; while ($this->isTraceClassOrSkippedFunction($trace, $i)) { if (isset($trace[$i]['class'])) { foreach ($this->skipClassesPartials as $part) { if (strpos($trace[$i]['class'], $part) !== false) { $i++; continue 2; } } } elseif (in_array($trace[$i]['function'], $this->skipFunctions)) { $i++; continue; } break; } $i += $this->skipStackFramesCount; // we should have the call source now $record['extra'] = array_merge( $record['extra'], array( 'file' => isset($trace[$i - 1]['file']) ? $trace[$i - 1]['file'] : null, 'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null, 'class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : null, 'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null, ) ); return $record; } private function isTraceClassOrSkippedFunction(array $trace, $index) { if (!isset($trace[$index])) { return false; } return isset($trace[$index]['class']) || in_array($trace[$index]['function'], $this->skipFunctions); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; /** * Injects url/method and remote IP of the current web request in all records * * @author Jordi Boggiano <j.boggiano@seld.be> */ class WebProcessor implements ProcessorInterface { /** * @var array|\ArrayAccess */ protected $serverData; /** * Default fields * * Array is structured as [key in record.extra => key in $serverData] * * @var array */ protected $extraFields = array( 'url' => 'REQUEST_URI', 'ip' => 'REMOTE_ADDR', 'http_method' => 'REQUEST_METHOD', 'server' => 'SERVER_NAME', 'referrer' => 'HTTP_REFERER', ); /** * @param array|\ArrayAccess $serverData Array or object w/ ArrayAccess that provides access to the $_SERVER data * @param array|null $extraFields Field names and the related key inside $serverData to be added. If not provided it defaults to: url, ip, http_method, server, referrer */ public function __construct($serverData = null, array $extraFields = null) { if (null === $serverData) { $this->serverData = &$_SERVER; } elseif (is_array($serverData) || $serverData instanceof \ArrayAccess) { $this->serverData = $serverData; } else { throw new \UnexpectedValueException('$serverData must be an array or object implementing ArrayAccess.'); } if (null !== $extraFields) { if (isset($extraFields[0])) { foreach (array_keys($this->extraFields) as $fieldName) { if (!in_array($fieldName, $extraFields)) { unset($this->extraFields[$fieldName]); } } } else { $this->extraFields = $extraFields; } } } /** * @param array $record * @return array */ public function __invoke(array $record) { // skip processing if for some reason request data // is not present (CLI or wonky SAPIs) if (!isset($this->serverData['REQUEST_URI'])) { return $record; } $record['extra'] = $this->appendExtraFields($record['extra']); return $record; } /** * @param string $extraName * @param string $serverName * @return $this */ public function addExtraField($extraName, $serverName) { $this->extraFields[$extraName] = $serverName; return $this; } /** * @param array $extra * @return array */ private function appendExtraFields(array $extra) { foreach ($this->extraFields as $extraName => $serverName) { $extra[$extraName] = isset($this->serverData[$serverName]) ? $this->serverData[$serverName] : null; } if (isset($this->serverData['UNIQUE_ID'])) { $extra['unique_id'] = $this->serverData['UNIQUE_ID']; } return $extra; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; /** * Injects memory_get_usage in all records * * @see Monolog\Processor\MemoryProcessor::__construct() for options * @author Rob Jensen */ class MemoryUsageProcessor extends MemoryProcessor { /** * @param array $record * @return array */ public function __invoke(array $record) { $bytes = memory_get_usage($this->realUsage); $formatted = $this->formatBytes($bytes); $record['extra']['memory_usage'] = $formatted; return $record; } } <?php /* * This file is part of the Monolog package. * * (c) Jonathan A. Schweder <jonathanschweder@gmail.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; use Monolog\Logger; /** * Injects Hg branch and Hg revision number in all records * * @author Jonathan A. Schweder <jonathanschweder@gmail.com> */ class MercurialProcessor implements ProcessorInterface { private $level; private static $cache; public function __construct($level = Logger::DEBUG) { $this->level = Logger::toMonologLevel($level); } /** * @param array $record * @return array */ public function __invoke(array $record) { // return if the level is not high enough if ($record['level'] < $this->level) { return $record; } $record['extra']['hg'] = self::getMercurialInfo(); return $record; } private static function getMercurialInfo() { if (self::$cache) { return self::$cache; } $result = explode(' ', trim(`hg id -nb`)); if (count($result) >= 3) { return self::$cache = array( 'branch' => $result[1], 'revision' => $result[2], ); } return self::$cache = array(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; /** * Adds value of getmypid into records * * @author Andreas Hörnicke */ class ProcessIdProcessor implements ProcessorInterface { /** * @param array $record * @return array */ public function __invoke(array $record) { $record['extra']['process_id'] = getmypid(); return $record; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; use Monolog\Utils; /** * Processes a record's message according to PSR-3 rules * * It replaces {foo} with the value from $context['foo'] * * @author Jordi Boggiano <j.boggiano@seld.be> */ class PsrLogMessageProcessor implements ProcessorInterface { /** * @param array $record * @return array */ public function __invoke(array $record) { if (false === strpos($record['message'], '{')) { return $record; } $replacements = array(); foreach ($record['context'] as $key => $val) { if (is_null($val) || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) { $replacements['{'.$key.'}'] = $val; } elseif (is_object($val)) { $replacements['{'.$key.'}'] = '[object '.Utils::getClass($val).']'; } else { $replacements['{'.$key.'}'] = '['.gettype($val).']'; } } $record['message'] = strtr($record['message'], $replacements); return $record; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Processor; /** * Adds a tags array into record * * @author Martijn Riemers */ class TagProcessor implements ProcessorInterface { private $tags; public function __construct(array $tags = array()) { $this->setTags($tags); } public function addTags(array $tags = array()) { $this->tags = array_merge($this->tags, $tags); } public function setTags(array $tags = array()) { $this->tags = $tags; } public function __invoke(array $record) { $record['extra']['tags'] = $this->tags; return $record; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog; class Utils { /** * @internal */ public static function getClass($object) { $class = \get_class($object); return 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class; } /** * Return the JSON representation of a value * * @param mixed $data * @param int $encodeFlags flags to pass to json encode, defaults to JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE * @param bool $ignoreErrors whether to ignore encoding errors or to throw on error, when ignored and the encoding fails, "null" is returned which is valid json for null * @throws \RuntimeException if encoding fails and errors are not ignored * @return string */ public static function jsonEncode($data, $encodeFlags = null, $ignoreErrors = false) { if (null === $encodeFlags && version_compare(PHP_VERSION, '5.4.0', '>=')) { $encodeFlags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; } if ($ignoreErrors) { $json = @json_encode($data, $encodeFlags); if (false === $json) { return 'null'; } return $json; } $json = json_encode($data, $encodeFlags); if (false === $json) { $json = self::handleJsonError(json_last_error(), $data); } return $json; } /** * Handle a json_encode failure. * * If the failure is due to invalid string encoding, try to clean the * input and encode again. If the second encoding attempt fails, the * inital error is not encoding related or the input can't be cleaned then * raise a descriptive exception. * * @param int $code return code of json_last_error function * @param mixed $data data that was meant to be encoded * @param int $encodeFlags flags to pass to json encode, defaults to JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE * @throws \RuntimeException if failure can't be corrected * @return string JSON encoded data after error correction */ public static function handleJsonError($code, $data, $encodeFlags = null) { if ($code !== JSON_ERROR_UTF8) { self::throwEncodeError($code, $data); } if (is_string($data)) { self::detectAndCleanUtf8($data); } elseif (is_array($data)) { array_walk_recursive($data, array('Monolog\Utils', 'detectAndCleanUtf8')); } else { self::throwEncodeError($code, $data); } if (null === $encodeFlags && version_compare(PHP_VERSION, '5.4.0', '>=')) { $encodeFlags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; } $json = json_encode($data, $encodeFlags); if ($json === false) { self::throwEncodeError(json_last_error(), $data); } return $json; } /** * Throws an exception according to a given code with a customized message * * @param int $code return code of json_last_error function * @param mixed $data data that was meant to be encoded * @throws \RuntimeException */ private static function throwEncodeError($code, $data) { switch ($code) { case JSON_ERROR_DEPTH: $msg = 'Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $msg = 'Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: $msg = 'Unexpected control character found'; break; case JSON_ERROR_UTF8: $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: $msg = 'Unknown error'; } throw new \RuntimeException('JSON encoding failed: '.$msg.'. Encoding: '.var_export($data, true)); } /** * Detect invalid UTF-8 string characters and convert to valid UTF-8. * * Valid UTF-8 input will be left unmodified, but strings containing * invalid UTF-8 codepoints will be reencoded as UTF-8 with an assumed * original encoding of ISO-8859-15. This conversion may result in * incorrect output if the actual encoding was not ISO-8859-15, but it * will be clean UTF-8 output and will not rely on expensive and fragile * detection algorithms. * * Function converts the input in place in the passed variable so that it * can be used as a callback for array_walk_recursive. * * @param mixed &$data Input to check and convert if needed * @private */ public static function detectAndCleanUtf8(&$data) { if (is_string($data) && !preg_match('//u', $data)) { $data = preg_replace_callback( '/[\x80-\xFF]+/', function ($m) { return utf8_encode($m[0]); }, $data ); $data = str_replace( array('¤', '¦', '¨', '´', '¸', '¼', '½', '¾'), array('€', 'Š', 'š', 'Ž', 'ž', 'Œ', 'œ', 'Ÿ'), $data ); } } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; /** * formats the record to be used in the FlowdockHandler * * @author Dominik Liebler <liebler.dominik@gmail.com> */ class FlowdockFormatter implements FormatterInterface { /** * @var string */ private $source; /** * @var string */ private $sourceEmail; /** * @param string $source * @param string $sourceEmail */ public function __construct($source, $sourceEmail) { $this->source = $source; $this->sourceEmail = $sourceEmail; } /** * {@inheritdoc} */ public function format(array $record) { $tags = array( '#logs', '#' . strtolower($record['level_name']), '#' . $record['channel'], ); foreach ($record['extra'] as $value) { $tags[] = '#' . $value; } $subject = sprintf( 'in %s: %s - %s', $this->source, $record['level_name'], $this->getShortMessage($record['message']) ); $record['flowdock'] = array( 'source' => $this->source, 'from_address' => $this->sourceEmail, 'subject' => $subject, 'content' => $record['message'], 'tags' => $tags, 'project' => $this->source, ); return $record; } /** * {@inheritdoc} */ public function formatBatch(array $records) { $formatted = array(); foreach ($records as $record) { $formatted[] = $this->format($record); } return $formatted; } /** * @param string $message * * @return string */ public function getShortMessage($message) { static $hasMbString; if (null === $hasMbString) { $hasMbString = function_exists('mb_strlen'); } $maxLength = 45; if ($hasMbString) { if (mb_strlen($message, 'UTF-8') > $maxLength) { $message = mb_substr($message, 0, $maxLength - 4, 'UTF-8') . ' ...'; } } else { if (strlen($message) > $maxLength) { $message = substr($message, 0, $maxLength - 4) . ' ...'; } } return $message; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; use Monolog\Logger; use Monolog\Utils; /** * Formats incoming records into an HTML table * * This is especially useful for html email logging * * @author Tiago Brito <tlfbrito@gmail.com> */ class HtmlFormatter extends NormalizerFormatter { /** * Translates Monolog log levels to html color priorities. */ protected $logLevels = array( Logger::DEBUG => '#cccccc', Logger::INFO => '#468847', Logger::NOTICE => '#3a87ad', Logger::WARNING => '#c09853', Logger::ERROR => '#f0ad4e', Logger::CRITICAL => '#FF7708', Logger::ALERT => '#C12A19', Logger::EMERGENCY => '#000000', ); /** * @param string $dateFormat The format of the timestamp: one supported by DateTime::format */ public function __construct($dateFormat = null) { parent::__construct($dateFormat); } /** * Creates an HTML table row * * @param string $th Row header content * @param string $td Row standard cell content * @param bool $escapeTd false if td content must not be html escaped * @return string */ protected function addRow($th, $td = ' ', $escapeTd = true) { $th = htmlspecialchars($th, ENT_NOQUOTES, 'UTF-8'); if ($escapeTd) { $td = '<pre>'.htmlspecialchars($td, ENT_NOQUOTES, 'UTF-8').'</pre>'; } return "<tr style=\"padding: 4px;text-align: left;\">\n<th style=\"vertical-align: top;background: #ccc;color: #000\" width=\"100\">$th:</th>\n<td style=\"padding: 4px;text-align: left;vertical-align: top;background: #eee;color: #000\">".$td."</td>\n</tr>"; } /** * Create a HTML h1 tag * * @param string $title Text to be in the h1 * @param int $level Error level * @return string */ protected function addTitle($title, $level) { $title = htmlspecialchars($title, ENT_NOQUOTES, 'UTF-8'); return '<h1 style="background: '.$this->logLevels[$level].';color: #ffffff;padding: 5px;" class="monolog-output">'.$title.'</h1>'; } /** * Formats a log record. * * @param array $record A record to format * @return mixed The formatted record */ public function format(array $record) { $output = $this->addTitle($record['level_name'], $record['level']); $output .= '<table cellspacing="1" width="100%" class="monolog-output">'; $output .= $this->addRow('Message', (string) $record['message']); $output .= $this->addRow('Time', $record['datetime']->format($this->dateFormat)); $output .= $this->addRow('Channel', $record['channel']); if ($record['context']) { $embeddedTable = '<table cellspacing="1" width="100%">'; foreach ($record['context'] as $key => $value) { $embeddedTable .= $this->addRow($key, $this->convertToString($value)); } $embeddedTable .= '</table>'; $output .= $this->addRow('Context', $embeddedTable, false); } if ($record['extra']) { $embeddedTable = '<table cellspacing="1" width="100%">'; foreach ($record['extra'] as $key => $value) { $embeddedTable .= $this->addRow($key, $this->convertToString($value)); } $embeddedTable .= '</table>'; $output .= $this->addRow('Extra', $embeddedTable, false); } return $output.'</table>'; } /** * Formats a set of log records. * * @param array $records A set of records to format * @return mixed The formatted set of records */ public function formatBatch(array $records) { $message = ''; foreach ($records as $record) { $message .= $this->format($record); } return $message; } protected function convertToString($data) { if (null === $data || is_scalar($data)) { return (string) $data; } $data = $this->normalize($data); if (version_compare(PHP_VERSION, '5.4.0', '>=')) { return Utils::jsonEncode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, true); } return str_replace('\\/', '/', Utils::jsonEncode($data, null, true)); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; use Exception; use Monolog\Utils; /** * Normalizes incoming records to remove objects/resources so it's easier to dump to various targets * * @author Jordi Boggiano <j.boggiano@seld.be> */ class NormalizerFormatter implements FormatterInterface { const SIMPLE_DATE = "Y-m-d H:i:s"; protected $dateFormat; /** * @param string $dateFormat The format of the timestamp: one supported by DateTime::format */ public function __construct($dateFormat = null) { $this->dateFormat = $dateFormat ?: static::SIMPLE_DATE; if (!function_exists('json_encode')) { throw new \RuntimeException('PHP\'s json extension is required to use Monolog\'s NormalizerFormatter'); } } /** * {@inheritdoc} */ public function format(array $record) { return $this->normalize($record); } /** * {@inheritdoc} */ public function formatBatch(array $records) { foreach ($records as $key => $record) { $records[$key] = $this->format($record); } return $records; } protected function normalize($data, $depth = 0) { if ($depth > 9) { return 'Over 9 levels deep, aborting normalization'; } if (null === $data || is_scalar($data)) { if (is_float($data)) { if (is_infinite($data)) { return ($data > 0 ? '' : '-') . 'INF'; } if (is_nan($data)) { return 'NaN'; } } return $data; } if (is_array($data)) { $normalized = array(); $count = 1; foreach ($data as $key => $value) { if ($count++ > 1000) { $normalized['...'] = 'Over 1000 items ('.count($data).' total), aborting normalization'; break; } $normalized[$key] = $this->normalize($value, $depth+1); } return $normalized; } if ($data instanceof \DateTime) { return $data->format($this->dateFormat); } if (is_object($data)) { // TODO 2.0 only check for Throwable if ($data instanceof Exception || (PHP_VERSION_ID > 70000 && $data instanceof \Throwable)) { return $this->normalizeException($data); } // non-serializable objects that implement __toString stringified if (method_exists($data, '__toString') && !$data instanceof \JsonSerializable) { $value = $data->__toString(); } else { // the rest is json-serialized in some way $value = $this->toJson($data, true); } return sprintf("[object] (%s: %s)", Utils::getClass($data), $value); } if (is_resource($data)) { return sprintf('[resource] (%s)', get_resource_type($data)); } return '[unknown('.gettype($data).')]'; } protected function normalizeException($e) { // TODO 2.0 only check for Throwable if (!$e instanceof Exception && !$e instanceof \Throwable) { throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Utils::getClass($e)); } $data = array( 'class' => Utils::getClass($e), 'message' => $e->getMessage(), 'code' => (int) $e->getCode(), 'file' => $e->getFile().':'.$e->getLine(), ); if ($e instanceof \SoapFault) { if (isset($e->faultcode)) { $data['faultcode'] = $e->faultcode; } if (isset($e->faultactor)) { $data['faultactor'] = $e->faultactor; } if (isset($e->detail) && (is_string($e->detail) || is_object($e->detail) || is_array($e->detail))) { $data['detail'] = is_string($e->detail) ? $e->detail : reset($e->detail); } } $trace = $e->getTrace(); foreach ($trace as $frame) { if (isset($frame['file'])) { $data['trace'][] = $frame['file'].':'.$frame['line']; } } if ($previous = $e->getPrevious()) { $data['previous'] = $this->normalizeException($previous); } return $data; } /** * Return the JSON representation of a value * * @param mixed $data * @param bool $ignoreErrors * @throws \RuntimeException if encoding fails and errors are not ignored * @return string */ protected function toJson($data, $ignoreErrors = false) { return Utils::jsonEncode($data, null, $ignoreErrors); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; /** * Formats data into an associative array of scalar values. * Objects and arrays will be JSON encoded. * * @author Andrew Lawson <adlawson@gmail.com> */ class ScalarFormatter extends NormalizerFormatter { /** * {@inheritdoc} */ public function format(array $record) { foreach ($record as $key => $value) { $record[$key] = $this->normalizeValue($value); } return $record; } /** * @param mixed $value * @return mixed */ protected function normalizeValue($value) { $normalized = $this->normalize($value); if (is_array($normalized) || is_object($normalized)) { return $this->toJson($normalized, true); } return $normalized; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; use Monolog\Logger; use Gelf\Message; /** * Serializes a log message to GELF * @see http://www.graylog2.org/about/gelf * * @author Matt Lehner <mlehner@gmail.com> */ class GelfMessageFormatter extends NormalizerFormatter { const DEFAULT_MAX_LENGTH = 32766; /** * @var string the name of the system for the Gelf log message */ protected $systemName; /** * @var string a prefix for 'extra' fields from the Monolog record (optional) */ protected $extraPrefix; /** * @var string a prefix for 'context' fields from the Monolog record (optional) */ protected $contextPrefix; /** * @var int max length per field */ protected $maxLength; /** * Translates Monolog log levels to Graylog2 log priorities. */ private $logLevels = array( Logger::DEBUG => 7, Logger::INFO => 6, Logger::NOTICE => 5, Logger::WARNING => 4, Logger::ERROR => 3, Logger::CRITICAL => 2, Logger::ALERT => 1, Logger::EMERGENCY => 0, ); public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_', $maxLength = null) { parent::__construct('U.u'); $this->systemName = $systemName ?: gethostname(); $this->extraPrefix = $extraPrefix; $this->contextPrefix = $contextPrefix; $this->maxLength = is_null($maxLength) ? self::DEFAULT_MAX_LENGTH : $maxLength; } /** * {@inheritdoc} */ public function format(array $record) { $record = parent::format($record); if (!isset($record['datetime'], $record['message'], $record['level'])) { throw new \InvalidArgumentException('The record should at least contain datetime, message and level keys, '.var_export($record, true).' given'); } $message = new Message(); $message ->setTimestamp($record['datetime']) ->setShortMessage((string) $record['message']) ->setHost($this->systemName) ->setLevel($this->logLevels[$record['level']]); // message length + system name length + 200 for padding / metadata $len = 200 + strlen((string) $record['message']) + strlen($this->systemName); if ($len > $this->maxLength) { $message->setShortMessage(substr($record['message'], 0, $this->maxLength)); } if (isset($record['channel'])) { $message->setFacility($record['channel']); } if (isset($record['extra']['line'])) { $message->setLine($record['extra']['line']); unset($record['extra']['line']); } if (isset($record['extra']['file'])) { $message->setFile($record['extra']['file']); unset($record['extra']['file']); } foreach ($record['extra'] as $key => $val) { $val = is_scalar($val) || null === $val ? $val : $this->toJson($val); $len = strlen($this->extraPrefix . $key . $val); if ($len > $this->maxLength) { $message->setAdditional($this->extraPrefix . $key, substr($val, 0, $this->maxLength)); break; } $message->setAdditional($this->extraPrefix . $key, $val); } foreach ($record['context'] as $key => $val) { $val = is_scalar($val) || null === $val ? $val : $this->toJson($val); $len = strlen($this->contextPrefix . $key . $val); if ($len > $this->maxLength) { $message->setAdditional($this->contextPrefix . $key, substr($val, 0, $this->maxLength)); break; } $message->setAdditional($this->contextPrefix . $key, $val); } if (null === $message->getFile() && isset($record['context']['exception']['file'])) { if (preg_match("/^(.+):([0-9]+)$/", $record['context']['exception']['file'], $matches)) { $message->setFile($matches[1]); $message->setLine($matches[2]); } } return $message; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; use Exception; use Monolog\Utils; use Throwable; /** * Encodes whatever record data is passed to it as json * * This can be useful to log to databases or remote APIs * * @author Jordi Boggiano <j.boggiano@seld.be> */ class JsonFormatter extends NormalizerFormatter { const BATCH_MODE_JSON = 1; const BATCH_MODE_NEWLINES = 2; protected $batchMode; protected $appendNewline; /** * @var bool */ protected $includeStacktraces = false; /** * @param int $batchMode * @param bool $appendNewline */ public function __construct($batchMode = self::BATCH_MODE_JSON, $appendNewline = true) { $this->batchMode = $batchMode; $this->appendNewline = $appendNewline; } /** * The batch mode option configures the formatting style for * multiple records. By default, multiple records will be * formatted as a JSON-encoded array. However, for * compatibility with some API endpoints, alternative styles * are available. * * @return int */ public function getBatchMode() { return $this->batchMode; } /** * True if newlines are appended to every formatted record * * @return bool */ public function isAppendingNewlines() { return $this->appendNewline; } /** * {@inheritdoc} */ public function format(array $record) { return $this->toJson($this->normalize($record), true) . ($this->appendNewline ? "\n" : ''); } /** * {@inheritdoc} */ public function formatBatch(array $records) { switch ($this->batchMode) { case static::BATCH_MODE_NEWLINES: return $this->formatBatchNewlines($records); case static::BATCH_MODE_JSON: default: return $this->formatBatchJson($records); } } /** * @param bool $include */ public function includeStacktraces($include = true) { $this->includeStacktraces = $include; } /** * Return a JSON-encoded array of records. * * @param array $records * @return string */ protected function formatBatchJson(array $records) { return $this->toJson($this->normalize($records), true); } /** * Use new lines to separate records instead of a * JSON-encoded array. * * @param array $records * @return string */ protected function formatBatchNewlines(array $records) { $instance = $this; $oldNewline = $this->appendNewline; $this->appendNewline = false; array_walk($records, function (&$value, $key) use ($instance) { $value = $instance->format($value); }); $this->appendNewline = $oldNewline; return implode("\n", $records); } /** * Normalizes given $data. * * @param mixed $data * * @return mixed */ protected function normalize($data, $depth = 0) { if ($depth > 9) { return 'Over 9 levels deep, aborting normalization'; } if (is_array($data)) { $normalized = array(); $count = 1; foreach ($data as $key => $value) { if ($count++ > 1000) { $normalized['...'] = 'Over 1000 items ('.count($data).' total), aborting normalization'; break; } $normalized[$key] = $this->normalize($value, $depth+1); } return $normalized; } if ($data instanceof Exception || $data instanceof Throwable) { return $this->normalizeException($data); } return $data; } /** * Normalizes given exception with or without its own stack trace based on * `includeStacktraces` property. * * @param Exception|Throwable $e * * @return array */ protected function normalizeException($e) { // TODO 2.0 only check for Throwable if (!$e instanceof Exception && !$e instanceof Throwable) { throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Utils::getClass($e)); } $data = array( 'class' => Utils::getClass($e), 'message' => $e->getMessage(), 'code' => (int) $e->getCode(), 'file' => $e->getFile().':'.$e->getLine(), ); if ($this->includeStacktraces) { $trace = $e->getTrace(); foreach ($trace as $frame) { if (isset($frame['file'])) { $data['trace'][] = $frame['file'].':'.$frame['line']; } } } if ($previous = $e->getPrevious()) { $data['previous'] = $this->normalizeException($previous); } return $data; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; /** * Serializes a log message to Logstash Event Format * * @see http://logstash.net/ * @see https://github.com/logstash/logstash/blob/master/lib/logstash/event.rb * * @author Tim Mower <timothy.mower@gmail.com> */ class LogstashFormatter extends NormalizerFormatter { const V0 = 0; const V1 = 1; /** * @var string the name of the system for the Logstash log message, used to fill the @source field */ protected $systemName; /** * @var string an application name for the Logstash log message, used to fill the @type field */ protected $applicationName; /** * @var string a prefix for 'extra' fields from the Monolog record (optional) */ protected $extraPrefix; /** * @var string a prefix for 'context' fields from the Monolog record (optional) */ protected $contextPrefix; /** * @var int logstash format version to use */ protected $version; /** * @param string $applicationName the application that sends the data, used as the "type" field of logstash * @param string $systemName the system/machine name, used as the "source" field of logstash, defaults to the hostname of the machine * @param string $extraPrefix prefix for extra keys inside logstash "fields" * @param string $contextPrefix prefix for context keys inside logstash "fields", defaults to ctxt_ * @param int $version the logstash format version to use, defaults to 0 */ public function __construct($applicationName, $systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_', $version = self::V0) { // logstash requires a ISO 8601 format date with optional millisecond precision. parent::__construct('Y-m-d\TH:i:s.uP'); $this->systemName = $systemName ?: gethostname(); $this->applicationName = $applicationName; $this->extraPrefix = $extraPrefix; $this->contextPrefix = $contextPrefix; $this->version = $version; } /** * {@inheritdoc} */ public function format(array $record) { $record = parent::format($record); if ($this->version === self::V1) { $message = $this->formatV1($record); } else { $message = $this->formatV0($record); } return $this->toJson($message) . "\n"; } protected function formatV0(array $record) { if (empty($record['datetime'])) { $record['datetime'] = gmdate('c'); } $message = array( '@timestamp' => $record['datetime'], '@source' => $this->systemName, '@fields' => array(), ); if (isset($record['message'])) { $message['@message'] = $record['message']; } if (isset($record['channel'])) { $message['@tags'] = array($record['channel']); $message['@fields']['channel'] = $record['channel']; } if (isset($record['level'])) { $message['@fields']['level'] = $record['level']; } if ($this->applicationName) { $message['@type'] = $this->applicationName; } if (isset($record['extra']['server'])) { $message['@source_host'] = $record['extra']['server']; } if (isset($record['extra']['url'])) { $message['@source_path'] = $record['extra']['url']; } if (!empty($record['extra'])) { foreach ($record['extra'] as $key => $val) { $message['@fields'][$this->extraPrefix . $key] = $val; } } if (!empty($record['context'])) { foreach ($record['context'] as $key => $val) { $message['@fields'][$this->contextPrefix . $key] = $val; } } return $message; } protected function formatV1(array $record) { if (empty($record['datetime'])) { $record['datetime'] = gmdate('c'); } $message = array( '@timestamp' => $record['datetime'], '@version' => 1, 'host' => $this->systemName, ); if (isset($record['message'])) { $message['message'] = $record['message']; } if (isset($record['channel'])) { $message['type'] = $record['channel']; $message['channel'] = $record['channel']; } if (isset($record['level_name'])) { $message['level'] = $record['level_name']; } if ($this->applicationName) { $message['type'] = $this->applicationName; } if (!empty($record['extra'])) { foreach ($record['extra'] as $key => $val) { $message[$this->extraPrefix . $key] = $val; } } if (!empty($record['context'])) { foreach ($record['context'] as $key => $val) { $message[$this->contextPrefix . $key] = $val; } } return $message; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; use Elastica\Document; /** * Format a log message into an Elastica Document * * @author Jelle Vink <jelle.vink@gmail.com> */ class ElasticaFormatter extends NormalizerFormatter { /** * @var string Elastic search index name */ protected $index; /** * @var string Elastic search document type */ protected $type; /** * @param string $index Elastic Search index name * @param string $type Elastic Search document type */ public function __construct($index, $type) { // elasticsearch requires a ISO 8601 format date with optional millisecond precision. parent::__construct('Y-m-d\TH:i:s.uP'); $this->index = $index; $this->type = $type; } /** * {@inheritdoc} */ public function format(array $record) { $record = parent::format($record); return $this->getDocument($record); } /** * Getter index * @return string */ public function getIndex() { return $this->index; } /** * Getter type * @return string */ public function getType() { return $this->type; } /** * Convert a log message into an Elastica Document * * @param array $record Log message * @return Document */ protected function getDocument($record) { $document = new Document(); $document->setData($record); $document->setType($this->type); $document->setIndex($this->index); return $document; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; use Monolog\Utils; /** * Class FluentdFormatter * * Serializes a log message to Fluentd unix socket protocol * * Fluentd config: * * <source> * type unix * path /var/run/td-agent/td-agent.sock * </source> * * Monolog setup: * * $logger = new Monolog\Logger('fluent.tag'); * $fluentHandler = new Monolog\Handler\SocketHandler('unix:///var/run/td-agent/td-agent.sock'); * $fluentHandler->setFormatter(new Monolog\Formatter\FluentdFormatter()); * $logger->pushHandler($fluentHandler); * * @author Andrius Putna <fordnox@gmail.com> */ class FluentdFormatter implements FormatterInterface { /** * @var bool $levelTag should message level be a part of the fluentd tag */ protected $levelTag = false; public function __construct($levelTag = false) { if (!function_exists('json_encode')) { throw new \RuntimeException('PHP\'s json extension is required to use Monolog\'s FluentdUnixFormatter'); } $this->levelTag = (bool) $levelTag; } public function isUsingLevelsInTag() { return $this->levelTag; } public function format(array $record) { $tag = $record['channel']; if ($this->levelTag) { $tag .= '.' . strtolower($record['level_name']); } $message = array( 'message' => $record['message'], 'context' => $record['context'], 'extra' => $record['extra'], ); if (!$this->levelTag) { $message['level'] = $record['level']; $message['level_name'] = $record['level_name']; } return Utils::jsonEncode(array($tag, $record['datetime']->getTimestamp(), $message)); } public function formatBatch(array $records) { $message = ''; foreach ($records as $record) { $message .= $this->format($record); } return $message; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; /** * Interface for formatters * * @author Jordi Boggiano <j.boggiano@seld.be> */ interface FormatterInterface { /** * Formats a log record. * * @param array $record A record to format * @return mixed The formatted record */ public function format(array $record); /** * Formats a set of log records. * * @param array $records A set of records to format * @return mixed The formatted set of records */ public function formatBatch(array $records); } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; use Monolog\Logger; /** * Formats a log message according to the ChromePHP array format * * @author Christophe Coevoet <stof@notk.org> */ class ChromePHPFormatter implements FormatterInterface { /** * Translates Monolog log levels to Wildfire levels. */ private $logLevels = array( Logger::DEBUG => 'log', Logger::INFO => 'info', Logger::NOTICE => 'info', Logger::WARNING => 'warn', Logger::ERROR => 'error', Logger::CRITICAL => 'error', Logger::ALERT => 'error', Logger::EMERGENCY => 'error', ); /** * {@inheritdoc} */ public function format(array $record) { // Retrieve the line and file if set and remove them from the formatted extra $backtrace = 'unknown'; if (isset($record['extra']['file'], $record['extra']['line'])) { $backtrace = $record['extra']['file'].' : '.$record['extra']['line']; unset($record['extra']['file'], $record['extra']['line']); } $message = array('message' => $record['message']); if ($record['context']) { $message['context'] = $record['context']; } if ($record['extra']) { $message['extra'] = $record['extra']; } if (count($message) === 1) { $message = reset($message); } return array( $record['channel'], $message, $backtrace, $this->logLevels[$record['level']], ); } public function formatBatch(array $records) { $formatted = array(); foreach ($records as $record) { $formatted[] = $this->format($record); } return $formatted; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; use Monolog\Utils; /** * Formats a record for use with the MongoDBHandler. * * @author Florian Plattner <me@florianplattner.de> */ class MongoDBFormatter implements FormatterInterface { private $exceptionTraceAsString; private $maxNestingLevel; /** * @param int $maxNestingLevel 0 means infinite nesting, the $record itself is level 1, $record['context'] is 2 * @param bool $exceptionTraceAsString set to false to log exception traces as a sub documents instead of strings */ public function __construct($maxNestingLevel = 3, $exceptionTraceAsString = true) { $this->maxNestingLevel = max($maxNestingLevel, 0); $this->exceptionTraceAsString = (bool) $exceptionTraceAsString; } /** * {@inheritDoc} */ public function format(array $record) { return $this->formatArray($record); } /** * {@inheritDoc} */ public function formatBatch(array $records) { foreach ($records as $key => $record) { $records[$key] = $this->format($record); } return $records; } protected function formatArray(array $record, $nestingLevel = 0) { if ($this->maxNestingLevel == 0 || $nestingLevel <= $this->maxNestingLevel) { foreach ($record as $name => $value) { if ($value instanceof \DateTime) { $record[$name] = $this->formatDate($value, $nestingLevel + 1); } elseif ($value instanceof \Exception) { $record[$name] = $this->formatException($value, $nestingLevel + 1); } elseif (is_array($value)) { $record[$name] = $this->formatArray($value, $nestingLevel + 1); } elseif (is_object($value)) { $record[$name] = $this->formatObject($value, $nestingLevel + 1); } } } else { $record = '[...]'; } return $record; } protected function formatObject($value, $nestingLevel) { $objectVars = get_object_vars($value); $objectVars['class'] = Utils::getClass($value); return $this->formatArray($objectVars, $nestingLevel); } protected function formatException(\Exception $exception, $nestingLevel) { $formattedException = array( 'class' => Utils::getClass($exception), 'message' => $exception->getMessage(), 'code' => (int) $exception->getCode(), 'file' => $exception->getFile() . ':' . $exception->getLine(), ); if ($this->exceptionTraceAsString === true) { $formattedException['trace'] = $exception->getTraceAsString(); } else { $formattedException['trace'] = $exception->getTrace(); } return $this->formatArray($formattedException, $nestingLevel); } protected function formatDate(\DateTime $value, $nestingLevel) { return new \MongoDate($value->getTimestamp()); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; use Monolog\Utils; /** * Formats incoming records into a one-line string * * This is especially useful for logging to files * * @author Jordi Boggiano <j.boggiano@seld.be> * @author Christophe Coevoet <stof@notk.org> */ class LineFormatter extends NormalizerFormatter { const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"; protected $format; protected $allowInlineLineBreaks; protected $ignoreEmptyContextAndExtra; protected $includeStacktraces; /** * @param string $format The format of the message * @param string $dateFormat The format of the timestamp: one supported by DateTime::format * @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries * @param bool $ignoreEmptyContextAndExtra */ public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false, $ignoreEmptyContextAndExtra = false) { $this->format = $format ?: static::SIMPLE_FORMAT; $this->allowInlineLineBreaks = $allowInlineLineBreaks; $this->ignoreEmptyContextAndExtra = $ignoreEmptyContextAndExtra; parent::__construct($dateFormat); } public function includeStacktraces($include = true) { $this->includeStacktraces = $include; if ($this->includeStacktraces) { $this->allowInlineLineBreaks = true; } } public function allowInlineLineBreaks($allow = true) { $this->allowInlineLineBreaks = $allow; } public function ignoreEmptyContextAndExtra($ignore = true) { $this->ignoreEmptyContextAndExtra = $ignore; } /** * {@inheritdoc} */ public function format(array $record) { $vars = parent::format($record); $output = $this->format; foreach ($vars['extra'] as $var => $val) { if (false !== strpos($output, '%extra.'.$var.'%')) { $output = str_replace('%extra.'.$var.'%', $this->stringify($val), $output); unset($vars['extra'][$var]); } } foreach ($vars['context'] as $var => $val) { if (false !== strpos($output, '%context.'.$var.'%')) { $output = str_replace('%context.'.$var.'%', $this->stringify($val), $output); unset($vars['context'][$var]); } } if ($this->ignoreEmptyContextAndExtra) { if (empty($vars['context'])) { unset($vars['context']); $output = str_replace('%context%', '', $output); } if (empty($vars['extra'])) { unset($vars['extra']); $output = str_replace('%extra%', '', $output); } } foreach ($vars as $var => $val) { if (false !== strpos($output, '%'.$var.'%')) { $output = str_replace('%'.$var.'%', $this->stringify($val), $output); } } // remove leftover %extra.xxx% and %context.xxx% if any if (false !== strpos($output, '%')) { $output = preg_replace('/%(?:extra|context)\..+?%/', '', $output); } return $output; } public function formatBatch(array $records) { $message = ''; foreach ($records as $record) { $message .= $this->format($record); } return $message; } public function stringify($value) { return $this->replaceNewlines($this->convertToString($value)); } protected function normalizeException($e) { // TODO 2.0 only check for Throwable if (!$e instanceof \Exception && !$e instanceof \Throwable) { throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Utils::getClass($e)); } $previousText = ''; if ($previous = $e->getPrevious()) { do { $previousText .= ', '.Utils::getClass($previous).'(code: '.$previous->getCode().'): '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine(); } while ($previous = $previous->getPrevious()); } $str = '[object] ('.Utils::getClass($e).'(code: '.$e->getCode().'): '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')'; if ($this->includeStacktraces) { $str .= "\n[stacktrace]\n".$e->getTraceAsString()."\n"; } return $str; } protected function convertToString($data) { if (null === $data || is_bool($data)) { return var_export($data, true); } if (is_scalar($data)) { return (string) $data; } if (version_compare(PHP_VERSION, '5.4.0', '>=')) { return $this->toJson($data, true); } return str_replace('\\/', '/', $this->toJson($data, true)); } protected function replaceNewlines($str) { if ($this->allowInlineLineBreaks) { if (0 === strpos($str, '{')) { return str_replace(array('\r', '\n'), array("\r", "\n"), $str); } return $str; } return str_replace(array("\r\n", "\r", "\n"), ' ', $str); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; /** * Encodes message information into JSON in a format compatible with Loggly. * * @author Adam Pancutt <adam@pancutt.com> */ class LogglyFormatter extends JsonFormatter { /** * Overrides the default batch mode to new lines for compatibility with the * Loggly bulk API. * * @param int $batchMode */ public function __construct($batchMode = self::BATCH_MODE_NEWLINES, $appendNewline = false) { parent::__construct($batchMode, $appendNewline); } /** * Appends the 'timestamp' parameter for indexing by Loggly. * * @see https://www.loggly.com/docs/automated-parsing/#json * @see \Monolog\Formatter\JsonFormatter::format() */ public function format(array $record) { if (isset($record["datetime"]) && ($record["datetime"] instanceof \DateTime)) { $record["timestamp"] = $record["datetime"]->format("Y-m-d\TH:i:s.uO"); // TODO 2.0 unset the 'datetime' parameter, retained for BC } return parent::format($record); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; use Monolog\Logger; /** * Serializes a log message according to Wildfire's header requirements * * @author Eric Clemmons (@ericclemmons) <eric@uxdriven.com> * @author Christophe Coevoet <stof@notk.org> * @author Kirill chEbba Chebunin <iam@chebba.org> */ class WildfireFormatter extends NormalizerFormatter { const TABLE = 'table'; /** * Translates Monolog log levels to Wildfire levels. */ private $logLevels = array( Logger::DEBUG => 'LOG', Logger::INFO => 'INFO', Logger::NOTICE => 'INFO', Logger::WARNING => 'WARN', Logger::ERROR => 'ERROR', Logger::CRITICAL => 'ERROR', Logger::ALERT => 'ERROR', Logger::EMERGENCY => 'ERROR', ); /** * {@inheritdoc} */ public function format(array $record) { // Retrieve the line and file if set and remove them from the formatted extra $file = $line = ''; if (isset($record['extra']['file'])) { $file = $record['extra']['file']; unset($record['extra']['file']); } if (isset($record['extra']['line'])) { $line = $record['extra']['line']; unset($record['extra']['line']); } $record = $this->normalize($record); $message = array('message' => $record['message']); $handleError = false; if ($record['context']) { $message['context'] = $record['context']; $handleError = true; } if ($record['extra']) { $message['extra'] = $record['extra']; $handleError = true; } if (count($message) === 1) { $message = reset($message); } if (isset($record['context'][self::TABLE])) { $type = 'TABLE'; $label = $record['channel'] .': '. $record['message']; $message = $record['context'][self::TABLE]; } else { $type = $this->logLevels[$record['level']]; $label = $record['channel']; } // Create JSON object describing the appearance of the message in the console $json = $this->toJson(array( array( 'Type' => $type, 'File' => $file, 'Line' => $line, 'Label' => $label, ), $message, ), $handleError); // The message itself is a serialization of the above JSON object + it's length return sprintf( '%s|%s|', strlen($json), $json ); } public function formatBatch(array $records) { throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter'); } protected function normalize($data, $depth = 0) { if (is_object($data) && !$data instanceof \DateTime) { return $data; } return parent::normalize($data, $depth); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler\FingersCrossed; use Monolog\Logger; /** * Channel and Error level based monolog activation strategy. Allows to trigger activation * based on level per channel. e.g. trigger activation on level 'ERROR' by default, except * for records of the 'sql' channel; those should trigger activation on level 'WARN'. * * Example: * * <code> * $activationStrategy = new ChannelLevelActivationStrategy( * Logger::CRITICAL, * array( * 'request' => Logger::ALERT, * 'sensitive' => Logger::ERROR, * ) * ); * $handler = new FingersCrossedHandler(new StreamHandler('php://stderr'), $activationStrategy); * </code> * * @author Mike Meessen <netmikey@gmail.com> */ class ChannelLevelActivationStrategy implements ActivationStrategyInterface { private $defaultActionLevel; private $channelToActionLevel; /** * @param int $defaultActionLevel The default action level to be used if the record's category doesn't match any * @param array $channelToActionLevel An array that maps channel names to action levels. */ public function __construct($defaultActionLevel, $channelToActionLevel = array()) { $this->defaultActionLevel = Logger::toMonologLevel($defaultActionLevel); $this->channelToActionLevel = array_map('Monolog\Logger::toMonologLevel', $channelToActionLevel); } public function isHandlerActivated(array $record) { if (isset($this->channelToActionLevel[$record['channel']])) { return $record['level'] >= $this->channelToActionLevel[$record['channel']]; } return $record['level'] >= $this->defaultActionLevel; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler\FingersCrossed; /** * Interface for activation strategies for the FingersCrossedHandler. * * @author Johannes M. Schmitt <schmittjoh@gmail.com> */ interface ActivationStrategyInterface { /** * Returns whether the given record activates the handler. * * @param array $record * @return bool */ public function isHandlerActivated(array $record); } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler\FingersCrossed; use Monolog\Logger; /** * Error level based activation strategy. * * @author Johannes M. Schmitt <schmittjoh@gmail.com> */ class ErrorLevelActivationStrategy implements ActivationStrategyInterface { private $actionLevel; public function __construct($actionLevel) { $this->actionLevel = Logger::toMonologLevel($actionLevel); } public function isHandlerActivated(array $record) { return $record['level'] >= $this->actionLevel; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Psr\Log\LoggerInterface; /** * Proxies log messages to an existing PSR-3 compliant logger. * * @author Michael Moussa <michael.moussa@gmail.com> */ class PsrHandler extends AbstractHandler { /** * PSR-3 compliant logger * * @var LoggerInterface */ protected $logger; /** * @param LoggerInterface $logger The underlying PSR-3 compliant logger to which messages will be proxied * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct(LoggerInterface $logger, $level = Logger::DEBUG, $bubble = true) { parent::__construct($level, $bubble); $this->logger = $logger; } /** * {@inheritDoc} */ public function handle(array $record) { if (!$this->isHandling($record)) { return false; } $this->logger->log(strtolower($record['level_name']), $record['message'], $record['context']); return false === $this->bubble; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\FormatterInterface; use Monolog\Logger; use Monolog\Utils; use Monolog\Handler\Slack\SlackRecord; /** * Sends notifications through Slack Webhooks * * @author Haralan Dobrev <hkdobrev@gmail.com> * @see https://api.slack.com/incoming-webhooks */ class SlackWebhookHandler extends AbstractProcessingHandler { /** * Slack Webhook token * @var string */ private $webhookUrl; /** * Instance of the SlackRecord util class preparing data for Slack API. * @var SlackRecord */ private $slackRecord; /** * @param string $webhookUrl Slack Webhook URL * @param string|null $channel Slack channel (encoded ID or name) * @param string|null $username Name of a bot * @param bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise) * @param string|null $iconEmoji The emoji name to use (or null) * @param bool $useShortAttachment Whether the the context/extra messages added to Slack as attachments are in a short style * @param bool $includeContextAndExtra Whether the attachment should include context and extra data * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param array $excludeFields Dot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2'] */ public function __construct($webhookUrl, $channel = null, $username = null, $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeContextAndExtra = false, $level = Logger::CRITICAL, $bubble = true, array $excludeFields = array()) { parent::__construct($level, $bubble); $this->webhookUrl = $webhookUrl; $this->slackRecord = new SlackRecord( $channel, $username, $useAttachment, $iconEmoji, $useShortAttachment, $includeContextAndExtra, $excludeFields, $this->formatter ); } public function getSlackRecord() { return $this->slackRecord; } public function getWebhookUrl() { return $this->webhookUrl; } /** * {@inheritdoc} * * @param array $record */ protected function write(array $record) { $postData = $this->slackRecord->getSlackData($record); $postString = Utils::jsonEncode($postData); $ch = curl_init(); $options = array( CURLOPT_URL => $this->webhookUrl, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Content-type: application/json'), CURLOPT_POSTFIELDS => $postString ); if (defined('CURLOPT_SAFE_UPLOAD')) { $options[CURLOPT_SAFE_UPLOAD] = true; } curl_setopt_array($ch, $options); Curl\Util::execute($ch); } public function setFormatter(FormatterInterface $formatter) { parent::setFormatter($formatter); $this->slackRecord->setFormatter($formatter); return $this; } public function getFormatter() { $formatter = parent::getFormatter(); $this->slackRecord->setFormatter($formatter); return $formatter; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler\SyslogUdp; class UdpSocket { const DATAGRAM_MAX_LENGTH = 65023; protected $ip; protected $port; protected $socket; public function __construct($ip, $port = 514) { $this->ip = $ip; $this->port = $port; $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); } public function write($line, $header = "") { $this->send($this->assembleMessage($line, $header)); } public function close() { if (is_resource($this->socket)) { socket_close($this->socket); $this->socket = null; } } protected function send($chunk) { if (!is_resource($this->socket)) { throw new \LogicException('The UdpSocket to '.$this->ip.':'.$this->port.' has been closed and can not be written to anymore'); } socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port); } protected function assembleMessage($line, $header) { $chunkSize = self::DATAGRAM_MAX_LENGTH - strlen($header); return $header . substr($line, 0, $chunkSize); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use RollbarNotifier; use Exception; use Monolog\Logger; /** * Sends errors to Rollbar * * If the context data contains a `payload` key, that is used as an array * of payload options to RollbarNotifier's report_message/report_exception methods. * * Rollbar's context info will contain the context + extra keys from the log record * merged, and then on top of that a few keys: * * - level (rollbar level name) * - monolog_level (monolog level name, raw level, as rollbar only has 5 but monolog 8) * - channel * - datetime (unix timestamp) * * @author Paul Statezny <paulstatezny@gmail.com> */ class RollbarHandler extends AbstractProcessingHandler { /** * Rollbar notifier * * @var RollbarNotifier */ protected $rollbarNotifier; protected $levelMap = array( Logger::DEBUG => 'debug', Logger::INFO => 'info', Logger::NOTICE => 'info', Logger::WARNING => 'warning', Logger::ERROR => 'error', Logger::CRITICAL => 'critical', Logger::ALERT => 'critical', Logger::EMERGENCY => 'critical', ); /** * Records whether any log records have been added since the last flush of the rollbar notifier * * @var bool */ private $hasRecords = false; protected $initialized = false; /** * @param RollbarNotifier $rollbarNotifier RollbarNotifier object constructed with valid token * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct(RollbarNotifier $rollbarNotifier, $level = Logger::ERROR, $bubble = true) { $this->rollbarNotifier = $rollbarNotifier; parent::__construct($level, $bubble); } /** * {@inheritdoc} */ protected function write(array $record) { if (!$this->initialized) { // __destructor() doesn't get called on Fatal errors register_shutdown_function(array($this, 'close')); $this->initialized = true; } $context = $record['context']; $payload = array(); if (isset($context['payload'])) { $payload = $context['payload']; unset($context['payload']); } $context = array_merge($context, $record['extra'], array( 'level' => $this->levelMap[$record['level']], 'monolog_level' => $record['level_name'], 'channel' => $record['channel'], 'datetime' => $record['datetime']->format('U'), )); if (isset($context['exception']) && $context['exception'] instanceof Exception) { $payload['level'] = $context['level']; $exception = $context['exception']; unset($context['exception']); $this->rollbarNotifier->report_exception($exception, $context, $payload); } else { $this->rollbarNotifier->report_message( $record['message'], $context['level'], $context, $payload ); } $this->hasRecords = true; } public function flush() { if ($this->hasRecords) { $this->rollbarNotifier->flush(); $this->hasRecords = false; } } /** * {@inheritdoc} */ public function close() { $this->flush(); } /** * {@inheritdoc} */ public function reset() { $this->flush(); parent::reset(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\FormatterInterface; use Monolog\Logger; use Monolog\Utils; use Monolog\Handler\Slack\SlackRecord; /** * Sends notifications through Slack API * * @author Greg Kedzierski <greg@gregkedzierski.com> * @see https://api.slack.com/ */ class SlackHandler extends SocketHandler { /** * Slack API token * @var string */ private $token; /** * Instance of the SlackRecord util class preparing data for Slack API. * @var SlackRecord */ private $slackRecord; /** * @param string $token Slack API token * @param string $channel Slack channel (encoded ID or name) * @param string|null $username Name of a bot * @param bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise) * @param string|null $iconEmoji The emoji name to use (or null) * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param bool $useShortAttachment Whether the the context/extra messages added to Slack as attachments are in a short style * @param bool $includeContextAndExtra Whether the attachment should include context and extra data * @param array $excludeFields Dot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2'] * @throws MissingExtensionException If no OpenSSL PHP extension configured */ public function __construct($token, $channel, $username = null, $useAttachment = true, $iconEmoji = null, $level = Logger::CRITICAL, $bubble = true, $useShortAttachment = false, $includeContextAndExtra = false, array $excludeFields = array()) { if (!extension_loaded('openssl')) { throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler'); } parent::__construct('ssl://slack.com:443', $level, $bubble); $this->slackRecord = new SlackRecord( $channel, $username, $useAttachment, $iconEmoji, $useShortAttachment, $includeContextAndExtra, $excludeFields, $this->formatter ); $this->token = $token; } public function getSlackRecord() { return $this->slackRecord; } public function getToken() { return $this->token; } /** * {@inheritdoc} * * @param array $record * @return string */ protected function generateDataStream($record) { $content = $this->buildContent($record); return $this->buildHeader($content) . $content; } /** * Builds the body of API call * * @param array $record * @return string */ private function buildContent($record) { $dataArray = $this->prepareContentData($record); return http_build_query($dataArray); } /** * Prepares content data * * @param array $record * @return array */ protected function prepareContentData($record) { $dataArray = $this->slackRecord->getSlackData($record); $dataArray['token'] = $this->token; if (!empty($dataArray['attachments'])) { $dataArray['attachments'] = Utils::jsonEncode($dataArray['attachments']); } return $dataArray; } /** * Builds the header of the API Call * * @param string $content * @return string */ private function buildHeader($content) { $header = "POST /api/chat.postMessage HTTP/1.1\r\n"; $header .= "Host: slack.com\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($content) . "\r\n"; $header .= "\r\n"; return $header; } /** * {@inheritdoc} * * @param array $record */ protected function write(array $record) { parent::write($record); $this->finalizeWrite(); } /** * Finalizes the request by reading some bytes and then closing the socket * * If we do not read some but close the socket too early, slack sometimes * drops the request entirely. */ protected function finalizeWrite() { $res = $this->getResource(); if (is_resource($res)) { @fread($res, 2048); } $this->closeSocket(); } /** * Returned a Slack message attachment color associated with * provided level. * * @param int $level * @return string * @deprecated Use underlying SlackRecord instead */ protected function getAttachmentColor($level) { trigger_error( 'SlackHandler::getAttachmentColor() is deprecated. Use underlying SlackRecord instead.', E_USER_DEPRECATED ); return $this->slackRecord->getAttachmentColor($level); } /** * Stringifies an array of key/value pairs to be used in attachment fields * * @param array $fields * @return string * @deprecated Use underlying SlackRecord instead */ protected function stringify($fields) { trigger_error( 'SlackHandler::stringify() is deprecated. Use underlying SlackRecord instead.', E_USER_DEPRECATED ); return $this->slackRecord->stringify($fields); } public function setFormatter(FormatterInterface $formatter) { parent::setFormatter($formatter); $this->slackRecord->setFormatter($formatter); return $this; } public function getFormatter() { $formatter = parent::getFormatter(); $this->slackRecord->setFormatter($formatter); return $formatter; } } <?php declare(strict_types=1); /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\FormatterInterface; use Monolog\Formatter\LineFormatter; /** * Helper trait for implementing FormattableInterface * * This trait is present in monolog 1.x to ease forward compatibility. * * @author Jordi Boggiano <j.boggiano@seld.be> */ trait FormattableHandlerTrait { /** * @var FormatterInterface */ protected $formatter; /** * {@inheritdoc} * @suppress PhanTypeMismatchReturn */ public function setFormatter(FormatterInterface $formatter): HandlerInterface { $this->formatter = $formatter; return $this; } /** * {@inheritdoc} */ public function getFormatter(): FormatterInterface { if (!$this->formatter) { $this->formatter = $this->getDefaultFormatter(); } return $this->formatter; } /** * Gets the default formatter. * * Overwrite this if the LineFormatter is not a good default for your handler. */ protected function getDefaultFormatter(): FormatterInterface { return new LineFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * Logs to syslog service. * * usage example: * * $log = new Logger('application'); * $syslog = new SyslogHandler('myfacility', 'local6'); * $formatter = new LineFormatter("%channel%.%level_name%: %message% %extra%"); * $syslog->setFormatter($formatter); * $log->pushHandler($syslog); * * @author Sven Paulus <sven@karlsruhe.org> */ class SyslogHandler extends AbstractSyslogHandler { protected $ident; protected $logopts; /** * @param string $ident * @param mixed $facility * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param int $logopts Option flags for the openlog() call, defaults to LOG_PID */ public function __construct($ident, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true, $logopts = LOG_PID) { parent::__construct($facility, $level, $bubble); $this->ident = $ident; $this->logopts = $logopts; } /** * {@inheritdoc} */ public function close() { closelog(); } /** * {@inheritdoc} */ protected function write(array $record) { if (!openlog($this->ident, $this->logopts, $this->facility)) { throw new \LogicException('Can\'t open syslog for ident "'.$this->ident.'" and facility "'.$this->facility.'"'); } syslog($this->logLevels[$record['level']], (string) $record['formatted']); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Handler\SyslogUdp\UdpSocket; /** * A Handler for logging to a remote syslogd server. * * @author Jesper Skovgaard Nielsen <nulpunkt@gmail.com> * @author Dominik Kukacka <dominik.kukacka@gmail.com> */ class SyslogUdpHandler extends AbstractSyslogHandler { const RFC3164 = 0; const RFC5424 = 1; private $dateFormats = array( self::RFC3164 => 'M d H:i:s', self::RFC5424 => \DateTime::RFC3339, ); protected $socket; protected $ident; protected $rfc; /** * @param string $host * @param int $port * @param mixed $facility * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param string $ident Program name or tag for each log message. * @param int $rfc RFC to format the message for. */ public function __construct($host, $port = 514, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true, $ident = 'php', $rfc = self::RFC5424) { parent::__construct($facility, $level, $bubble); $this->ident = $ident; $this->rfc = $rfc; $this->socket = new UdpSocket($host, $port ?: 514); } protected function write(array $record) { $lines = $this->splitMessageIntoLines($record['formatted']); $header = $this->makeCommonSyslogHeader($this->logLevels[$record['level']]); foreach ($lines as $line) { $this->socket->write($line, $header); } } public function close() { $this->socket->close(); } private function splitMessageIntoLines($message) { if (is_array($message)) { $message = implode("\n", $message); } return preg_split('/$\R?^/m', $message, -1, PREG_SPLIT_NO_EMPTY); } /** * Make common syslog header (see rfc5424 or rfc3164) */ protected function makeCommonSyslogHeader($severity) { $priority = $severity + $this->facility; if (!$pid = getmypid()) { $pid = '-'; } if (!$hostname = gethostname()) { $hostname = '-'; } $date = $this->getDateTime(); if ($this->rfc === self::RFC3164) { return "<$priority>" . $date . " " . $hostname . " " . $this->ident . "[" . $pid . "]: "; } else { return "<$priority>1 " . $date . " " . $hostname . " " . $this->ident . " " . $pid . " - - "; } } protected function getDateTime() { return date($this->dateFormats[$this->rfc]); } /** * Inject your own socket, mainly used for testing */ public function setSocket($socket) { $this->socket = $socket; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler\Slack; use Monolog\Logger; use Monolog\Utils; use Monolog\Formatter\NormalizerFormatter; use Monolog\Formatter\FormatterInterface; /** * Slack record utility helping to log to Slack webhooks or API. * * @author Greg Kedzierski <greg@gregkedzierski.com> * @author Haralan Dobrev <hkdobrev@gmail.com> * @see https://api.slack.com/incoming-webhooks * @see https://api.slack.com/docs/message-attachments */ class SlackRecord { const COLOR_DANGER = 'danger'; const COLOR_WARNING = 'warning'; const COLOR_GOOD = 'good'; const COLOR_DEFAULT = '#e3e4e6'; /** * Slack channel (encoded ID or name) * @var string|null */ private $channel; /** * Name of a bot * @var string|null */ private $username; /** * User icon e.g. 'ghost', 'http://example.com/user.png' * @var string */ private $userIcon; /** * Whether the message should be added to Slack as attachment (plain text otherwise) * @var bool */ private $useAttachment; /** * Whether the the context/extra messages added to Slack as attachments are in a short style * @var bool */ private $useShortAttachment; /** * Whether the attachment should include context and extra data * @var bool */ private $includeContextAndExtra; /** * Dot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2'] * @var array */ private $excludeFields; /** * @var FormatterInterface */ private $formatter; /** * @var NormalizerFormatter */ private $normalizerFormatter; public function __construct($channel = null, $username = null, $useAttachment = true, $userIcon = null, $useShortAttachment = false, $includeContextAndExtra = false, array $excludeFields = array(), FormatterInterface $formatter = null) { $this->channel = $channel; $this->username = $username; $this->userIcon = trim($userIcon, ':'); $this->useAttachment = $useAttachment; $this->useShortAttachment = $useShortAttachment; $this->includeContextAndExtra = $includeContextAndExtra; $this->excludeFields = $excludeFields; $this->formatter = $formatter; if ($this->includeContextAndExtra) { $this->normalizerFormatter = new NormalizerFormatter(); } } public function getSlackData(array $record) { $dataArray = array(); $record = $this->excludeFields($record); if ($this->username) { $dataArray['username'] = $this->username; } if ($this->channel) { $dataArray['channel'] = $this->channel; } if ($this->formatter && !$this->useAttachment) { $message = $this->formatter->format($record); } else { $message = $record['message']; } if ($this->useAttachment) { $attachment = array( 'fallback' => $message, 'text' => $message, 'color' => $this->getAttachmentColor($record['level']), 'fields' => array(), 'mrkdwn_in' => array('fields'), 'ts' => $record['datetime']->getTimestamp() ); if ($this->useShortAttachment) { $attachment['title'] = $record['level_name']; } else { $attachment['title'] = 'Message'; $attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name']); } if ($this->includeContextAndExtra) { foreach (array('extra', 'context') as $key) { if (empty($record[$key])) { continue; } if ($this->useShortAttachment) { $attachment['fields'][] = $this->generateAttachmentField( $key, $record[$key] ); } else { // Add all extra fields as individual fields in attachment $attachment['fields'] = array_merge( $attachment['fields'], $this->generateAttachmentFields($record[$key]) ); } } } $dataArray['attachments'] = array($attachment); } else { $dataArray['text'] = $message; } if ($this->userIcon) { if (filter_var($this->userIcon, FILTER_VALIDATE_URL)) { $dataArray['icon_url'] = $this->userIcon; } else { $dataArray['icon_emoji'] = ":{$this->userIcon}:"; } } return $dataArray; } /** * Returned a Slack message attachment color associated with * provided level. * * @param int $level * @return string */ public function getAttachmentColor($level) { switch (true) { case $level >= Logger::ERROR: return self::COLOR_DANGER; case $level >= Logger::WARNING: return self::COLOR_WARNING; case $level >= Logger::INFO: return self::COLOR_GOOD; default: return self::COLOR_DEFAULT; } } /** * Stringifies an array of key/value pairs to be used in attachment fields * * @param array $fields * * @return string */ public function stringify($fields) { $normalized = $this->normalizerFormatter->format($fields); $prettyPrintFlag = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 128; $flags = 0; if (PHP_VERSION_ID >= 50400) { $flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; } $hasSecondDimension = count(array_filter($normalized, 'is_array')); $hasNonNumericKeys = !count(array_filter(array_keys($normalized), 'is_numeric')); return $hasSecondDimension || $hasNonNumericKeys ? Utils::jsonEncode($normalized, $prettyPrintFlag | $flags) : Utils::jsonEncode($normalized, $flags); } /** * Sets the formatter * * @param FormatterInterface $formatter */ public function setFormatter(FormatterInterface $formatter) { $this->formatter = $formatter; } /** * Generates attachment field * * @param string $title * @param string|array $value * * @return array */ private function generateAttachmentField($title, $value) { $value = is_array($value) ? sprintf('```%s```', $this->stringify($value)) : $value; return array( 'title' => ucfirst($title), 'value' => $value, 'short' => false ); } /** * Generates a collection of attachment fields from array * * @param array $data * * @return array */ private function generateAttachmentFields(array $data) { $fields = array(); foreach ($this->normalizerFormatter->format($data) as $key => $value) { $fields[] = $this->generateAttachmentField($key, $value); } return $fields; } /** * Get a copy of record with fields excluded according to $this->excludeFields * * @param array $record * * @return array */ private function excludeFields(array $record) { foreach ($this->excludeFields as $field) { $keys = explode('.', $field); $node = &$record; $lastKey = end($keys); foreach ($keys as $key) { if (!isset($node[$key])) { break; } if ($lastKey === $key) { unset($node[$key]); break; } $node = &$node[$key]; } } return $record; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * @author Robert Kaufmann III <rok3@rok3.me> */ class LogEntriesHandler extends SocketHandler { /** * @var string */ protected $logToken; /** * @param string $token Log token supplied by LogEntries * @param bool $useSSL Whether or not SSL encryption should be used. * @param int $level The minimum logging level to trigger this handler * @param bool $bubble Whether or not messages that are handled should bubble up the stack. * * @throws MissingExtensionException If SSL encryption is set to true and OpenSSL is missing */ public function __construct($token, $useSSL = true, $level = Logger::DEBUG, $bubble = true, $host = 'data.logentries.com') { if ($useSSL && !extension_loaded('openssl')) { throw new MissingExtensionException('The OpenSSL PHP plugin is required to use SSL encrypted connection for LogEntriesHandler'); } $endpoint = $useSSL ? 'ssl://' . $host . ':443' : $host . ':80'; parent::__construct($endpoint, $level, $bubble); $this->logToken = $token; } /** * {@inheritdoc} * * @param array $record * @return string */ protected function generateDataStream($record) { return $this->logToken . ' ' . $record['formatted']; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\FormatterInterface; use Monolog\Formatter\LineFormatter; use Monolog\Logger; use Monolog\ResettableInterface; /** * Base Handler class providing the Handler structure * * @author Jordi Boggiano <j.boggiano@seld.be> */ abstract class AbstractHandler implements HandlerInterface, ResettableInterface { protected $level = Logger::DEBUG; protected $bubble = true; /** * @var FormatterInterface */ protected $formatter; protected $processors = array(); /** * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct($level = Logger::DEBUG, $bubble = true) { $this->setLevel($level); $this->bubble = $bubble; } /** * {@inheritdoc} */ public function isHandling(array $record) { return $record['level'] >= $this->level; } /** * {@inheritdoc} */ public function handleBatch(array $records) { foreach ($records as $record) { $this->handle($record); } } /** * Closes the handler. * * This will be called automatically when the object is destroyed */ public function close() { } /** * {@inheritdoc} */ public function pushProcessor($callback) { if (!is_callable($callback)) { throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given'); } array_unshift($this->processors, $callback); return $this; } /** * {@inheritdoc} */ public function popProcessor() { if (!$this->processors) { throw new \LogicException('You tried to pop from an empty processor stack.'); } return array_shift($this->processors); } /** * {@inheritdoc} */ public function setFormatter(FormatterInterface $formatter) { $this->formatter = $formatter; return $this; } /** * {@inheritdoc} */ public function getFormatter() { if (!$this->formatter) { $this->formatter = $this->getDefaultFormatter(); } return $this->formatter; } /** * Sets minimum logging level at which this handler will be triggered. * * @param int|string $level Level or level name * @return self */ public function setLevel($level) { $this->level = Logger::toMonologLevel($level); return $this; } /** * Gets minimum logging level at which this handler will be triggered. * * @return int */ public function getLevel() { return $this->level; } /** * Sets the bubbling behavior. * * @param bool $bubble true means that this handler allows bubbling. * false means that bubbling is not permitted. * @return self */ public function setBubble($bubble) { $this->bubble = $bubble; return $this; } /** * Gets the bubbling behavior. * * @return bool true means that this handler allows bubbling. * false means that bubbling is not permitted. */ public function getBubble() { return $this->bubble; } public function __destruct() { try { $this->close(); } catch (\Exception $e) { // do nothing } catch (\Throwable $e) { // do nothing } } public function reset() { foreach ($this->processors as $processor) { if ($processor instanceof ResettableInterface) { $processor->reset(); } } } /** * Gets the default formatter. * * @return FormatterInterface */ protected function getDefaultFormatter() { return new LineFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * Blackhole * * Any record it can handle will be thrown away. This can be used * to put on top of an existing stack to override it temporarily. * * @author Jordi Boggiano <j.boggiano@seld.be> */ class NullHandler extends AbstractHandler { /** * @param int $level The minimum logging level at which this handler will be triggered */ public function __construct($level = Logger::DEBUG) { parent::__construct($level, false); } /** * {@inheritdoc} */ public function handle(array $record) { if ($record['level'] < $this->level) { return false; } return true; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\ResettableInterface; /** * Base Handler class providing the Handler structure * * Classes extending it should (in most cases) only implement write($record) * * @author Jordi Boggiano <j.boggiano@seld.be> * @author Christophe Coevoet <stof@notk.org> */ abstract class AbstractProcessingHandler extends AbstractHandler { /** * {@inheritdoc} */ public function handle(array $record) { if (!$this->isHandling($record)) { return false; } $record = $this->processRecord($record); $record['formatted'] = $this->getFormatter()->format($record); $this->write($record); return false === $this->bubble; } /** * Writes the record down to the log of the implementing handler * * @param array $record * @return void */ abstract protected function write(array $record); /** * Processes a record. * * @param array $record * @return array */ protected function processRecord(array $record) { if ($this->processors) { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } } return $record; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; /** * Forwards records to multiple handlers suppressing failures of each handler * and continuing through to give every handler a chance to succeed. * * @author Craig D'Amelio <craig@damelio.ca> */ class WhatFailureGroupHandler extends GroupHandler { /** * {@inheritdoc} */ public function handle(array $record) { if ($this->processors) { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } } foreach ($this->handlers as $handler) { try { $handler->handle($record); } catch (\Exception $e) { // What failure? } catch (\Throwable $e) { // What failure? } } return false === $this->bubble; } /** * {@inheritdoc} */ public function handleBatch(array $records) { if ($this->processors) { $processed = array(); foreach ($records as $record) { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } $processed[] = $record; } $records = $processed; } foreach ($this->handlers as $handler) { try { $handler->handleBatch($records); } catch (\Exception $e) { // What failure? } catch (\Throwable $e) { // What failure? } } } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Formatter\LineFormatter; /** * Common syslog functionality */ abstract class AbstractSyslogHandler extends AbstractProcessingHandler { protected $facility; /** * Translates Monolog log levels to syslog log priorities. */ protected $logLevels = array( Logger::DEBUG => LOG_DEBUG, Logger::INFO => LOG_INFO, Logger::NOTICE => LOG_NOTICE, Logger::WARNING => LOG_WARNING, Logger::ERROR => LOG_ERR, Logger::CRITICAL => LOG_CRIT, Logger::ALERT => LOG_ALERT, Logger::EMERGENCY => LOG_EMERG, ); /** * List of valid log facility names. */ protected $facilities = array( 'auth' => LOG_AUTH, 'authpriv' => LOG_AUTHPRIV, 'cron' => LOG_CRON, 'daemon' => LOG_DAEMON, 'kern' => LOG_KERN, 'lpr' => LOG_LPR, 'mail' => LOG_MAIL, 'news' => LOG_NEWS, 'syslog' => LOG_SYSLOG, 'user' => LOG_USER, 'uucp' => LOG_UUCP, ); /** * @param mixed $facility * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct($facility = LOG_USER, $level = Logger::DEBUG, $bubble = true) { parent::__construct($level, $bubble); if (!defined('PHP_WINDOWS_VERSION_BUILD')) { $this->facilities['local0'] = LOG_LOCAL0; $this->facilities['local1'] = LOG_LOCAL1; $this->facilities['local2'] = LOG_LOCAL2; $this->facilities['local3'] = LOG_LOCAL3; $this->facilities['local4'] = LOG_LOCAL4; $this->facilities['local5'] = LOG_LOCAL5; $this->facilities['local6'] = LOG_LOCAL6; $this->facilities['local7'] = LOG_LOCAL7; } else { $this->facilities['local0'] = 128; // LOG_LOCAL0 $this->facilities['local1'] = 136; // LOG_LOCAL1 $this->facilities['local2'] = 144; // LOG_LOCAL2 $this->facilities['local3'] = 152; // LOG_LOCAL3 $this->facilities['local4'] = 160; // LOG_LOCAL4 $this->facilities['local5'] = 168; // LOG_LOCAL5 $this->facilities['local6'] = 176; // LOG_LOCAL6 $this->facilities['local7'] = 184; // LOG_LOCAL7 } // convert textual description of facility to syslog constant if (array_key_exists(strtolower($facility), $this->facilities)) { $facility = $this->facilities[strtolower($facility)]; } elseif (!in_array($facility, array_values($this->facilities), true)) { throw new \UnexpectedValueException('Unknown facility value "'.$facility.'" given'); } $this->facility = $facility; } /** * {@inheritdoc} */ protected function getDefaultFormatter() { return new LineFormatter('%channel%.%level_name%: %message% %context% %extra%'); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\LineFormatter; use Monolog\Formatter\FormatterInterface; use Monolog\Logger; use Raven_Client; /** * Handler to send messages to a Sentry (https://github.com/getsentry/sentry) server * using sentry-php (https://github.com/getsentry/sentry-php) * * @author Marc Abramowitz <marc@marc-abramowitz.com> */ class RavenHandler extends AbstractProcessingHandler { /** * Translates Monolog log levels to Raven log levels. */ protected $logLevels = array( Logger::DEBUG => Raven_Client::DEBUG, Logger::INFO => Raven_Client::INFO, Logger::NOTICE => Raven_Client::INFO, Logger::WARNING => Raven_Client::WARNING, Logger::ERROR => Raven_Client::ERROR, Logger::CRITICAL => Raven_Client::FATAL, Logger::ALERT => Raven_Client::FATAL, Logger::EMERGENCY => Raven_Client::FATAL, ); /** * @var string should represent the current version of the calling * software. Can be any string (git commit, version number) */ protected $release; /** * @var Raven_Client the client object that sends the message to the server */ protected $ravenClient; /** * @var LineFormatter The formatter to use for the logs generated via handleBatch() */ protected $batchFormatter; /** * @param Raven_Client $ravenClient * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct(Raven_Client $ravenClient, $level = Logger::DEBUG, $bubble = true) { @trigger_error('The Monolog\Handler\RavenHandler class is deprecated. You should rather upgrade to the sentry/sentry 2.x and use Sentry\Monolog\Handler, see https://github.com/getsentry/sentry-php/blob/master/src/Monolog/Handler.php', E_USER_DEPRECATED); parent::__construct($level, $bubble); $this->ravenClient = $ravenClient; } /** * {@inheritdoc} */ public function handleBatch(array $records) { $level = $this->level; // filter records based on their level $records = array_filter($records, function ($record) use ($level) { return $record['level'] >= $level; }); if (!$records) { return; } // the record with the highest severity is the "main" one $record = array_reduce($records, function ($highest, $record) { if ($record['level'] > $highest['level']) { return $record; } return $highest; }); // the other ones are added as a context item $logs = array(); foreach ($records as $r) { $logs[] = $this->processRecord($r); } if ($logs) { $record['context']['logs'] = (string) $this->getBatchFormatter()->formatBatch($logs); } $this->handle($record); } /** * Sets the formatter for the logs generated by handleBatch(). * * @param FormatterInterface $formatter */ public function setBatchFormatter(FormatterInterface $formatter) { $this->batchFormatter = $formatter; } /** * Gets the formatter for the logs generated by handleBatch(). * * @return FormatterInterface */ public function getBatchFormatter() { if (!$this->batchFormatter) { $this->batchFormatter = $this->getDefaultBatchFormatter(); } return $this->batchFormatter; } /** * {@inheritdoc} */ protected function write(array $record) { $previousUserContext = false; $options = array(); $options['level'] = $this->logLevels[$record['level']]; $options['tags'] = array(); if (!empty($record['extra']['tags'])) { $options['tags'] = array_merge($options['tags'], $record['extra']['tags']); unset($record['extra']['tags']); } if (!empty($record['context']['tags'])) { $options['tags'] = array_merge($options['tags'], $record['context']['tags']); unset($record['context']['tags']); } if (!empty($record['context']['fingerprint'])) { $options['fingerprint'] = $record['context']['fingerprint']; unset($record['context']['fingerprint']); } if (!empty($record['context']['logger'])) { $options['logger'] = $record['context']['logger']; unset($record['context']['logger']); } else { $options['logger'] = $record['channel']; } foreach ($this->getExtraParameters() as $key) { foreach (array('extra', 'context') as $source) { if (!empty($record[$source][$key])) { $options[$key] = $record[$source][$key]; unset($record[$source][$key]); } } } if (!empty($record['context'])) { $options['extra']['context'] = $record['context']; if (!empty($record['context']['user'])) { $previousUserContext = $this->ravenClient->context->user; $this->ravenClient->user_context($record['context']['user']); unset($options['extra']['context']['user']); } } if (!empty($record['extra'])) { $options['extra']['extra'] = $record['extra']; } if (!empty($this->release) && !isset($options['release'])) { $options['release'] = $this->release; } if (isset($record['context']['exception']) && ($record['context']['exception'] instanceof \Exception || (PHP_VERSION_ID >= 70000 && $record['context']['exception'] instanceof \Throwable))) { $options['message'] = $record['formatted']; $this->ravenClient->captureException($record['context']['exception'], $options); } else { $this->ravenClient->captureMessage($record['formatted'], array(), $options); } if ($previousUserContext !== false) { $this->ravenClient->user_context($previousUserContext); } } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new LineFormatter('[%channel%] %message%'); } /** * Gets the default formatter for the logs generated by handleBatch(). * * @return FormatterInterface */ protected function getDefaultBatchFormatter() { return new LineFormatter(); } /** * Gets extra parameters supported by Raven that can be found in "extra" and "context" * * @return array */ protected function getExtraParameters() { return array('contexts', 'checksum', 'release', 'event_id'); } /** * @param string $value * @return self */ public function setRelease($value) { $this->release = $value; return $this; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * Sends notifications through Slack's Slackbot * * @author Haralan Dobrev <hkdobrev@gmail.com> * @see https://slack.com/apps/A0F81R8ET-slackbot * @deprecated According to Slack the API used on this handler it is deprecated. * Therefore this handler will be removed on 2.x * Slack suggests to use webhooks instead. Please contact slack for more information. */ class SlackbotHandler extends AbstractProcessingHandler { /** * The slug of the Slack team * @var string */ private $slackTeam; /** * Slackbot token * @var string */ private $token; /** * Slack channel name * @var string */ private $channel; /** * @param string $slackTeam Slack team slug * @param string $token Slackbot token * @param string $channel Slack channel (encoded ID or name) * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct($slackTeam, $token, $channel, $level = Logger::CRITICAL, $bubble = true) { @trigger_error('SlackbotHandler is deprecated and will be removed on 2.x', E_USER_DEPRECATED); parent::__construct($level, $bubble); $this->slackTeam = $slackTeam; $this->token = $token; $this->channel = $channel; } /** * {@inheritdoc} * * @param array $record */ protected function write(array $record) { $slackbotUrl = sprintf( 'https://%s.slack.com/services/hooks/slackbot?token=%s&channel=%s', $this->slackTeam, $this->token, $this->channel ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $slackbotUrl); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $record['message']); Curl\Util::execute($ch); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * Inspired on LogEntriesHandler. * * @author Robert Kaufmann III <rok3@rok3.me> * @author Gabriel Machado <gabriel.ms1@hotmail.com> */ class InsightOpsHandler extends SocketHandler { /** * @var string */ protected $logToken; /** * @param string $token Log token supplied by InsightOps * @param string $region Region where InsightOps account is hosted. Could be 'us' or 'eu'. * @param bool $useSSL Whether or not SSL encryption should be used * @param int $level The minimum logging level to trigger this handler * @param bool $bubble Whether or not messages that are handled should bubble up the stack. * * @throws MissingExtensionException If SSL encryption is set to true and OpenSSL is missing */ public function __construct($token, $region = 'us', $useSSL = true, $level = Logger::DEBUG, $bubble = true) { if ($useSSL && !extension_loaded('openssl')) { throw new MissingExtensionException('The OpenSSL PHP plugin is required to use SSL encrypted connection for InsightOpsHandler'); } $endpoint = $useSSL ? 'ssl://' . $region . '.data.logs.insight.rapid7.com:443' : $region . '.data.logs.insight.rapid7.com:80'; parent::__construct($endpoint, $level, $bubble); $this->logToken = $token; } /** * {@inheritdoc} * * @param array $record * @return string */ protected function generateDataStream($record) { return $this->logToken . ' ' . $record['formatted']; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Formatter\FormatterInterface; use Monolog\Formatter\LineFormatter; use Swift; /** * SwiftMailerHandler uses Swift_Mailer to send the emails * * @author Gyula Sallai */ class SwiftMailerHandler extends MailHandler { protected $mailer; private $messageTemplate; /** * @param \Swift_Mailer $mailer The mailer to use * @param callable|\Swift_Message $message An example message for real messages, only the body will be replaced * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = true) { parent::__construct($level, $bubble); $this->mailer = $mailer; $this->messageTemplate = $message; } /** * {@inheritdoc} */ protected function send($content, array $records) { $this->mailer->send($this->buildMessage($content, $records)); } /** * Gets the formatter for the Swift_Message subject. * * @param string $format The format of the subject * @return FormatterInterface */ protected function getSubjectFormatter($format) { return new LineFormatter($format); } /** * Creates instance of Swift_Message to be sent * * @param string $content formatted email body to be sent * @param array $records Log records that formed the content * @return \Swift_Message */ protected function buildMessage($content, array $records) { $message = null; if ($this->messageTemplate instanceof \Swift_Message) { $message = clone $this->messageTemplate; $message->generateId(); } elseif (is_callable($this->messageTemplate)) { $message = call_user_func($this->messageTemplate, $content, $records); } if (!$message instanceof \Swift_Message) { throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it'); } if ($records) { $subjectFormatter = $this->getSubjectFormatter($message->getSubject()); $message->setSubject($subjectFormatter->format($this->getHighestRecord($records))); } $message->setBody($content); if (version_compare(Swift::VERSION, '6.0.0', '>=')) { $message->setDate(new \DateTimeImmutable()); } else { $message->setDate(time()); } return $message; } /** * BC getter, to be removed in 2.0 */ public function __get($name) { if ($name === 'message') { trigger_error('SwiftMailerHandler->message is deprecated, use ->buildMessage() instead to retrieve the message', E_USER_DEPRECATED); return $this->buildMessage(null, array()); } throw new \InvalidArgumentException('Invalid property '.$name); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Formatter\NormalizerFormatter; /** * Logs to a MongoDB database. * * usage example: * * $log = new Logger('application'); * $mongodb = new MongoDBHandler(new \Mongo("mongodb://localhost:27017"), "logs", "prod"); * $log->pushHandler($mongodb); * * @author Thomas Tourlourat <thomas@tourlourat.com> */ class MongoDBHandler extends AbstractProcessingHandler { protected $mongoCollection; public function __construct($mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true) { if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo || $mongo instanceof \MongoDB\Client)) { throw new \InvalidArgumentException('MongoClient, Mongo or MongoDB\Client instance required'); } $this->mongoCollection = $mongo->selectCollection($database, $collection); parent::__construct($level, $bubble); } protected function write(array $record) { if ($this->mongoCollection instanceof \MongoDB\Collection) { $this->mongoCollection->insertOne($record["formatted"]); } else { $this->mongoCollection->save($record["formatted"]); } } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new NormalizerFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\ResettableInterface; use Monolog\Formatter\FormatterInterface; /** * Buffers all records until closing the handler and then pass them as batch. * * This is useful for a MailHandler to send only one mail per request instead of * sending one per log message. * * @author Christophe Coevoet <stof@notk.org> */ class BufferHandler extends AbstractHandler { protected $handler; protected $bufferSize = 0; protected $bufferLimit; protected $flushOnOverflow; protected $buffer = array(); protected $initialized = false; /** * @param HandlerInterface $handler Handler. * @param int $bufferLimit How many entries should be buffered at most, beyond that the oldest items are removed from the buffer. * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param bool $flushOnOverflow If true, the buffer is flushed when the max size has been reached, by default oldest entries are discarded */ public function __construct(HandlerInterface $handler, $bufferLimit = 0, $level = Logger::DEBUG, $bubble = true, $flushOnOverflow = false) { parent::__construct($level, $bubble); $this->handler = $handler; $this->bufferLimit = (int) $bufferLimit; $this->flushOnOverflow = $flushOnOverflow; } /** * {@inheritdoc} */ public function handle(array $record) { if ($record['level'] < $this->level) { return false; } if (!$this->initialized) { // __destructor() doesn't get called on Fatal errors register_shutdown_function(array($this, 'close')); $this->initialized = true; } if ($this->bufferLimit > 0 && $this->bufferSize === $this->bufferLimit) { if ($this->flushOnOverflow) { $this->flush(); } else { array_shift($this->buffer); $this->bufferSize--; } } if ($this->processors) { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } } $this->buffer[] = $record; $this->bufferSize++; return false === $this->bubble; } public function flush() { if ($this->bufferSize === 0) { return; } $this->handler->handleBatch($this->buffer); $this->clear(); } public function __destruct() { // suppress the parent behavior since we already have register_shutdown_function() // to call close(), and the reference contained there will prevent this from being // GC'd until the end of the request } /** * {@inheritdoc} */ public function close() { $this->flush(); } /** * Clears the buffer without flushing any messages down to the wrapped handler. */ public function clear() { $this->bufferSize = 0; $this->buffer = array(); } public function reset() { $this->flush(); parent::reset(); if ($this->handler instanceof ResettableInterface) { $this->handler->reset(); } } /** * {@inheritdoc} */ public function setFormatter(FormatterInterface $formatter) { $this->handler->setFormatter($formatter); return $this; } /** * {@inheritdoc} */ public function getFormatter() { return $this->handler->getFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\LineFormatter; use Monolog\Logger; /** * Stores to PHP error_log() handler. * * @author Elan Ruusamäe <glen@delfi.ee> */ class ErrorLogHandler extends AbstractProcessingHandler { const OPERATING_SYSTEM = 0; const SAPI = 4; protected $messageType; protected $expandNewlines; /** * @param int $messageType Says where the error should go. * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param bool $expandNewlines If set to true, newlines in the message will be expanded to be take multiple log entries */ public function __construct($messageType = self::OPERATING_SYSTEM, $level = Logger::DEBUG, $bubble = true, $expandNewlines = false) { parent::__construct($level, $bubble); if (false === in_array($messageType, self::getAvailableTypes())) { $message = sprintf('The given message type "%s" is not supported', print_r($messageType, true)); throw new \InvalidArgumentException($message); } $this->messageType = $messageType; $this->expandNewlines = $expandNewlines; } /** * @return array With all available types */ public static function getAvailableTypes() { return array( self::OPERATING_SYSTEM, self::SAPI, ); } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new LineFormatter('[%datetime%] %channel%.%level_name%: %message% %context% %extra%'); } /** * {@inheritdoc} */ protected function write(array $record) { if ($this->expandNewlines) { $lines = preg_split('{[\r\n]+}', (string) $record['formatted']); foreach ($lines as $line) { error_log($line, $this->messageType); } } else { error_log((string) $record['formatted'], $this->messageType); } } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Gelf\IMessagePublisher; use Gelf\PublisherInterface; use Gelf\Publisher; use InvalidArgumentException; use Monolog\Logger; use Monolog\Formatter\GelfMessageFormatter; /** * Handler to send messages to a Graylog2 (http://www.graylog2.org) server * * @author Matt Lehner <mlehner@gmail.com> * @author Benjamin Zikarsky <benjamin@zikarsky.de> */ class GelfHandler extends AbstractProcessingHandler { /** * @var Publisher the publisher object that sends the message to the server */ protected $publisher; /** * @param PublisherInterface|IMessagePublisher|Publisher $publisher a publisher object * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct($publisher, $level = Logger::DEBUG, $bubble = true) { parent::__construct($level, $bubble); if (!$publisher instanceof Publisher && !$publisher instanceof IMessagePublisher && !$publisher instanceof PublisherInterface) { throw new InvalidArgumentException('Invalid publisher, expected a Gelf\Publisher, Gelf\IMessagePublisher or Gelf\PublisherInterface instance'); } $this->publisher = $publisher; } /** * {@inheritdoc} */ protected function write(array $record) { $this->publisher->publish($record['formatted']); } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new GelfMessageFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\NormalizerFormatter; use Monolog\Logger; /** * Handler sending logs to Zend Monitor * * @author Christian Bergau <cbergau86@gmail.com> * @author Jason Davis <happydude@jasondavis.net> */ class ZendMonitorHandler extends AbstractProcessingHandler { /** * Monolog level / ZendMonitor Custom Event priority map * * @var array */ protected $levelMap = array(); /** * Construct * * @param int $level * @param bool $bubble * @throws MissingExtensionException */ public function __construct($level = Logger::DEBUG, $bubble = true) { if (!function_exists('zend_monitor_custom_event')) { throw new MissingExtensionException( 'You must have Zend Server installed with Zend Monitor enabled in order to use this handler' ); } //zend monitor constants are not defined if zend monitor is not enabled. $this->levelMap = array( Logger::DEBUG => \ZEND_MONITOR_EVENT_SEVERITY_INFO, Logger::INFO => \ZEND_MONITOR_EVENT_SEVERITY_INFO, Logger::NOTICE => \ZEND_MONITOR_EVENT_SEVERITY_INFO, Logger::WARNING => \ZEND_MONITOR_EVENT_SEVERITY_WARNING, Logger::ERROR => \ZEND_MONITOR_EVENT_SEVERITY_ERROR, Logger::CRITICAL => \ZEND_MONITOR_EVENT_SEVERITY_ERROR, Logger::ALERT => \ZEND_MONITOR_EVENT_SEVERITY_ERROR, Logger::EMERGENCY => \ZEND_MONITOR_EVENT_SEVERITY_ERROR, ); parent::__construct($level, $bubble); } /** * {@inheritdoc} */ protected function write(array $record) { $this->writeZendMonitorCustomEvent( Logger::getLevelName($record['level']), $record['message'], $record['formatted'], $this->levelMap[$record['level']] ); } /** * Write to Zend Monitor Events * @param string $type Text displayed in "Class Name (custom)" field * @param string $message Text displayed in "Error String" * @param mixed $formatted Displayed in Custom Variables tab * @param int $severity Set the event severity level (-1,0,1) */ protected function writeZendMonitorCustomEvent($type, $message, $formatted, $severity) { zend_monitor_custom_event($type, $message, $formatted, $severity); } /** * {@inheritdoc} */ public function getDefaultFormatter() { return new NormalizerFormatter(); } /** * Get the level map * * @return array */ public function getLevelMap() { return $this->levelMap; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Formatter\FormatterInterface; /** * Simple handler wrapper that filters records based on a list of levels * * It can be configured with an exact list of levels to allow, or a min/max level. * * @author Hennadiy Verkh * @author Jordi Boggiano <j.boggiano@seld.be> */ class FilterHandler extends AbstractHandler { /** * Handler or factory callable($record, $this) * * @var callable|\Monolog\Handler\HandlerInterface */ protected $handler; /** * Minimum level for logs that are passed to handler * * @var int[] */ protected $acceptedLevels; /** * Whether the messages that are handled can bubble up the stack or not * * @var bool */ protected $bubble; /** * @param callable|HandlerInterface $handler Handler or factory callable($record|null, $filterHandler). * @param int|array $minLevelOrList A list of levels to accept or a minimum level if maxLevel is provided * @param int $maxLevel Maximum level to accept, only used if $minLevelOrList is not an array * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct($handler, $minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY, $bubble = true) { $this->handler = $handler; $this->bubble = $bubble; $this->setAcceptedLevels($minLevelOrList, $maxLevel); if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) { throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object"); } } /** * @return array */ public function getAcceptedLevels() { return array_flip($this->acceptedLevels); } /** * @param int|string|array $minLevelOrList A list of levels to accept or a minimum level or level name if maxLevel is provided * @param int|string $maxLevel Maximum level or level name to accept, only used if $minLevelOrList is not an array */ public function setAcceptedLevels($minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY) { if (is_array($minLevelOrList)) { $acceptedLevels = array_map('Monolog\Logger::toMonologLevel', $minLevelOrList); } else { $minLevelOrList = Logger::toMonologLevel($minLevelOrList); $maxLevel = Logger::toMonologLevel($maxLevel); $acceptedLevels = array_values(array_filter(Logger::getLevels(), function ($level) use ($minLevelOrList, $maxLevel) { return $level >= $minLevelOrList && $level <= $maxLevel; })); } $this->acceptedLevels = array_flip($acceptedLevels); } /** * {@inheritdoc} */ public function isHandling(array $record) { return isset($this->acceptedLevels[$record['level']]); } /** * {@inheritdoc} */ public function handle(array $record) { if (!$this->isHandling($record)) { return false; } if ($this->processors) { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } } $this->getHandler($record)->handle($record); return false === $this->bubble; } /** * {@inheritdoc} */ public function handleBatch(array $records) { $filtered = array(); foreach ($records as $record) { if ($this->isHandling($record)) { $filtered[] = $record; } } $this->getHandler($filtered[count($filtered) - 1])->handleBatch($filtered); } /** * Return the nested handler * * If the handler was provided as a factory callable, this will trigger the handler's instantiation. * * @return HandlerInterface */ public function getHandler(array $record = null) { if (!$this->handler instanceof HandlerInterface) { $this->handler = call_user_func($this->handler, $record, $this); if (!$this->handler instanceof HandlerInterface) { throw new \RuntimeException("The factory callable should return a HandlerInterface"); } } return $this->handler; } /** * {@inheritdoc} */ public function setFormatter(FormatterInterface $formatter) { $this->getHandler()->setFormatter($formatter); return $this; } /** * {@inheritdoc} */ public function getFormatter() { return $this->getHandler()->getFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * Stores to any stream resource * * Can be used to store into php://stderr, remote and local files, etc. * * @author Jordi Boggiano <j.boggiano@seld.be> */ class StreamHandler extends AbstractProcessingHandler { protected $stream; protected $url; private $errorMessage; protected $filePermission; protected $useLocking; private $dirCreated; /** * @param resource|string $stream * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write) * @param bool $useLocking Try to lock log file before doing any writes * * @throws \Exception If a missing directory is not buildable * @throws \InvalidArgumentException If stream is not a resource or string */ public function __construct($stream, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false) { parent::__construct($level, $bubble); if (is_resource($stream)) { $this->stream = $stream; } elseif (is_string($stream)) { $this->url = $stream; } else { throw new \InvalidArgumentException('A stream must either be a resource or a string.'); } $this->filePermission = $filePermission; $this->useLocking = $useLocking; } /** * {@inheritdoc} */ public function close() { if ($this->url && is_resource($this->stream)) { fclose($this->stream); } $this->stream = null; $this->dirCreated = null; } /** * Return the currently active stream if it is open * * @return resource|null */ public function getStream() { return $this->stream; } /** * Return the stream URL if it was configured with a URL and not an active resource * * @return string|null */ public function getUrl() { return $this->url; } /** * {@inheritdoc} */ protected function write(array $record) { if (!is_resource($this->stream)) { if (null === $this->url || '' === $this->url) { throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().'); } $this->createDir(); $this->errorMessage = null; set_error_handler(array($this, 'customErrorHandler')); $this->stream = fopen($this->url, 'a'); if ($this->filePermission !== null) { @chmod($this->url, $this->filePermission); } restore_error_handler(); if (!is_resource($this->stream)) { $this->stream = null; throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url)); } } if ($this->useLocking) { // ignoring errors here, there's not much we can do about them flock($this->stream, LOCK_EX); } $this->streamWrite($this->stream, $record); if ($this->useLocking) { flock($this->stream, LOCK_UN); } } /** * Write to stream * @param resource $stream * @param array $record */ protected function streamWrite($stream, array $record) { fwrite($stream, (string) $record['formatted']); } private function customErrorHandler($code, $msg) { $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg); } /** * @param string $stream * * @return null|string */ private function getDirFromStream($stream) { $pos = strpos($stream, '://'); if ($pos === false) { return dirname($stream); } if ('file://' === substr($stream, 0, 7)) { return dirname(substr($stream, 7)); } return; } private function createDir() { // Do not try to create dir if it has already been tried. if ($this->dirCreated) { return; } $dir = $this->getDirFromStream($this->url); if (null !== $dir && !is_dir($dir)) { $this->errorMessage = null; set_error_handler(array($this, 'customErrorHandler')); $status = mkdir($dir, 0777, true); restore_error_handler(); if (false === $status && !is_dir($dir)) { throw new \UnexpectedValueException(sprintf('There is no existing directory at "%s" and its not buildable: '.$this->errorMessage, $dir)); } } $this->dirCreated = true; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * MandrillHandler uses cURL to send the emails to the Mandrill API * * @author Adam Nicholson <adamnicholson10@gmail.com> */ class MandrillHandler extends MailHandler { protected $message; protected $apiKey; /** * @param string $apiKey A valid Mandrill API key * @param callable|\Swift_Message $message An example message for real messages, only the body will be replaced * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct($apiKey, $message, $level = Logger::ERROR, $bubble = true) { parent::__construct($level, $bubble); if (!$message instanceof \Swift_Message && is_callable($message)) { $message = call_user_func($message); } if (!$message instanceof \Swift_Message) { throw new \InvalidArgumentException('You must provide either a Swift_Message instance or a callable returning it'); } $this->message = $message; $this->apiKey = $apiKey; } /** * {@inheritdoc} */ protected function send($content, array $records) { $message = clone $this->message; $message->setBody($content); $message->setDate(time()); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://mandrillapp.com/api/1.0/messages/send-raw.json'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'key' => $this->apiKey, 'raw_message' => (string) $message, 'async' => false, ))); Curl\Util::execute($ch); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\ChromePHPFormatter; use Monolog\Logger; use Monolog\Utils; /** * Handler sending logs to the ChromePHP extension (http://www.chromephp.com/) * * This also works out of the box with Firefox 43+ * * @author Christophe Coevoet <stof@notk.org> */ class ChromePHPHandler extends AbstractProcessingHandler { /** * Version of the extension */ const VERSION = '4.0'; /** * Header name */ const HEADER_NAME = 'X-ChromeLogger-Data'; /** * Regular expression to detect supported browsers (matches any Chrome, or Firefox 43+) */ const USER_AGENT_REGEX = '{\b(?:Chrome/\d+(?:\.\d+)*|HeadlessChrome|Firefox/(?:4[3-9]|[5-9]\d|\d{3,})(?:\.\d)*)\b}'; protected static $initialized = false; /** * Tracks whether we sent too much data * * Chrome limits the headers to 4KB, so when we sent 3KB we stop sending * * @var bool */ protected static $overflowed = false; protected static $json = array( 'version' => self::VERSION, 'columns' => array('label', 'log', 'backtrace', 'type'), 'rows' => array(), ); protected static $sendHeaders = true; /** * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct($level = Logger::DEBUG, $bubble = true) { parent::__construct($level, $bubble); if (!function_exists('json_encode')) { throw new \RuntimeException('PHP\'s json extension is required to use Monolog\'s ChromePHPHandler'); } } /** * {@inheritdoc} */ public function handleBatch(array $records) { $messages = array(); foreach ($records as $record) { if ($record['level'] < $this->level) { continue; } $messages[] = $this->processRecord($record); } if (!empty($messages)) { $messages = $this->getFormatter()->formatBatch($messages); self::$json['rows'] = array_merge(self::$json['rows'], $messages); $this->send(); } } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new ChromePHPFormatter(); } /** * Creates & sends header for a record * * @see sendHeader() * @see send() * @param array $record */ protected function write(array $record) { self::$json['rows'][] = $record['formatted']; $this->send(); } /** * Sends the log header * * @see sendHeader() */ protected function send() { if (self::$overflowed || !self::$sendHeaders) { return; } if (!self::$initialized) { self::$initialized = true; self::$sendHeaders = $this->headersAccepted(); if (!self::$sendHeaders) { return; } self::$json['request_uri'] = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; } $json = Utils::jsonEncode(self::$json, null, true); $data = base64_encode(utf8_encode($json)); if (strlen($data) > 3 * 1024) { self::$overflowed = true; $record = array( 'message' => 'Incomplete logs, chrome header size limit reached', 'context' => array(), 'level' => Logger::WARNING, 'level_name' => Logger::getLevelName(Logger::WARNING), 'channel' => 'monolog', 'datetime' => new \DateTime(), 'extra' => array(), ); self::$json['rows'][count(self::$json['rows']) - 1] = $this->getFormatter()->format($record); $json = Utils::jsonEncode(self::$json, null, true); $data = base64_encode(utf8_encode($json)); } if (trim($data) !== '') { $this->sendHeader(self::HEADER_NAME, $data); } } /** * Send header string to the client * * @param string $header * @param string $content */ protected function sendHeader($header, $content) { if (!headers_sent() && self::$sendHeaders) { header(sprintf('%s: %s', $header, $content)); } } /** * Verifies if the headers are accepted by the current user agent * * @return bool */ protected function headersAccepted() { if (empty($_SERVER['HTTP_USER_AGENT'])) { return false; } return preg_match(self::USER_AGENT_REGEX, $_SERVER['HTTP_USER_AGENT']); } /** * BC getter for the sendHeaders property that has been made static */ public function __get($property) { if ('sendHeaders' !== $property) { throw new \InvalidArgumentException('Undefined property '.$property); } return static::$sendHeaders; } /** * BC setter for the sendHeaders property that has been made static */ public function __set($property, $value) { if ('sendHeaders' !== $property) { throw new \InvalidArgumentException('Undefined property '.$property); } static::$sendHeaders = $value; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\LineFormatter; use Monolog\Logger; /** * Sends logs to Fleep.io using Webhook integrations * * You'll need a Fleep.io account to use this handler. * * @see https://fleep.io/integrations/webhooks/ Fleep Webhooks Documentation * @author Ando Roots <ando@sqroot.eu> */ class FleepHookHandler extends SocketHandler { const FLEEP_HOST = 'fleep.io'; const FLEEP_HOOK_URI = '/hook/'; /** * @var string Webhook token (specifies the conversation where logs are sent) */ protected $token; /** * Construct a new Fleep.io Handler. * * For instructions on how to create a new web hook in your conversations * see https://fleep.io/integrations/webhooks/ * * @param string $token Webhook token * @param bool|int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @throws MissingExtensionException */ public function __construct($token, $level = Logger::DEBUG, $bubble = true) { if (!extension_loaded('openssl')) { throw new MissingExtensionException('The OpenSSL PHP extension is required to use the FleepHookHandler'); } $this->token = $token; $connectionString = 'ssl://' . self::FLEEP_HOST . ':443'; parent::__construct($connectionString, $level, $bubble); } /** * Returns the default formatter to use with this handler * * Overloaded to remove empty context and extra arrays from the end of the log message. * * @return LineFormatter */ protected function getDefaultFormatter() { return new LineFormatter(null, null, true, true); } /** * Handles a log record * * @param array $record */ public function write(array $record) { parent::write($record); $this->closeSocket(); } /** * {@inheritdoc} * * @param array $record * @return string */ protected function generateDataStream($record) { $content = $this->buildContent($record); return $this->buildHeader($content) . $content; } /** * Builds the header of the API Call * * @param string $content * @return string */ private function buildHeader($content) { $header = "POST " . self::FLEEP_HOOK_URI . $this->token . " HTTP/1.1\r\n"; $header .= "Host: " . self::FLEEP_HOST . "\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($content) . "\r\n"; $header .= "\r\n"; return $header; } /** * Builds the body of API call * * @param array $record * @return string */ private function buildContent($record) { $dataArray = array( 'message' => $record['formatted'], ); return http_build_query($dataArray); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * Simple handler wrapper that deduplicates log records across multiple requests * * It also includes the BufferHandler functionality and will buffer * all messages until the end of the request or flush() is called. * * This works by storing all log records' messages above $deduplicationLevel * to the file specified by $deduplicationStore. When further logs come in at the end of the * request (or when flush() is called), all those above $deduplicationLevel are checked * against the existing stored logs. If they match and the timestamps in the stored log is * not older than $time seconds, the new log record is discarded. If no log record is new, the * whole data set is discarded. * * This is mainly useful in combination with Mail handlers or things like Slack or HipChat handlers * that send messages to people, to avoid spamming with the same message over and over in case of * a major component failure like a database server being down which makes all requests fail in the * same way. * * @author Jordi Boggiano <j.boggiano@seld.be> */ class DeduplicationHandler extends BufferHandler { /** * @var string */ protected $deduplicationStore; /** * @var int */ protected $deduplicationLevel; /** * @var int */ protected $time; /** * @var bool */ private $gc = false; /** * @param HandlerInterface $handler Handler. * @param string $deduplicationStore The file/path where the deduplication log should be kept * @param int $deduplicationLevel The minimum logging level for log records to be looked at for deduplication purposes * @param int $time The period (in seconds) during which duplicate entries should be suppressed after a given log is sent through * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct(HandlerInterface $handler, $deduplicationStore = null, $deduplicationLevel = Logger::ERROR, $time = 60, $bubble = true) { parent::__construct($handler, 0, Logger::DEBUG, $bubble, false); $this->deduplicationStore = $deduplicationStore === null ? sys_get_temp_dir() . '/monolog-dedup-' . substr(md5(__FILE__), 0, 20) .'.log' : $deduplicationStore; $this->deduplicationLevel = Logger::toMonologLevel($deduplicationLevel); $this->time = $time; } public function flush() { if ($this->bufferSize === 0) { return; } $passthru = null; foreach ($this->buffer as $record) { if ($record['level'] >= $this->deduplicationLevel) { $passthru = $passthru || !$this->isDuplicate($record); if ($passthru) { $this->appendRecord($record); } } } // default of null is valid as well as if no record matches duplicationLevel we just pass through if ($passthru === true || $passthru === null) { $this->handler->handleBatch($this->buffer); } $this->clear(); if ($this->gc) { $this->collectLogs(); } } private function isDuplicate(array $record) { if (!file_exists($this->deduplicationStore)) { return false; } $store = file($this->deduplicationStore, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if (!is_array($store)) { return false; } $yesterday = time() - 86400; $timestampValidity = $record['datetime']->getTimestamp() - $this->time; $expectedMessage = preg_replace('{[\r\n].*}', '', $record['message']); for ($i = count($store) - 1; $i >= 0; $i--) { list($timestamp, $level, $message) = explode(':', $store[$i], 3); if ($level === $record['level_name'] && $message === $expectedMessage && $timestamp > $timestampValidity) { return true; } if ($timestamp < $yesterday) { $this->gc = true; } } return false; } private function collectLogs() { if (!file_exists($this->deduplicationStore)) { return false; } $handle = fopen($this->deduplicationStore, 'rw+'); flock($handle, LOCK_EX); $validLogs = array(); $timestampValidity = time() - $this->time; while (!feof($handle)) { $log = fgets($handle); if (substr($log, 0, 10) >= $timestampValidity) { $validLogs[] = $log; } } ftruncate($handle, 0); rewind($handle); foreach ($validLogs as $log) { fwrite($handle, $log); } flock($handle, LOCK_UN); fclose($handle); $this->gc = false; } private function appendRecord(array $record) { file_put_contents($this->deduplicationStore, $record['datetime']->getTimestamp() . ':' . $record['level_name'] . ':' . preg_replace('{[\r\n].*}', '', $record['message']) . "\n", FILE_APPEND); } } <?php declare(strict_types=1); /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Processor\ProcessorInterface; /** * Interface to describe loggers that have processors * * This interface is present in monolog 1.x to ease forward compatibility. * * @author Jordi Boggiano <j.boggiano@seld.be> */ interface ProcessableHandlerInterface { /** * Adds a processor in the stack. * * @param ProcessorInterface|callable $callback * @return HandlerInterface self */ public function pushProcessor($callback): HandlerInterface; /** * Removes the processor on top of the stack and returns it. * * @throws \LogicException In case the processor stack is empty * @return callable */ public function popProcessor(): callable; } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\FormatterInterface; /** * Sampling handler * * A sampled event stream can be useful for logging high frequency events in * a production environment where you only need an idea of what is happening * and are not concerned with capturing every occurrence. Since the decision to * handle or not handle a particular event is determined randomly, the * resulting sampled log is not guaranteed to contain 1/N of the events that * occurred in the application, but based on the Law of large numbers, it will * tend to be close to this ratio with a large number of attempts. * * @author Bryan Davis <bd808@wikimedia.org> * @author Kunal Mehta <legoktm@gmail.com> */ class SamplingHandler extends AbstractHandler { /** * @var callable|HandlerInterface $handler */ protected $handler; /** * @var int $factor */ protected $factor; /** * @param callable|HandlerInterface $handler Handler or factory callable($record|null, $samplingHandler). * @param int $factor Sample factor */ public function __construct($handler, $factor) { parent::__construct(); $this->handler = $handler; $this->factor = $factor; if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) { throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object"); } } public function isHandling(array $record) { return $this->getHandler($record)->isHandling($record); } public function handle(array $record) { if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) { if ($this->processors) { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } } $this->getHandler($record)->handle($record); } return false === $this->bubble; } /** * Return the nested handler * * If the handler was provided as a factory callable, this will trigger the handler's instantiation. * * @return HandlerInterface */ public function getHandler(array $record = null) { if (!$this->handler instanceof HandlerInterface) { $this->handler = call_user_func($this->handler, $record, $this); if (!$this->handler instanceof HandlerInterface) { throw new \RuntimeException("The factory callable should return a HandlerInterface"); } } return $this->handler; } /** * {@inheritdoc} */ public function setFormatter(FormatterInterface $formatter) { $this->getHandler()->setFormatter($formatter); return $this; } /** * {@inheritdoc} */ public function getFormatter() { return $this->getHandler()->getFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; /** * Exception can be thrown if an extension for an handler is missing * * @author Christian Bergau <cbergau86@gmail.com> */ class MissingExtensionException extends \Exception { } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\LineFormatter; use Monolog\Logger; /** * Logs to a Redis key using rpush * * usage example: * * $log = new Logger('application'); * $redis = new RedisHandler(new Predis\Client("tcp://localhost:6379"), "logs", "prod"); * $log->pushHandler($redis); * * @author Thomas Tourlourat <thomas@tourlourat.com> */ class RedisHandler extends AbstractProcessingHandler { private $redisClient; private $redisKey; protected $capSize; /** * @param \Predis\Client|\Redis $redis The redis instance * @param string $key The key name to push records to * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param int $capSize Number of entries to limit list size to */ public function __construct($redis, $key, $level = Logger::DEBUG, $bubble = true, $capSize = false) { if (!(($redis instanceof \Predis\Client) || ($redis instanceof \Redis))) { throw new \InvalidArgumentException('Predis\Client or Redis instance required'); } $this->redisClient = $redis; $this->redisKey = $key; $this->capSize = $capSize; parent::__construct($level, $bubble); } /** * {@inheritDoc} */ protected function write(array $record) { if ($this->capSize) { $this->writeCapped($record); } else { $this->redisClient->rpush($this->redisKey, $record["formatted"]); } } /** * Write and cap the collection * Writes the record to the redis list and caps its * * @param array $record associative record array * @return void */ protected function writeCapped(array $record) { if ($this->redisClient instanceof \Redis) { $this->redisClient->multi() ->rpush($this->redisKey, $record["formatted"]) ->ltrim($this->redisKey, -$this->capSize, -1) ->exec(); } else { $redisKey = $this->redisKey; $capSize = $this->capSize; $this->redisClient->transaction(function ($tx) use ($record, $redisKey, $capSize) { $tx->rpush($redisKey, $record["formatted"]); $tx->ltrim($redisKey, -$capSize, -1); }); } } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new LineFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * Stores logs to files that are rotated every day and a limited number of files are kept. * * This rotation is only intended to be used as a workaround. Using logrotate to * handle the rotation is strongly encouraged when you can use it. * * @author Christophe Coevoet <stof@notk.org> * @author Jordi Boggiano <j.boggiano@seld.be> */ class RotatingFileHandler extends StreamHandler { const FILE_PER_DAY = 'Y-m-d'; const FILE_PER_MONTH = 'Y-m'; const FILE_PER_YEAR = 'Y'; protected $filename; protected $maxFiles; protected $mustRotate; protected $nextRotation; protected $filenameFormat; protected $dateFormat; /** * @param string $filename * @param int $maxFiles The maximal amount of files to keep (0 means unlimited) * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write) * @param bool $useLocking Try to lock log file before doing any writes */ public function __construct($filename, $maxFiles = 0, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false) { $this->filename = $filename; $this->maxFiles = (int) $maxFiles; $this->nextRotation = new \DateTime('tomorrow'); $this->filenameFormat = '{filename}-{date}'; $this->dateFormat = 'Y-m-d'; parent::__construct($this->getTimedFilename(), $level, $bubble, $filePermission, $useLocking); } /** * {@inheritdoc} */ public function close() { parent::close(); if (true === $this->mustRotate) { $this->rotate(); } } /** * {@inheritdoc} */ public function reset() { parent::reset(); if (true === $this->mustRotate) { $this->rotate(); } } public function setFilenameFormat($filenameFormat, $dateFormat) { if (!preg_match('{^Y(([/_.-]?m)([/_.-]?d)?)?$}', $dateFormat)) { trigger_error( 'Invalid date format - format must be one of '. 'RotatingFileHandler::FILE_PER_DAY ("Y-m-d"), RotatingFileHandler::FILE_PER_MONTH ("Y-m") '. 'or RotatingFileHandler::FILE_PER_YEAR ("Y"), or you can set one of the '. 'date formats using slashes, underscores and/or dots instead of dashes.', E_USER_DEPRECATED ); } if (substr_count($filenameFormat, '{date}') === 0) { trigger_error( 'Invalid filename format - format should contain at least `{date}`, because otherwise rotating is impossible.', E_USER_DEPRECATED ); } $this->filenameFormat = $filenameFormat; $this->dateFormat = $dateFormat; $this->url = $this->getTimedFilename(); $this->close(); } /** * {@inheritdoc} */ protected function write(array $record) { // on the first record written, if the log is new, we should rotate (once per day) if (null === $this->mustRotate) { $this->mustRotate = !file_exists($this->url); } if ($this->nextRotation < $record['datetime']) { $this->mustRotate = true; $this->close(); } parent::write($record); } /** * Rotates the files. */ protected function rotate() { // update filename $this->url = $this->getTimedFilename(); $this->nextRotation = new \DateTime('tomorrow'); // skip GC of old logs if files are unlimited if (0 === $this->maxFiles) { return; } $logFiles = glob($this->getGlobPattern()); if ($this->maxFiles >= count($logFiles)) { // no files to remove return; } // Sorting the files by name to remove the older ones usort($logFiles, function ($a, $b) { return strcmp($b, $a); }); foreach (array_slice($logFiles, $this->maxFiles) as $file) { if (is_writable($file)) { // suppress errors here as unlink() might fail if two processes // are cleaning up/rotating at the same time set_error_handler(function ($errno, $errstr, $errfile, $errline) {}); unlink($file); restore_error_handler(); } } $this->mustRotate = false; } protected function getTimedFilename() { $fileInfo = pathinfo($this->filename); $timedFilename = str_replace( array('{filename}', '{date}'), array($fileInfo['filename'], date($this->dateFormat)), $fileInfo['dirname'] . '/' . $this->filenameFormat ); if (!empty($fileInfo['extension'])) { $timedFilename .= '.'.$fileInfo['extension']; } return $timedFilename; } protected function getGlobPattern() { $fileInfo = pathinfo($this->filename); $glob = str_replace( array('{filename}', '{date}'), array($fileInfo['filename'], '[0-9][0-9][0-9][0-9]*'), $fileInfo['dirname'] . '/' . $this->filenameFormat ); if (!empty($fileInfo['extension'])) { $glob .= '.'.$fileInfo['extension']; } return $glob; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler\Curl; class Util { private static $retriableErrorCodes = array( CURLE_COULDNT_RESOLVE_HOST, CURLE_COULDNT_CONNECT, CURLE_HTTP_NOT_FOUND, CURLE_READ_ERROR, CURLE_OPERATION_TIMEOUTED, CURLE_HTTP_POST_ERROR, CURLE_SSL_CONNECT_ERROR, ); /** * Executes a CURL request with optional retries and exception on failure * * @param resource $ch curl handler * @throws \RuntimeException */ public static function execute($ch, $retries = 5, $closeAfterDone = true) { while ($retries--) { if (curl_exec($ch) === false) { $curlErrno = curl_errno($ch); if (false === in_array($curlErrno, self::$retriableErrorCodes, true) || !$retries) { $curlError = curl_error($ch); if ($closeAfterDone) { curl_close($ch); } throw new \RuntimeException(sprintf('Curl error (code %s): %s', $curlErrno, $curlError)); } continue; } if ($closeAfterDone) { curl_close($ch); } break; } } } <?php declare(strict_types=1); /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\ResettableInterface; /** * Helper trait for implementing ProcessableInterface * * This trait is present in monolog 1.x to ease forward compatibility. * * @author Jordi Boggiano <j.boggiano@seld.be> */ trait ProcessableHandlerTrait { /** * @var callable[] */ protected $processors = []; /** * {@inheritdoc} * @suppress PhanTypeMismatchReturn */ public function pushProcessor($callback): HandlerInterface { array_unshift($this->processors, $callback); return $this; } /** * {@inheritdoc} */ public function popProcessor(): callable { if (!$this->processors) { throw new \LogicException('You tried to pop from an empty processor stack.'); } return array_shift($this->processors); } /** * Processes a record. */ protected function processRecord(array $record): array { foreach ($this->processors as $processor) { $record = $processor($record); } return $record; } protected function resetProcessors(): void { foreach ($this->processors as $processor) { if ($processor instanceof ResettableInterface) { $processor->reset(); } } } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Utils; use Monolog\Formatter\NormalizerFormatter; /** * Class to record a log on a NewRelic application. * Enabling New Relic High Security mode may prevent capture of useful information. * * This handler requires a NormalizerFormatter to function and expects an array in $record['formatted'] * * @see https://docs.newrelic.com/docs/agents/php-agent * @see https://docs.newrelic.com/docs/accounts-partnerships/accounts/security/high-security */ class NewRelicHandler extends AbstractProcessingHandler { /** * Name of the New Relic application that will receive logs from this handler. * * @var string */ protected $appName; /** * Name of the current transaction * * @var string */ protected $transactionName; /** * Some context and extra data is passed into the handler as arrays of values. Do we send them as is * (useful if we are using the API), or explode them for display on the NewRelic RPM website? * * @var bool */ protected $explodeArrays; /** * {@inheritDoc} * * @param string $appName * @param bool $explodeArrays * @param string $transactionName */ public function __construct( $level = Logger::ERROR, $bubble = true, $appName = null, $explodeArrays = false, $transactionName = null ) { parent::__construct($level, $bubble); $this->appName = $appName; $this->explodeArrays = $explodeArrays; $this->transactionName = $transactionName; } /** * {@inheritDoc} */ protected function write(array $record) { if (!$this->isNewRelicEnabled()) { throw new MissingExtensionException('The newrelic PHP extension is required to use the NewRelicHandler'); } if ($appName = $this->getAppName($record['context'])) { $this->setNewRelicAppName($appName); } if ($transactionName = $this->getTransactionName($record['context'])) { $this->setNewRelicTransactionName($transactionName); unset($record['formatted']['context']['transaction_name']); } if (isset($record['context']['exception']) && ($record['context']['exception'] instanceof \Exception || (PHP_VERSION_ID >= 70000 && $record['context']['exception'] instanceof \Throwable))) { newrelic_notice_error($record['message'], $record['context']['exception']); unset($record['formatted']['context']['exception']); } else { newrelic_notice_error($record['message']); } if (isset($record['formatted']['context']) && is_array($record['formatted']['context'])) { foreach ($record['formatted']['context'] as $key => $parameter) { if (is_array($parameter) && $this->explodeArrays) { foreach ($parameter as $paramKey => $paramValue) { $this->setNewRelicParameter('context_' . $key . '_' . $paramKey, $paramValue); } } else { $this->setNewRelicParameter('context_' . $key, $parameter); } } } if (isset($record['formatted']['extra']) && is_array($record['formatted']['extra'])) { foreach ($record['formatted']['extra'] as $key => $parameter) { if (is_array($parameter) && $this->explodeArrays) { foreach ($parameter as $paramKey => $paramValue) { $this->setNewRelicParameter('extra_' . $key . '_' . $paramKey, $paramValue); } } else { $this->setNewRelicParameter('extra_' . $key, $parameter); } } } } /** * Checks whether the NewRelic extension is enabled in the system. * * @return bool */ protected function isNewRelicEnabled() { return extension_loaded('newrelic'); } /** * Returns the appname where this log should be sent. Each log can override the default appname, set in this * handler's constructor, by providing the appname in it's context. * * @param array $context * @return null|string */ protected function getAppName(array $context) { if (isset($context['appname'])) { return $context['appname']; } return $this->appName; } /** * Returns the name of the current transaction. Each log can override the default transaction name, set in this * handler's constructor, by providing the transaction_name in it's context * * @param array $context * * @return null|string */ protected function getTransactionName(array $context) { if (isset($context['transaction_name'])) { return $context['transaction_name']; } return $this->transactionName; } /** * Sets the NewRelic application that should receive this log. * * @param string $appName */ protected function setNewRelicAppName($appName) { newrelic_set_appname($appName); } /** * Overwrites the name of the current transaction * * @param string $transactionName */ protected function setNewRelicTransactionName($transactionName) { newrelic_name_transaction($transactionName); } /** * @param string $key * @param mixed $value */ protected function setNewRelicParameter($key, $value) { if (null === $value || is_scalar($value)) { newrelic_add_custom_parameter($key, $value); } else { newrelic_add_custom_parameter($key, Utils::jsonEncode($value, null, true)); } } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new NormalizerFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\LineFormatter; /** * Handler sending logs to browser's javascript console with no browser extension required * * @author Olivier Poitrey <rs@dailymotion.com> */ class BrowserConsoleHandler extends AbstractProcessingHandler { protected static $initialized = false; protected static $records = array(); /** * {@inheritDoc} * * Formatted output may contain some formatting markers to be transferred to `console.log` using the %c format. * * Example of formatted string: * * You can do [[blue text]]{color: blue} or [[green background]]{background-color: green; color: white} */ protected function getDefaultFormatter() { return new LineFormatter('[[%channel%]]{macro: autolabel} [[%level_name%]]{font-weight: bold} %message%'); } /** * {@inheritDoc} */ protected function write(array $record) { // Accumulate records static::$records[] = $record; // Register shutdown handler if not already done if (!static::$initialized) { static::$initialized = true; $this->registerShutdownFunction(); } } /** * Convert records to javascript console commands and send it to the browser. * This method is automatically called on PHP shutdown if output is HTML or Javascript. */ public static function send() { $format = static::getResponseFormat(); if ($format === 'unknown') { return; } if (count(static::$records)) { if ($format === 'html') { static::writeOutput('<script>' . static::generateScript() . '</script>'); } elseif ($format === 'js') { static::writeOutput(static::generateScript()); } static::resetStatic(); } } public function close() { self::resetStatic(); } public function reset() { self::resetStatic(); } /** * Forget all logged records */ public static function resetStatic() { static::$records = array(); } /** * Wrapper for register_shutdown_function to allow overriding */ protected function registerShutdownFunction() { if (PHP_SAPI !== 'cli') { register_shutdown_function(array('Monolog\Handler\BrowserConsoleHandler', 'send')); } } /** * Wrapper for echo to allow overriding * * @param string $str */ protected static function writeOutput($str) { echo $str; } /** * Checks the format of the response * * If Content-Type is set to application/javascript or text/javascript -> js * If Content-Type is set to text/html, or is unset -> html * If Content-Type is anything else -> unknown * * @return string One of 'js', 'html' or 'unknown' */ protected static function getResponseFormat() { // Check content type foreach (headers_list() as $header) { if (stripos($header, 'content-type:') === 0) { // This handler only works with HTML and javascript outputs // text/javascript is obsolete in favour of application/javascript, but still used if (stripos($header, 'application/javascript') !== false || stripos($header, 'text/javascript') !== false) { return 'js'; } if (stripos($header, 'text/html') === false) { return 'unknown'; } break; } } return 'html'; } private static function generateScript() { $script = array(); foreach (static::$records as $record) { $context = static::dump('Context', $record['context']); $extra = static::dump('Extra', $record['extra']); if (empty($context) && empty($extra)) { $script[] = static::call_array('log', static::handleStyles($record['formatted'])); } else { $script = array_merge($script, array(static::call_array('groupCollapsed', static::handleStyles($record['formatted']))), $context, $extra, array(static::call('groupEnd')) ); } } return "(function (c) {if (c && c.groupCollapsed) {\n" . implode("\n", $script) . "\n}})(console);"; } private static function handleStyles($formatted) { $args = array(); $format = '%c' . $formatted; preg_match_all('/\[\[(.*?)\]\]\{([^}]*)\}/s', $format, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); foreach (array_reverse($matches) as $match) { $args[] = '"font-weight: normal"'; $args[] = static::quote(static::handleCustomStyles($match[2][0], $match[1][0])); $pos = $match[0][1]; $format = substr($format, 0, $pos) . '%c' . $match[1][0] . '%c' . substr($format, $pos + strlen($match[0][0])); } $args[] = static::quote('font-weight: normal'); $args[] = static::quote($format); return array_reverse($args); } private static function handleCustomStyles($style, $string) { static $colors = array('blue', 'green', 'red', 'magenta', 'orange', 'black', 'grey'); static $labels = array(); return preg_replace_callback('/macro\s*:(.*?)(?:;|$)/', function ($m) use ($string, &$colors, &$labels) { if (trim($m[1]) === 'autolabel') { // Format the string as a label with consistent auto assigned background color if (!isset($labels[$string])) { $labels[$string] = $colors[count($labels) % count($colors)]; } $color = $labels[$string]; return "background-color: $color; color: white; border-radius: 3px; padding: 0 2px 0 2px"; } return $m[1]; }, $style); } private static function dump($title, array $dict) { $script = array(); $dict = array_filter($dict); if (empty($dict)) { return $script; } $script[] = static::call('log', static::quote('%c%s'), static::quote('font-weight: bold'), static::quote($title)); foreach ($dict as $key => $value) { $value = json_encode($value); if (empty($value)) { $value = static::quote(''); } $script[] = static::call('log', static::quote('%s: %o'), static::quote($key), $value); } return $script; } private static function quote($arg) { return '"' . addcslashes($arg, "\"\n\\") . '"'; } private static function call() { $args = func_get_args(); $method = array_shift($args); return static::call_array($method, $args); } private static function call_array($method, array $args) { return 'c.' . $method . '(' . implode(', ', $args) . ');'; } } <?php declare(strict_types=1); /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\FormatterInterface; /** * Interface to describe loggers that have a formatter * * This interface is present in monolog 1.x to ease forward compatibility. * * @author Jordi Boggiano <j.boggiano@seld.be> */ interface FormattableHandlerInterface { /** * Sets the formatter. * * @param FormatterInterface $formatter * @return HandlerInterface self */ public function setFormatter(FormatterInterface $formatter): HandlerInterface; /** * Gets the formatter. * * @return FormatterInterface */ public function getFormatter(): FormatterInterface; } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Formatter\LineFormatter; /** * NativeMailerHandler uses the mail() function to send the emails * * @author Christophe Coevoet <stof@notk.org> * @author Mark Garrett <mark@moderndeveloperllc.com> */ class NativeMailerHandler extends MailHandler { /** * The email addresses to which the message will be sent * @var array */ protected $to; /** * The subject of the email * @var string */ protected $subject; /** * Optional headers for the message * @var array */ protected $headers = array(); /** * Optional parameters for the message * @var array */ protected $parameters = array(); /** * The wordwrap length for the message * @var int */ protected $maxColumnWidth; /** * The Content-type for the message * @var string */ protected $contentType = 'text/plain'; /** * The encoding for the message * @var string */ protected $encoding = 'utf-8'; /** * @param string|array $to The receiver of the mail * @param string $subject The subject of the mail * @param string $from The sender of the mail * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param int $maxColumnWidth The maximum column width that the message lines will have */ public function __construct($to, $subject, $from, $level = Logger::ERROR, $bubble = true, $maxColumnWidth = 70) { parent::__construct($level, $bubble); $this->to = is_array($to) ? $to : array($to); $this->subject = $subject; $this->addHeader(sprintf('From: %s', $from)); $this->maxColumnWidth = $maxColumnWidth; } /** * Add headers to the message * * @param string|array $headers Custom added headers * @return self */ public function addHeader($headers) { foreach ((array) $headers as $header) { if (strpos($header, "\n") !== false || strpos($header, "\r") !== false) { throw new \InvalidArgumentException('Headers can not contain newline characters for security reasons'); } $this->headers[] = $header; } return $this; } /** * Add parameters to the message * * @param string|array $parameters Custom added parameters * @return self */ public function addParameter($parameters) { $this->parameters = array_merge($this->parameters, (array) $parameters); return $this; } /** * {@inheritdoc} */ protected function send($content, array $records) { $content = wordwrap($content, $this->maxColumnWidth); $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n"); $headers .= 'Content-type: ' . $this->getContentType() . '; charset=' . $this->getEncoding() . "\r\n"; if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) { $headers .= 'MIME-Version: 1.0' . "\r\n"; } $subject = $this->subject; if ($records) { $subjectFormatter = new LineFormatter($this->subject); $subject = $subjectFormatter->format($this->getHighestRecord($records)); } $parameters = implode(' ', $this->parameters); foreach ($this->to as $to) { mail($to, $subject, $content, $headers, $parameters); } } /** * @return string $contentType */ public function getContentType() { return $this->contentType; } /** * @return string $encoding */ public function getEncoding() { return $this->encoding; } /** * @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML * messages. * @return self */ public function setContentType($contentType) { if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) { throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection'); } $this->contentType = $contentType; return $this; } /** * @param string $encoding * @return self */ public function setEncoding($encoding) { if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) { throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection'); } $this->encoding = $encoding; return $this; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Utils; /** * Logs to Cube. * * @link http://square.github.com/cube/ * @author Wan Chen <kami@kamisama.me> */ class CubeHandler extends AbstractProcessingHandler { private $udpConnection; private $httpConnection; private $scheme; private $host; private $port; private $acceptedSchemes = array('http', 'udp'); /** * Create a Cube handler * * @throws \UnexpectedValueException when given url is not a valid url. * A valid url must consist of three parts : protocol://host:port * Only valid protocols used by Cube are http and udp */ public function __construct($url, $level = Logger::DEBUG, $bubble = true) { $urlInfo = parse_url($url); if (!isset($urlInfo['scheme'], $urlInfo['host'], $urlInfo['port'])) { throw new \UnexpectedValueException('URL "'.$url.'" is not valid'); } if (!in_array($urlInfo['scheme'], $this->acceptedSchemes)) { throw new \UnexpectedValueException( 'Invalid protocol (' . $urlInfo['scheme'] . ').' . ' Valid options are ' . implode(', ', $this->acceptedSchemes)); } $this->scheme = $urlInfo['scheme']; $this->host = $urlInfo['host']; $this->port = $urlInfo['port']; parent::__construct($level, $bubble); } /** * Establish a connection to an UDP socket * * @throws \LogicException when unable to connect to the socket * @throws MissingExtensionException when there is no socket extension */ protected function connectUdp() { if (!extension_loaded('sockets')) { throw new MissingExtensionException('The sockets extension is required to use udp URLs with the CubeHandler'); } $this->udpConnection = socket_create(AF_INET, SOCK_DGRAM, 0); if (!$this->udpConnection) { throw new \LogicException('Unable to create a socket'); } if (!socket_connect($this->udpConnection, $this->host, $this->port)) { throw new \LogicException('Unable to connect to the socket at ' . $this->host . ':' . $this->port); } } /** * Establish a connection to a http server * @throws \LogicException when no curl extension */ protected function connectHttp() { if (!extension_loaded('curl')) { throw new \LogicException('The curl extension is needed to use http URLs with the CubeHandler'); } $this->httpConnection = curl_init('http://'.$this->host.':'.$this->port.'/1.0/event/put'); if (!$this->httpConnection) { throw new \LogicException('Unable to connect to ' . $this->host . ':' . $this->port); } curl_setopt($this->httpConnection, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($this->httpConnection, CURLOPT_RETURNTRANSFER, true); } /** * {@inheritdoc} */ protected function write(array $record) { $date = $record['datetime']; $data = array('time' => $date->format('Y-m-d\TH:i:s.uO')); unset($record['datetime']); if (isset($record['context']['type'])) { $data['type'] = $record['context']['type']; unset($record['context']['type']); } else { $data['type'] = $record['channel']; } $data['data'] = $record['context']; $data['data']['level'] = $record['level']; if ($this->scheme === 'http') { $this->writeHttp(Utils::jsonEncode($data)); } else { $this->writeUdp(Utils::jsonEncode($data)); } } private function writeUdp($data) { if (!$this->udpConnection) { $this->connectUdp(); } socket_send($this->udpConnection, $data, strlen($data), 0); } private function writeHttp($data) { if (!$this->httpConnection) { $this->connectHttp(); } curl_setopt($this->httpConnection, CURLOPT_POSTFIELDS, '['.$data.']'); curl_setopt($this->httpConnection, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen('['.$data.']'), )); Curl\Util::execute($this->httpConnection, 5, false); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\FormatterInterface; use Monolog\Formatter\ElasticaFormatter; use Monolog\Logger; use Elastica\Client; use Elastica\Exception\ExceptionInterface; /** * Elastic Search handler * * Usage example: * * $client = new \Elastica\Client(); * $options = array( * 'index' => 'elastic_index_name', * 'type' => 'elastic_doc_type', * ); * $handler = new ElasticSearchHandler($client, $options); * $log = new Logger('application'); * $log->pushHandler($handler); * * @author Jelle Vink <jelle.vink@gmail.com> */ class ElasticSearchHandler extends AbstractProcessingHandler { /** * @var Client */ protected $client; /** * @var array Handler config options */ protected $options = array(); /** * @param Client $client Elastica Client object * @param array $options Handler configuration * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct(Client $client, array $options = array(), $level = Logger::DEBUG, $bubble = true) { parent::__construct($level, $bubble); $this->client = $client; $this->options = array_merge( array( 'index' => 'monolog', // Elastic index name 'type' => 'record', // Elastic document type 'ignore_error' => false, // Suppress Elastica exceptions ), $options ); } /** * {@inheritDoc} */ protected function write(array $record) { $this->bulkSend(array($record['formatted'])); } /** * {@inheritdoc} */ public function setFormatter(FormatterInterface $formatter) { if ($formatter instanceof ElasticaFormatter) { return parent::setFormatter($formatter); } throw new \InvalidArgumentException('ElasticSearchHandler is only compatible with ElasticaFormatter'); } /** * Getter options * @return array */ public function getOptions() { return $this->options; } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new ElasticaFormatter($this->options['index'], $this->options['type']); } /** * {@inheritdoc} */ public function handleBatch(array $records) { $documents = $this->getFormatter()->formatBatch($records); $this->bulkSend($documents); } /** * Use Elasticsearch bulk API to send list of documents * @param array $documents * @throws \RuntimeException */ protected function bulkSend(array $documents) { try { $this->client->addDocuments($documents); } catch (ExceptionInterface $e) { if (!$this->options['ignore_error']) { throw new \RuntimeException("Error sending messages to Elasticsearch", 0, $e); } } } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * Sends notifications through the pushover api to mobile phones * * @author Sebastian Göttschkes <sebastian.goettschkes@googlemail.com> * @see https://www.pushover.net/api */ class PushoverHandler extends SocketHandler { private $token; private $users; private $title; private $user; private $retry; private $expire; private $highPriorityLevel; private $emergencyLevel; private $useFormattedMessage = false; /** * All parameters that can be sent to Pushover * @see https://pushover.net/api * @var array */ private $parameterNames = array( 'token' => true, 'user' => true, 'message' => true, 'device' => true, 'title' => true, 'url' => true, 'url_title' => true, 'priority' => true, 'timestamp' => true, 'sound' => true, 'retry' => true, 'expire' => true, 'callback' => true, ); /** * Sounds the api supports by default * @see https://pushover.net/api#sounds * @var array */ private $sounds = array( 'pushover', 'bike', 'bugle', 'cashregister', 'classical', 'cosmic', 'falling', 'gamelan', 'incoming', 'intermission', 'magic', 'mechanical', 'pianobar', 'siren', 'spacealarm', 'tugboat', 'alien', 'climb', 'persistent', 'echo', 'updown', 'none', ); /** * @param string $token Pushover api token * @param string|array $users Pushover user id or array of ids the message will be sent to * @param string $title Title sent to the Pushover API * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param bool $useSSL Whether to connect via SSL. Required when pushing messages to users that are not * the pushover.net app owner. OpenSSL is required for this option. * @param int $highPriorityLevel The minimum logging level at which this handler will start * sending "high priority" requests to the Pushover API * @param int $emergencyLevel The minimum logging level at which this handler will start * sending "emergency" requests to the Pushover API * @param int $retry The retry parameter specifies how often (in seconds) the Pushover servers will send the same notification to the user. * @param int $expire The expire parameter specifies how many seconds your notification will continue to be retried for (every retry seconds). */ public function __construct($token, $users, $title = null, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $highPriorityLevel = Logger::CRITICAL, $emergencyLevel = Logger::EMERGENCY, $retry = 30, $expire = 25200) { $connectionString = $useSSL ? 'ssl://api.pushover.net:443' : 'api.pushover.net:80'; parent::__construct($connectionString, $level, $bubble); $this->token = $token; $this->users = (array) $users; $this->title = $title ?: gethostname(); $this->highPriorityLevel = Logger::toMonologLevel($highPriorityLevel); $this->emergencyLevel = Logger::toMonologLevel($emergencyLevel); $this->retry = $retry; $this->expire = $expire; } protected function generateDataStream($record) { $content = $this->buildContent($record); return $this->buildHeader($content) . $content; } private function buildContent($record) { // Pushover has a limit of 512 characters on title and message combined. $maxMessageLength = 512 - strlen($this->title); $message = ($this->useFormattedMessage) ? $record['formatted'] : $record['message']; $message = substr($message, 0, $maxMessageLength); $timestamp = $record['datetime']->getTimestamp(); $dataArray = array( 'token' => $this->token, 'user' => $this->user, 'message' => $message, 'title' => $this->title, 'timestamp' => $timestamp, ); if (isset($record['level']) && $record['level'] >= $this->emergencyLevel) { $dataArray['priority'] = 2; $dataArray['retry'] = $this->retry; $dataArray['expire'] = $this->expire; } elseif (isset($record['level']) && $record['level'] >= $this->highPriorityLevel) { $dataArray['priority'] = 1; } // First determine the available parameters $context = array_intersect_key($record['context'], $this->parameterNames); $extra = array_intersect_key($record['extra'], $this->parameterNames); // Least important info should be merged with subsequent info $dataArray = array_merge($extra, $context, $dataArray); // Only pass sounds that are supported by the API if (isset($dataArray['sound']) && !in_array($dataArray['sound'], $this->sounds)) { unset($dataArray['sound']); } return http_build_query($dataArray); } private function buildHeader($content) { $header = "POST /1/messages.json HTTP/1.1\r\n"; $header .= "Host: api.pushover.net\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($content) . "\r\n"; $header .= "\r\n"; return $header; } protected function write(array $record) { foreach ($this->users as $user) { $this->user = $user; parent::write($record); $this->closeSocket(); } $this->user = null; } public function setHighPriorityLevel($value) { $this->highPriorityLevel = $value; } public function setEmergencyLevel($value) { $this->emergencyLevel = $value; } /** * Use the formatted message? * @param bool $value */ public function useFormattedMessage($value) { $this->useFormattedMessage = (bool) $value; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Formatter\NormalizerFormatter; use Doctrine\CouchDB\CouchDBClient; /** * CouchDB handler for Doctrine CouchDB ODM * * @author Markus Bachmann <markus.bachmann@bachi.biz> */ class DoctrineCouchDBHandler extends AbstractProcessingHandler { private $client; public function __construct(CouchDBClient $client, $level = Logger::DEBUG, $bubble = true) { $this->client = $client; parent::__construct($level, $bubble); } /** * {@inheritDoc} */ protected function write(array $record) { $this->client->postDocument($record['formatted']); } protected function getDefaultFormatter() { return new NormalizerFormatter; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\FormatterInterface; /** * Interface that all Monolog Handlers must implement * * @author Jordi Boggiano <j.boggiano@seld.be> */ interface HandlerInterface { /** * Checks whether the given record will be handled by this handler. * * This is mostly done for performance reasons, to avoid calling processors for nothing. * * Handlers should still check the record levels within handle(), returning false in isHandling() * is no guarantee that handle() will not be called, and isHandling() might not be called * for a given record. * * @param array $record Partial log record containing only a level key * * @return bool */ public function isHandling(array $record); /** * Handles a record. * * All records may be passed to this method, and the handler should discard * those that it does not want to handle. * * The return value of this function controls the bubbling process of the handler stack. * Unless the bubbling is interrupted (by returning true), the Logger class will keep on * calling further handlers in the stack with a given log record. * * @param array $record The record to handle * @return bool true means that this handler handled the record, and that bubbling is not permitted. * false means the record was either not processed or that this handler allows bubbling. */ public function handle(array $record); /** * Handles a set of records at once. * * @param array $records The records to handle (an array of record arrays) */ public function handleBatch(array $records); /** * Adds a processor in the stack. * * @param callable $callback * @return self */ public function pushProcessor($callback); /** * Removes the processor on top of the stack and returns it. * * @return callable */ public function popProcessor(); /** * Sets the formatter. * * @param FormatterInterface $formatter * @return self */ public function setFormatter(FormatterInterface $formatter); /** * Gets the formatter. * * @return FormatterInterface */ public function getFormatter(); } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\FormatterInterface; use Monolog\ResettableInterface; /** * Forwards records to multiple handlers * * @author Lenar Lõhmus <lenar@city.ee> */ class GroupHandler extends AbstractHandler { protected $handlers; /** * @param array $handlers Array of Handlers. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct(array $handlers, $bubble = true) { foreach ($handlers as $handler) { if (!$handler instanceof HandlerInterface) { throw new \InvalidArgumentException('The first argument of the GroupHandler must be an array of HandlerInterface instances.'); } } $this->handlers = $handlers; $this->bubble = $bubble; } /** * {@inheritdoc} */ public function isHandling(array $record) { foreach ($this->handlers as $handler) { if ($handler->isHandling($record)) { return true; } } return false; } /** * {@inheritdoc} */ public function handle(array $record) { if ($this->processors) { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } } foreach ($this->handlers as $handler) { $handler->handle($record); } return false === $this->bubble; } /** * {@inheritdoc} */ public function handleBatch(array $records) { if ($this->processors) { $processed = array(); foreach ($records as $record) { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } $processed[] = $record; } $records = $processed; } foreach ($this->handlers as $handler) { $handler->handleBatch($records); } } public function reset() { parent::reset(); foreach ($this->handlers as $handler) { if ($handler instanceof ResettableInterface) { $handler->reset(); } } } /** * {@inheritdoc} */ public function setFormatter(FormatterInterface $formatter) { foreach ($this->handlers as $handler) { $handler->setFormatter($formatter); } return $this; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Utils; use Monolog\Formatter\FlowdockFormatter; use Monolog\Formatter\FormatterInterface; /** * Sends notifications through the Flowdock push API * * This must be configured with a FlowdockFormatter instance via setFormatter() * * Notes: * API token - Flowdock API token * * @author Dominik Liebler <liebler.dominik@gmail.com> * @see https://www.flowdock.com/api/push */ class FlowdockHandler extends SocketHandler { /** * @var string */ protected $apiToken; /** * @param string $apiToken * @param bool|int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * * @throws MissingExtensionException if OpenSSL is missing */ public function __construct($apiToken, $level = Logger::DEBUG, $bubble = true) { if (!extension_loaded('openssl')) { throw new MissingExtensionException('The OpenSSL PHP extension is required to use the FlowdockHandler'); } parent::__construct('ssl://api.flowdock.com:443', $level, $bubble); $this->apiToken = $apiToken; } /** * {@inheritdoc} */ public function setFormatter(FormatterInterface $formatter) { if (!$formatter instanceof FlowdockFormatter) { throw new \InvalidArgumentException('The FlowdockHandler requires an instance of Monolog\Formatter\FlowdockFormatter to function correctly'); } return parent::setFormatter($formatter); } /** * Gets the default formatter. * * @return FormatterInterface */ protected function getDefaultFormatter() { throw new \InvalidArgumentException('The FlowdockHandler must be configured (via setFormatter) with an instance of Monolog\Formatter\FlowdockFormatter to function correctly'); } /** * {@inheritdoc} * * @param array $record */ protected function write(array $record) { parent::write($record); $this->closeSocket(); } /** * {@inheritdoc} * * @param array $record * @return string */ protected function generateDataStream($record) { $content = $this->buildContent($record); return $this->buildHeader($content) . $content; } /** * Builds the body of API call * * @param array $record * @return string */ private function buildContent($record) { return Utils::jsonEncode($record['formatted']['flowdock']); } /** * Builds the header of the API Call * * @param string $content * @return string */ private function buildHeader($content) { $header = "POST /v1/messages/team_inbox/" . $this->apiToken . " HTTP/1.1\r\n"; $header .= "Host: api.flowdock.com\r\n"; $header .= "Content-Type: application/json\r\n"; $header .= "Content-Length: " . strlen($content) . "\r\n"; $header .= "\r\n"; return $header; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Formatter\LogglyFormatter; /** * Sends errors to Loggly. * * @author Przemek Sobstel <przemek@sobstel.org> * @author Adam Pancutt <adam@pancutt.com> * @author Gregory Barchard <gregory@barchard.net> */ class LogglyHandler extends AbstractProcessingHandler { const HOST = 'logs-01.loggly.com'; const ENDPOINT_SINGLE = 'inputs'; const ENDPOINT_BATCH = 'bulk'; protected $token; protected $tag = array(); public function __construct($token, $level = Logger::DEBUG, $bubble = true) { if (!extension_loaded('curl')) { throw new \LogicException('The curl extension is needed to use the LogglyHandler'); } $this->token = $token; parent::__construct($level, $bubble); } public function setTag($tag) { $tag = !empty($tag) ? $tag : array(); $this->tag = is_array($tag) ? $tag : array($tag); } public function addTag($tag) { if (!empty($tag)) { $tag = is_array($tag) ? $tag : array($tag); $this->tag = array_unique(array_merge($this->tag, $tag)); } } protected function write(array $record) { $this->send($record["formatted"], self::ENDPOINT_SINGLE); } public function handleBatch(array $records) { $level = $this->level; $records = array_filter($records, function ($record) use ($level) { return ($record['level'] >= $level); }); if ($records) { $this->send($this->getFormatter()->formatBatch($records), self::ENDPOINT_BATCH); } } protected function send($data, $endpoint) { $url = sprintf("https://%s/%s/%s/", self::HOST, $endpoint, $this->token); $headers = array('Content-Type: application/json'); if (!empty($this->tag)) { $headers[] = 'X-LOGGLY-TAG: '.implode(',', $this->tag); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); Curl\Util::execute($ch); } protected function getDefaultFormatter() { return new LogglyFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; use Monolog\Handler\FingersCrossed\ActivationStrategyInterface; use Monolog\Logger; use Monolog\ResettableInterface; use Monolog\Formatter\FormatterInterface; /** * Buffers all records until a certain level is reached * * The advantage of this approach is that you don't get any clutter in your log files. * Only requests which actually trigger an error (or whatever your actionLevel is) will be * in the logs, but they will contain all records, not only those above the level threshold. * * You can find the various activation strategies in the * Monolog\Handler\FingersCrossed\ namespace. * * @author Jordi Boggiano <j.boggiano@seld.be> */ class FingersCrossedHandler extends AbstractHandler { protected $handler; protected $activationStrategy; protected $buffering = true; protected $bufferSize; protected $buffer = array(); protected $stopBuffering; protected $passthruLevel; /** * @param callable|HandlerInterface $handler Handler or factory callable($record|null, $fingersCrossedHandler). * @param int|ActivationStrategyInterface $activationStrategy Strategy which determines when this handler takes action * @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param bool $stopBuffering Whether the handler should stop buffering after being triggered (default true) * @param int $passthruLevel Minimum level to always flush to handler on close, even if strategy not triggered */ public function __construct($handler, $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true, $passthruLevel = null) { if (null === $activationStrategy) { $activationStrategy = new ErrorLevelActivationStrategy(Logger::WARNING); } // convert simple int activationStrategy to an object if (!$activationStrategy instanceof ActivationStrategyInterface) { $activationStrategy = new ErrorLevelActivationStrategy($activationStrategy); } $this->handler = $handler; $this->activationStrategy = $activationStrategy; $this->bufferSize = $bufferSize; $this->bubble = $bubble; $this->stopBuffering = $stopBuffering; if ($passthruLevel !== null) { $this->passthruLevel = Logger::toMonologLevel($passthruLevel); } if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) { throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object"); } } /** * {@inheritdoc} */ public function isHandling(array $record) { return true; } /** * Manually activate this logger regardless of the activation strategy */ public function activate() { if ($this->stopBuffering) { $this->buffering = false; } $this->getHandler(end($this->buffer) ?: null)->handleBatch($this->buffer); $this->buffer = array(); } /** * {@inheritdoc} */ public function handle(array $record) { if ($this->processors) { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } } if ($this->buffering) { $this->buffer[] = $record; if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) { array_shift($this->buffer); } if ($this->activationStrategy->isHandlerActivated($record)) { $this->activate(); } } else { $this->getHandler($record)->handle($record); } return false === $this->bubble; } /** * {@inheritdoc} */ public function close() { $this->flushBuffer(); } public function reset() { $this->flushBuffer(); parent::reset(); if ($this->getHandler() instanceof ResettableInterface) { $this->getHandler()->reset(); } } /** * Clears the buffer without flushing any messages down to the wrapped handler. * * It also resets the handler to its initial buffering state. */ public function clear() { $this->buffer = array(); $this->reset(); } /** * Resets the state of the handler. Stops forwarding records to the wrapped handler. */ private function flushBuffer() { if (null !== $this->passthruLevel) { $level = $this->passthruLevel; $this->buffer = array_filter($this->buffer, function ($record) use ($level) { return $record['level'] >= $level; }); if (count($this->buffer) > 0) { $this->getHandler(end($this->buffer) ?: null)->handleBatch($this->buffer); } } $this->buffer = array(); $this->buffering = true; } /** * Return the nested handler * * If the handler was provided as a factory callable, this will trigger the handler's instantiation. * * @return HandlerInterface */ public function getHandler(array $record = null) { if (!$this->handler instanceof HandlerInterface) { $this->handler = call_user_func($this->handler, $record, $this); if (!$this->handler instanceof HandlerInterface) { throw new \RuntimeException("The factory callable should return a HandlerInterface"); } } return $this->handler; } /** * {@inheritdoc} */ public function setFormatter(FormatterInterface $formatter) { $this->getHandler()->setFormatter($formatter); return $this; } /** * {@inheritdoc} */ public function getFormatter() { return $this->getHandler()->getFormatter(); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * Stores to any socket - uses fsockopen() or pfsockopen(). * * @author Pablo de Leon Belloc <pablolb@gmail.com> * @see http://php.net/manual/en/function.fsockopen.php */ class SocketHandler extends AbstractProcessingHandler { private $connectionString; private $connectionTimeout; private $resource; private $timeout = 0; private $writingTimeout = 10; private $lastSentBytes = null; private $chunkSize = null; private $persistent = false; private $errno; private $errstr; private $lastWritingAt; /** * @param string $connectionString Socket connection string * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct($connectionString, $level = Logger::DEBUG, $bubble = true) { parent::__construct($level, $bubble); $this->connectionString = $connectionString; $this->connectionTimeout = (float) ini_get('default_socket_timeout'); } /** * Connect (if necessary) and write to the socket * * @param array $record * * @throws \UnexpectedValueException * @throws \RuntimeException */ protected function write(array $record) { $this->connectIfNotConnected(); $data = $this->generateDataStream($record); $this->writeToSocket($data); } /** * We will not close a PersistentSocket instance so it can be reused in other requests. */ public function close() { if (!$this->isPersistent()) { $this->closeSocket(); } } /** * Close socket, if open */ public function closeSocket() { if (is_resource($this->resource)) { fclose($this->resource); $this->resource = null; } } /** * Set socket connection to nbe persistent. It only has effect before the connection is initiated. * * @param bool $persistent */ public function setPersistent($persistent) { $this->persistent = (bool) $persistent; } /** * Set connection timeout. Only has effect before we connect. * * @param float $seconds * * @see http://php.net/manual/en/function.fsockopen.php */ public function setConnectionTimeout($seconds) { $this->validateTimeout($seconds); $this->connectionTimeout = (float) $seconds; } /** * Set write timeout. Only has effect before we connect. * * @param float $seconds * * @see http://php.net/manual/en/function.stream-set-timeout.php */ public function setTimeout($seconds) { $this->validateTimeout($seconds); $this->timeout = (float) $seconds; } /** * Set writing timeout. Only has effect during connection in the writing cycle. * * @param float $seconds 0 for no timeout */ public function setWritingTimeout($seconds) { $this->validateTimeout($seconds); $this->writingTimeout = (float) $seconds; } /** * Set chunk size. Only has effect during connection in the writing cycle. * * @param float $bytes */ public function setChunkSize($bytes) { $this->chunkSize = $bytes; } /** * Get current connection string * * @return string */ public function getConnectionString() { return $this->connectionString; } /** * Get persistent setting * * @return bool */ public function isPersistent() { return $this->persistent; } /** * Get current connection timeout setting * * @return float */ public function getConnectionTimeout() { return $this->connectionTimeout; } /** * Get current in-transfer timeout * * @return float */ public function getTimeout() { return $this->timeout; } /** * Get current local writing timeout * * @return float */ public function getWritingTimeout() { return $this->writingTimeout; } /** * Get current chunk size * * @return float */ public function getChunkSize() { return $this->chunkSize; } /** * Check to see if the socket is currently available. * * UDP might appear to be connected but might fail when writing. See http://php.net/fsockopen for details. * * @return bool */ public function isConnected() { return is_resource($this->resource) && !feof($this->resource); // on TCP - other party can close connection. } /** * Wrapper to allow mocking */ protected function pfsockopen() { return @pfsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout); } /** * Wrapper to allow mocking */ protected function fsockopen() { return @fsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout); } /** * Wrapper to allow mocking * * @see http://php.net/manual/en/function.stream-set-timeout.php */ protected function streamSetTimeout() { $seconds = floor($this->timeout); $microseconds = round(($this->timeout - $seconds) * 1e6); return stream_set_timeout($this->resource, $seconds, $microseconds); } /** * Wrapper to allow mocking * * @see http://php.net/manual/en/function.stream-set-chunk-size.php */ protected function streamSetChunkSize() { return stream_set_chunk_size($this->resource, $this->chunkSize); } /** * Wrapper to allow mocking */ protected function fwrite($data) { return @fwrite($this->resource, $data); } /** * Wrapper to allow mocking */ protected function streamGetMetadata() { return stream_get_meta_data($this->resource); } private function validateTimeout($value) { $ok = filter_var($value, FILTER_VALIDATE_FLOAT); if ($ok === false || $value < 0) { throw new \InvalidArgumentException("Timeout must be 0 or a positive float (got $value)"); } } private function connectIfNotConnected() { if ($this->isConnected()) { return; } $this->connect(); } protected function generateDataStream($record) { return (string) $record['formatted']; } /** * @return resource|null */ protected function getResource() { return $this->resource; } private function connect() { $this->createSocketResource(); $this->setSocketTimeout(); $this->setStreamChunkSize(); } private function createSocketResource() { if ($this->isPersistent()) { $resource = $this->pfsockopen(); } else { $resource = $this->fsockopen(); } if (!$resource) { throw new \UnexpectedValueException("Failed connecting to $this->connectionString ($this->errno: $this->errstr)"); } $this->resource = $resource; } private function setSocketTimeout() { if (!$this->streamSetTimeout()) { throw new \UnexpectedValueException("Failed setting timeout with stream_set_timeout()"); } } private function setStreamChunkSize() { if ($this->chunkSize && !$this->streamSetChunkSize()) { throw new \UnexpectedValueException("Failed setting chunk size with stream_set_chunk_size()"); } } private function writeToSocket($data) { $length = strlen($data); $sent = 0; $this->lastSentBytes = $sent; while ($this->isConnected() && $sent < $length) { if (0 == $sent) { $chunk = $this->fwrite($data); } else { $chunk = $this->fwrite(substr($data, $sent)); } if ($chunk === false) { throw new \RuntimeException("Could not write to socket"); } $sent += $chunk; $socketInfo = $this->streamGetMetadata(); if ($socketInfo['timed_out']) { throw new \RuntimeException("Write timed-out"); } if ($this->writingIsTimedOut($sent)) { throw new \RuntimeException("Write timed-out, no data sent for `{$this->writingTimeout}` seconds, probably we got disconnected (sent $sent of $length)"); } } if (!$this->isConnected() && $sent < $length) { throw new \RuntimeException("End-of-file reached, probably we got disconnected (sent $sent of $length)"); } } private function writingIsTimedOut($sent) { $writingTimeout = (int) floor($this->writingTimeout); if (0 === $writingTimeout) { return false; } if ($sent !== $this->lastSentBytes) { $this->lastWritingAt = time(); $this->lastSentBytes = $sent; return false; } else { usleep(100); } if ((time() - $this->lastWritingAt) >= $writingTimeout) { $this->closeSocket(); return true; } return false; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Aws\Sdk; use Aws\DynamoDb\DynamoDbClient; use Aws\DynamoDb\Marshaler; use Monolog\Formatter\ScalarFormatter; use Monolog\Logger; /** * Amazon DynamoDB handler (http://aws.amazon.com/dynamodb/) * * @link https://github.com/aws/aws-sdk-php/ * @author Andrew Lawson <adlawson@gmail.com> */ class DynamoDbHandler extends AbstractProcessingHandler { const DATE_FORMAT = 'Y-m-d\TH:i:s.uO'; /** * @var DynamoDbClient */ protected $client; /** * @var string */ protected $table; /** * @var int */ protected $version; /** * @var Marshaler */ protected $marshaler; /** * @param DynamoDbClient $client * @param string $table * @param int $level * @param bool $bubble */ public function __construct(DynamoDbClient $client, $table, $level = Logger::DEBUG, $bubble = true) { if (defined('Aws\Sdk::VERSION') && version_compare(Sdk::VERSION, '3.0', '>=')) { $this->version = 3; $this->marshaler = new Marshaler; } else { $this->version = 2; } $this->client = $client; $this->table = $table; parent::__construct($level, $bubble); } /** * {@inheritdoc} */ protected function write(array $record) { $filtered = $this->filterEmptyFields($record['formatted']); if ($this->version === 3) { $formatted = $this->marshaler->marshalItem($filtered); } else { $formatted = $this->client->formatAttributes($filtered); } $this->client->putItem(array( 'TableName' => $this->table, 'Item' => $formatted, )); } /** * @param array $record * @return array */ protected function filterEmptyFields(array $record) { return array_filter($record, function ($value) { return !empty($value) || false === $value || 0 === $value; }); } /** * {@inheritdoc} */ protected function getDefaultFormatter() { return new ScalarFormatter(self::DATE_FORMAT); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\ResettableInterface; use Monolog\Formatter\FormatterInterface; /** * This simple wrapper class can be used to extend handlers functionality. * * Example: A custom filtering that can be applied to any handler. * * Inherit from this class and override handle() like this: * * public function handle(array $record) * { * if ($record meets certain conditions) { * return false; * } * return $this->handler->handle($record); * } * * @author Alexey Karapetov <alexey@karapetov.com> */ class HandlerWrapper implements HandlerInterface, ResettableInterface { /** * @var HandlerInterface */ protected $handler; /** * HandlerWrapper constructor. * @param HandlerInterface $handler */ public function __construct(HandlerInterface $handler) { $this->handler = $handler; } /** * {@inheritdoc} */ public function isHandling(array $record) { return $this->handler->isHandling($record); } /** * {@inheritdoc} */ public function handle(array $record) { return $this->handler->handle($record); } /** * {@inheritdoc} */ public function handleBatch(array $records) { return $this->handler->handleBatch($records); } /** * {@inheritdoc} */ public function pushProcessor($callback) { $this->handler->pushProcessor($callback); return $this; } /** * {@inheritdoc} */ public function popProcessor() { return $this->handler->popProcessor(); } /** * {@inheritdoc} */ public function setFormatter(FormatterInterface $formatter) { $this->handler->setFormatter($formatter); return $this; } /** * {@inheritdoc} */ public function getFormatter() { return $this->handler->getFormatter(); } public function reset() { if ($this->handler instanceof ResettableInterface) { return $this->handler->reset(); } } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; /** * Base class for all mail handlers * * @author Gyula Sallai */ abstract class MailHandler extends AbstractProcessingHandler { /** * {@inheritdoc} */ public function handleBatch(array $records) { $messages = array(); foreach ($records as $record) { if ($record['level'] < $this->level) { continue; } $messages[] = $this->processRecord($record); } if (!empty($messages)) { $this->send((string) $this->getFormatter()->formatBatch($messages), $messages); } } /** * Send a mail with the given content * * @param string $content formatted email body to be sent * @param array $records the array of log records that formed this content */ abstract protected function send($content, array $records); /** * {@inheritdoc} */ protected function write(array $record) { $this->send((string) $record['formatted'], array($record)); } protected function getHighestRecord(array $records) { $highestRecord = null; foreach ($records as $record) { if ($highestRecord === null || $highestRecord['level'] < $record['level']) { $highestRecord = $record; } } return $highestRecord; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; /** * Sends notifications through the hipchat api to a hipchat room * * Notes: * API token - HipChat API token * Room - HipChat Room Id or name, where messages are sent * Name - Name used to send the message (from) * notify - Should the message trigger a notification in the clients * version - The API version to use (HipChatHandler::API_V1 | HipChatHandler::API_V2) * * @author Rafael Dohms <rafael@doh.ms> * @see https://www.hipchat.com/docs/api */ class HipChatHandler extends SocketHandler { /** * Use API version 1 */ const API_V1 = 'v1'; /** * Use API version v2 */ const API_V2 = 'v2'; /** * The maximum allowed length for the name used in the "from" field. */ const MAXIMUM_NAME_LENGTH = 15; /** * The maximum allowed length for the message. */ const MAXIMUM_MESSAGE_LENGTH = 9500; /** * @var string */ private $token; /** * @var string */ private $room; /** * @var string */ private $name; /** * @var bool */ private $notify; /** * @var string */ private $format; /** * @var string */ private $host; /** * @var string */ private $version; /** * @param string $token HipChat API Token * @param string $room The room that should be alerted of the message (Id or Name) * @param string $name Name used in the "from" field. * @param bool $notify Trigger a notification in clients or not * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param bool $useSSL Whether to connect via SSL. * @param string $format The format of the messages (default to text, can be set to html if you have html in the messages) * @param string $host The HipChat server hostname. * @param string $version The HipChat API version (default HipChatHandler::API_V1) */ public function __construct($token, $room, $name = 'Monolog', $notify = false, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $format = 'text', $host = 'api.hipchat.com', $version = self::API_V1) { @trigger_error('The Monolog\Handler\HipChatHandler class is deprecated. You should migrate to Slack and the SlackWebhookHandler / SlackbotHandler, see https://www.atlassian.com/partnerships/slack', E_USER_DEPRECATED); if ($version == self::API_V1 && !$this->validateStringLength($name, static::MAXIMUM_NAME_LENGTH)) { throw new \InvalidArgumentException('The supplied name is too long. HipChat\'s v1 API supports names up to 15 UTF-8 characters.'); } $connectionString = $useSSL ? 'ssl://'.$host.':443' : $host.':80'; parent::__construct($connectionString, $level, $bubble); $this->token = $token; $this->name = $name; $this->notify = $notify; $this->room = $room; $this->format = $format; $this->host = $host; $this->version = $version; } /** * {@inheritdoc} * * @param array $record * @return string */ protected function generateDataStream($record) { $content = $this->buildContent($record); return $this->buildHeader($content) . $content; } /** * Builds the body of API call * * @param array $record * @return string */ private function buildContent($record) { $dataArray = array( 'notify' => $this->version == self::API_V1 ? ($this->notify ? 1 : 0) : ($this->notify ? 'true' : 'false'), 'message' => $record['formatted'], 'message_format' => $this->format, 'color' => $this->getAlertColor($record['level']), ); if (!$this->validateStringLength($dataArray['message'], static::MAXIMUM_MESSAGE_LENGTH)) { if (function_exists('mb_substr')) { $dataArray['message'] = mb_substr($dataArray['message'], 0, static::MAXIMUM_MESSAGE_LENGTH).' [truncated]'; } else { $dataArray['message'] = substr($dataArray['message'], 0, static::MAXIMUM_MESSAGE_LENGTH).' [truncated]'; } } // if we are using the legacy API then we need to send some additional information if ($this->version == self::API_V1) { $dataArray['room_id'] = $this->room; } // append the sender name if it is set // always append it if we use the v1 api (it is required in v1) if ($this->version == self::API_V1 || $this->name !== null) { $dataArray['from'] = (string) $this->name; } return http_build_query($dataArray); } /** * Builds the header of the API Call * * @param string $content * @return string */ private function buildHeader($content) { if ($this->version == self::API_V1) { $header = "POST /v1/rooms/message?format=json&auth_token={$this->token} HTTP/1.1\r\n"; } else { // needed for rooms with special (spaces, etc) characters in the name $room = rawurlencode($this->room); $header = "POST /v2/room/{$room}/notification?auth_token={$this->token} HTTP/1.1\r\n"; } $header .= "Host: {$this->host}\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($content) . "\r\n"; $header .= "\r\n"; return $header; } /** * Assigns a color to each level of log records. * * @param int $level * @return string */ protected function getAlertColor($level) { switch (true) { case $level >= Logger::ERROR: return 'red'; case $level >= Logger::WARNING: return 'yellow'; case $level >= Logger::INFO: return 'green'; case $level == Logger::DEBUG: return 'gray'; default: return 'yellow'; } } /** * {@inheritdoc} * * @param array $record */ protected function write(array $record) { parent::write($record); $this->finalizeWrite(); } /** * Finalizes the request by reading some bytes and then closing the socket * * If we do not read some but close the socket too early, hipchat sometimes * drops the request entirely. */ protected function finalizeWrite() { $res = $this->getResource(); if (is_resource($res)) { @fread($res, 2048); } $this->closeSocket(); } /** * {@inheritdoc} */ public function handleBatch(array $records) { if (count($records) == 0) { return true; } $batchRecords = $this->combineRecords($records); $handled = false; foreach ($batchRecords as $batchRecord) { if ($this->isHandling($batchRecord)) { $this->write($batchRecord); $handled = true; } } if (!$handled) { return false; } return false === $this->bubble; } /** * Combines multiple records into one. Error level of the combined record * will be the highest level from the given records. Datetime will be taken * from the first record. * * @param $records * @return array */ private function combineRecords($records) { $batchRecord = null; $batchRecords = array(); $messages = array(); $formattedMessages = array(); $level = 0; $levelName = null; $datetime = null; foreach ($records as $record) { $record = $this->processRecord($record); if ($record['level'] > $level) { $level = $record['level']; $levelName = $record['level_name']; } if (null === $datetime) { $datetime = $record['datetime']; } $messages[] = $record['message']; $messageStr = implode(PHP_EOL, $messages); $formattedMessages[] = $this->getFormatter()->format($record); $formattedMessageStr = implode('', $formattedMessages); $batchRecord = array( 'message' => $messageStr, 'formatted' => $formattedMessageStr, 'context' => array(), 'extra' => array(), ); if (!$this->validateStringLength($batchRecord['formatted'], static::MAXIMUM_MESSAGE_LENGTH)) { // Pop the last message and implode the remaining messages $lastMessage = array_pop($messages); $lastFormattedMessage = array_pop($formattedMessages); $batchRecord['message'] = implode(PHP_EOL, $messages); $batchRecord['formatted'] = implode('', $formattedMessages); $batchRecords[] = $batchRecord; $messages = array($lastMessage); $formattedMessages = array($lastFormattedMessage); $batchRecord = null; } } if (null !== $batchRecord) { $batchRecords[] = $batchRecord; } // Set the max level and datetime for all records foreach ($batchRecords as &$batchRecord) { $batchRecord = array_merge( $batchRecord, array( 'level' => $level, 'level_name' => $levelName, 'datetime' => $datetime, ) ); } return $batchRecords; } /** * Validates the length of a string. * * If the `mb_strlen()` function is available, it will use that, as HipChat * allows UTF-8 characters. Otherwise, it will fall back to `strlen()`. * * Note that this might cause false failures in the specific case of using * a valid name with less than 16 characters, but 16 or more bytes, on a * system where `mb_strlen()` is unavailable. * * @param string $str * @param int $length * * @return bool */ private function validateStringLength($str, $length) { if (function_exists('mb_strlen')) { return (mb_strlen($str) <= $length); } return (strlen($str) <= $length); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Utils; /** * IFTTTHandler uses cURL to trigger IFTTT Maker actions * * Register a secret key and trigger/event name at https://ifttt.com/maker * * value1 will be the channel from monolog's Logger constructor, * value2 will be the level name (ERROR, WARNING, ..) * value3 will be the log record's message * * @author Nehal Patel <nehal@nehalpatel.me> */ class IFTTTHandler extends AbstractProcessingHandler { private $eventName; private $secretKey; /** * @param string $eventName The name of the IFTTT Maker event that should be triggered * @param string $secretKey A valid IFTTT secret key * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct($eventName, $secretKey, $level = Logger::ERROR, $bubble = true) { $this->eventName = $eventName; $this->secretKey = $secretKey; parent::__construct($level, $bubble); } /** * {@inheritdoc} */ public function write(array $record) { $postData = array( "value1" => $record["channel"], "value2" => $record["level_name"], "value3" => $record["message"], ); $postString = Utils::jsonEncode($postData); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://maker.ifttt.com/trigger/" . $this->eventName . "/with/key/" . $this->secretKey); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postString); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type: application/json", )); Curl\Util::execute($ch); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\JsonFormatter; use Monolog\Logger; /** * CouchDB handler * * @author Markus Bachmann <markus.bachmann@bachi.biz> */ class CouchDBHandler extends AbstractProcessingHandler { private $options; public function __construct(array $options = array(), $level = Logger::DEBUG, $bubble = true) { $this->options = array_merge(array( 'host' => 'localhost', 'port' => 5984, 'dbname' => 'logger', 'username' => null, 'password' => null, ), $options); parent::__construct($level, $bubble); } /** * {@inheritDoc} */ protected function write(array $record) { $basicAuth = null; if ($this->options['username']) { $basicAuth = sprintf('%s:%s@', $this->options['username'], $this->options['password']); } $url = 'http://'.$basicAuth.$this->options['host'].':'.$this->options['port'].'/'.$this->options['dbname']; $context = stream_context_create(array( 'http' => array( 'method' => 'POST', 'content' => $record['formatted'], 'ignore_errors' => true, 'max_redirects' => 0, 'header' => 'Content-type: application/json', ), )); if (false === @file_get_contents($url, null, $context)) { throw new \RuntimeException(sprintf('Could not connect to %s', $url)); } } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Exception; use Monolog\Formatter\LineFormatter; use Monolog\Logger; use Monolog\Utils; use PhpConsole\Connector; use PhpConsole\Handler; use PhpConsole\Helper; /** * Monolog handler for Google Chrome extension "PHP Console" * * Display PHP error/debug log messages in Google Chrome console and notification popups, executes PHP code remotely * * Usage: * 1. Install Google Chrome extension https://chrome.google.com/webstore/detail/php-console/nfhmhhlpfleoednkpnnnkolmclajemef * 2. See overview https://github.com/barbushin/php-console#overview * 3. Install PHP Console library https://github.com/barbushin/php-console#installation * 4. Example (result will looks like http://i.hizliresim.com/vg3Pz4.png) * * $logger = new \Monolog\Logger('all', array(new \Monolog\Handler\PHPConsoleHandler())); * \Monolog\ErrorHandler::register($logger); * echo $undefinedVar; * $logger->addDebug('SELECT * FROM users', array('db', 'time' => 0.012)); * PC::debug($_SERVER); // PHP Console debugger for any type of vars * * @author Sergey Barbushin https://www.linkedin.com/in/barbushin */ class PHPConsoleHandler extends AbstractProcessingHandler { private $options = array( 'enabled' => true, // bool Is PHP Console server enabled 'classesPartialsTraceIgnore' => array('Monolog\\'), // array Hide calls of classes started with... 'debugTagsKeysInContext' => array(0, 'tag'), // bool Is PHP Console server enabled 'useOwnErrorsHandler' => false, // bool Enable errors handling 'useOwnExceptionsHandler' => false, // bool Enable exceptions handling 'sourcesBasePath' => null, // string Base path of all project sources to strip in errors source paths 'registerHelper' => true, // bool Register PhpConsole\Helper that allows short debug calls like PC::debug($var, 'ta.g.s') 'serverEncoding' => null, // string|null Server internal encoding 'headersLimit' => null, // int|null Set headers size limit for your web-server 'password' => null, // string|null Protect PHP Console connection by password 'enableSslOnlyMode' => false, // bool Force connection by SSL for clients with PHP Console installed 'ipMasks' => array(), // array Set IP masks of clients that will be allowed to connect to PHP Console: array('192.168.*.*', '127.0.0.1') 'enableEvalListener' => false, // bool Enable eval request to be handled by eval dispatcher(if enabled, 'password' option is also required) 'dumperDetectCallbacks' => false, // bool Convert callback items in dumper vars to (callback SomeClass::someMethod) strings 'dumperLevelLimit' => 5, // int Maximum dumped vars array or object nested dump level 'dumperItemsCountLimit' => 100, // int Maximum dumped var same level array items or object properties number 'dumperItemSizeLimit' => 5000, // int Maximum length of any string or dumped array item 'dumperDumpSizeLimit' => 500000, // int Maximum approximate size of dumped vars result formatted in JSON 'detectDumpTraceAndSource' => false, // bool Autodetect and append trace data to debug 'dataStorage' => null, // PhpConsole\Storage|null Fixes problem with custom $_SESSION handler(see http://goo.gl/Ne8juJ) ); /** @var Connector */ private $connector; /** * @param array $options See \Monolog\Handler\PHPConsoleHandler::$options for more details * @param Connector|null $connector Instance of \PhpConsole\Connector class (optional) * @param int $level * @param bool $bubble * @throws Exception */ public function __construct(array $options = array(), Connector $connector = null, $level = Logger::DEBUG, $bubble = true) { if (!class_exists('PhpConsole\Connector')) { throw new Exception('PHP Console library not found. See https://github.com/barbushin/php-console#installation'); } parent::__construct($level, $bubble); $this->options = $this->initOptions($options); $this->connector = $this->initConnector($connector); } private function initOptions(array $options) { $wrongOptions = array_diff(array_keys($options), array_keys($this->options)); if ($wrongOptions) { throw new Exception('Unknown options: ' . implode(', ', $wrongOptions)); } return array_replace($this->options, $options); } private function initConnector(Connector $connector = null) { if (!$connector) { if ($this->options['dataStorage']) { Connector::setPostponeStorage($this->options['dataStorage']); } $connector = Connector::getInstance(); } if ($this->options['registerHelper'] && !Helper::isRegistered()) { Helper::register(); } if ($this->options['enabled'] && $connector->isActiveClient()) { if ($this->options['useOwnErrorsHandler'] || $this->options['useOwnExceptionsHandler']) { $handler = Handler::getInstance(); $handler->setHandleErrors($this->options['useOwnErrorsHandler']); $handler->setHandleExceptions($this->options['useOwnExceptionsHandler']); $handler->start(); } if ($this->options['sourcesBasePath']) { $connector->setSourcesBasePath($this->options['sourcesBasePath']); } if ($this->options['serverEncoding']) { $connector->setServerEncoding($this->options['serverEncoding']); } if ($this->options['password']) { $connector->setPassword($this->options['password']); } if ($this->options['enableSslOnlyMode']) { $connector->enableSslOnlyMode(); } if ($this->options['ipMasks']) { $connector->setAllowedIpMasks($this->options['ipMasks']); } if ($this->options['headersLimit']) { $connector->setHeadersLimit($this->options['headersLimit']); } if ($this->options['detectDumpTraceAndSource']) { $connector->getDebugDispatcher()->detectTraceAndSource = true; } $dumper = $connector->getDumper(); $dumper->levelLimit = $this->options['dumperLevelLimit']; $dumper->itemsCountLimit = $this->options['dumperItemsCountLimit']; $dumper->itemSizeLimit = $this->options['dumperItemSizeLimit']; $dumper->dumpSizeLimit = $this->options['dumperDumpSizeLimit']; $dumper->detectCallbacks = $this->options['dumperDetectCallbacks']; if ($this->options['enableEvalListener']) { $connector->startEvalRequestsListener(); } } return $connector; } public function getConnector() { return $this->connector; } public function getOptions() { return $this->options; } public function handle(array $record) { if ($this->options['enabled'] && $this->connector->isActiveClient()) { return parent::handle($record); } return !$this->bubble; } /** * Writes the record down to the log of the implementing handler * * @param array $record * @return void */ protected function write(array $record) { if ($record['level'] < Logger::NOTICE) { $this->handleDebugRecord($record); } elseif (isset($record['context']['exception']) && $record['context']['exception'] instanceof Exception) { $this->handleExceptionRecord($record); } else { $this->handleErrorRecord($record); } } private function handleDebugRecord(array $record) { $tags = $this->getRecordTags($record); $message = $record['message']; if ($record['context']) { $message .= ' ' . Utils::jsonEncode($this->connector->getDumper()->dump(array_filter($record['context'])), null, true); } $this->connector->getDebugDispatcher()->dispatchDebug($message, $tags, $this->options['classesPartialsTraceIgnore']); } private function handleExceptionRecord(array $record) { $this->connector->getErrorsDispatcher()->dispatchException($record['context']['exception']); } private function handleErrorRecord(array $record) { $context = $record['context']; $this->connector->getErrorsDispatcher()->dispatchError( isset($context['code']) ? $context['code'] : null, isset($context['message']) ? $context['message'] : $record['message'], isset($context['file']) ? $context['file'] : null, isset($context['line']) ? $context['line'] : null, $this->options['classesPartialsTraceIgnore'] ); } private function getRecordTags(array &$record) { $tags = null; if (!empty($record['context'])) { $context = & $record['context']; foreach ($this->options['debugTagsKeysInContext'] as $key) { if (!empty($context[$key])) { $tags = $context[$key]; if ($key === 0) { array_shift($context); } else { unset($context[$key]); } break; } } } return $tags ?: strtolower($record['level_name']); } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new LineFormatter('%message%'); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Formatter\JsonFormatter; use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Channel\AMQPChannel; use AMQPExchange; class AmqpHandler extends AbstractProcessingHandler { /** * @var AMQPExchange|AMQPChannel $exchange */ protected $exchange; /** * @var string */ protected $exchangeName; /** * @param AMQPExchange|AMQPChannel $exchange AMQPExchange (php AMQP ext) or PHP AMQP lib channel, ready for use * @param string $exchangeName * @param int $level * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct($exchange, $exchangeName = 'log', $level = Logger::DEBUG, $bubble = true) { if ($exchange instanceof AMQPExchange) { $exchange->setName($exchangeName); } elseif ($exchange instanceof AMQPChannel) { $this->exchangeName = $exchangeName; } else { throw new \InvalidArgumentException('PhpAmqpLib\Channel\AMQPChannel or AMQPExchange instance required'); } $this->exchange = $exchange; parent::__construct($level, $bubble); } /** * {@inheritDoc} */ protected function write(array $record) { $data = $record["formatted"]; $routingKey = $this->getRoutingKey($record); if ($this->exchange instanceof AMQPExchange) { $this->exchange->publish( $data, $routingKey, 0, array( 'delivery_mode' => 2, 'content_type' => 'application/json', ) ); } else { $this->exchange->basic_publish( $this->createAmqpMessage($data), $this->exchangeName, $routingKey ); } } /** * {@inheritDoc} */ public function handleBatch(array $records) { if ($this->exchange instanceof AMQPExchange) { parent::handleBatch($records); return; } foreach ($records as $record) { if (!$this->isHandling($record)) { continue; } $record = $this->processRecord($record); $data = $this->getFormatter()->format($record); $this->exchange->batch_basic_publish( $this->createAmqpMessage($data), $this->exchangeName, $this->getRoutingKey($record) ); } $this->exchange->publish_batch(); } /** * Gets the routing key for the AMQP exchange * * @param array $record * @return string */ protected function getRoutingKey(array $record) { $routingKey = sprintf( '%s.%s', // TODO 2.0 remove substr call substr($record['level_name'], 0, 4), $record['channel'] ); return strtolower($routingKey); } /** * @param string $data * @return AMQPMessage */ private function createAmqpMessage($data) { return new AMQPMessage( (string) $data, array( 'delivery_mode' => 2, 'content_type' => 'application/json', ) ); } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; /** * Used for testing purposes. * * It records all records and gives you access to them for verification. * * @author Jordi Boggiano <j.boggiano@seld.be> * * @method bool hasEmergency($record) * @method bool hasAlert($record) * @method bool hasCritical($record) * @method bool hasError($record) * @method bool hasWarning($record) * @method bool hasNotice($record) * @method bool hasInfo($record) * @method bool hasDebug($record) * * @method bool hasEmergencyRecords() * @method bool hasAlertRecords() * @method bool hasCriticalRecords() * @method bool hasErrorRecords() * @method bool hasWarningRecords() * @method bool hasNoticeRecords() * @method bool hasInfoRecords() * @method bool hasDebugRecords() * * @method bool hasEmergencyThatContains($message) * @method bool hasAlertThatContains($message) * @method bool hasCriticalThatContains($message) * @method bool hasErrorThatContains($message) * @method bool hasWarningThatContains($message) * @method bool hasNoticeThatContains($message) * @method bool hasInfoThatContains($message) * @method bool hasDebugThatContains($message) * * @method bool hasEmergencyThatMatches($message) * @method bool hasAlertThatMatches($message) * @method bool hasCriticalThatMatches($message) * @method bool hasErrorThatMatches($message) * @method bool hasWarningThatMatches($message) * @method bool hasNoticeThatMatches($message) * @method bool hasInfoThatMatches($message) * @method bool hasDebugThatMatches($message) * * @method bool hasEmergencyThatPasses($message) * @method bool hasAlertThatPasses($message) * @method bool hasCriticalThatPasses($message) * @method bool hasErrorThatPasses($message) * @method bool hasWarningThatPasses($message) * @method bool hasNoticeThatPasses($message) * @method bool hasInfoThatPasses($message) * @method bool hasDebugThatPasses($message) */ class TestHandler extends AbstractProcessingHandler { protected $records = array(); protected $recordsByLevel = array(); private $skipReset = false; public function getRecords() { return $this->records; } public function clear() { $this->records = array(); $this->recordsByLevel = array(); } public function reset() { if (!$this->skipReset) { $this->clear(); } } public function setSkipReset($skipReset) { $this->skipReset = $skipReset; } public function hasRecords($level) { return isset($this->recordsByLevel[$level]); } /** * @param string|array $record Either a message string or an array containing message and optionally context keys that will be checked against all records * @param int $level Logger::LEVEL constant value */ public function hasRecord($record, $level) { if (is_string($record)) { $record = array('message' => $record); } return $this->hasRecordThatPasses(function ($rec) use ($record) { if ($rec['message'] !== $record['message']) { return false; } if (isset($record['context']) && $rec['context'] !== $record['context']) { return false; } return true; }, $level); } public function hasRecordThatContains($message, $level) { return $this->hasRecordThatPasses(function ($rec) use ($message) { return strpos($rec['message'], $message) !== false; }, $level); } public function hasRecordThatMatches($regex, $level) { return $this->hasRecordThatPasses(function ($rec) use ($regex) { return preg_match($regex, $rec['message']) > 0; }, $level); } public function hasRecordThatPasses($predicate, $level) { if (!is_callable($predicate)) { throw new \InvalidArgumentException("Expected a callable for hasRecordThatSucceeds"); } if (!isset($this->recordsByLevel[$level])) { return false; } foreach ($this->recordsByLevel[$level] as $i => $rec) { if (call_user_func($predicate, $rec, $i)) { return true; } } return false; } /** * {@inheritdoc} */ protected function write(array $record) { $this->recordsByLevel[$record['level']][] = $record; $this->records[] = $record; } public function __call($method, $args) { if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) { $genericMethod = $matches[1] . ('Records' !== $matches[3] ? 'Record' : '') . $matches[3]; $level = constant('Monolog\Logger::' . strtoupper($matches[2])); if (method_exists($this, $genericMethod)) { $args[] = $level; return call_user_func_array(array($this, $genericMethod), $args); } } throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()'); } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\WildfireFormatter; /** * Simple FirePHP Handler (http://www.firephp.org/), which uses the Wildfire protocol. * * @author Eric Clemmons (@ericclemmons) <eric@uxdriven.com> */ class FirePHPHandler extends AbstractProcessingHandler { /** * WildFire JSON header message format */ const PROTOCOL_URI = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2'; /** * FirePHP structure for parsing messages & their presentation */ const STRUCTURE_URI = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1'; /** * Must reference a "known" plugin, otherwise headers won't display in FirePHP */ const PLUGIN_URI = 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3'; /** * Header prefix for Wildfire to recognize & parse headers */ const HEADER_PREFIX = 'X-Wf'; /** * Whether or not Wildfire vendor-specific headers have been generated & sent yet */ protected static $initialized = false; /** * Shared static message index between potentially multiple handlers * @var int */ protected static $messageIndex = 1; protected static $sendHeaders = true; /** * Base header creation function used by init headers & record headers * * @param array $meta Wildfire Plugin, Protocol & Structure Indexes * @param string $message Log message * @return array Complete header string ready for the client as key and message as value */ protected function createHeader(array $meta, $message) { $header = sprintf('%s-%s', self::HEADER_PREFIX, join('-', $meta)); return array($header => $message); } /** * Creates message header from record * * @see createHeader() * @param array $record * @return string */ protected function createRecordHeader(array $record) { // Wildfire is extensible to support multiple protocols & plugins in a single request, // but we're not taking advantage of that (yet), so we're using "1" for simplicity's sake. return $this->createHeader( array(1, 1, 1, self::$messageIndex++), $record['formatted'] ); } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new WildfireFormatter(); } /** * Wildfire initialization headers to enable message parsing * * @see createHeader() * @see sendHeader() * @return array */ protected function getInitHeaders() { // Initial payload consists of required headers for Wildfire return array_merge( $this->createHeader(array('Protocol', 1), self::PROTOCOL_URI), $this->createHeader(array(1, 'Structure', 1), self::STRUCTURE_URI), $this->createHeader(array(1, 'Plugin', 1), self::PLUGIN_URI) ); } /** * Send header string to the client * * @param string $header * @param string $content */ protected function sendHeader($header, $content) { if (!headers_sent() && self::$sendHeaders) { header(sprintf('%s: %s', $header, $content)); } } /** * Creates & sends header for a record, ensuring init headers have been sent prior * * @see sendHeader() * @see sendInitHeaders() * @param array $record */ protected function write(array $record) { if (!self::$sendHeaders) { return; } // WildFire-specific headers must be sent prior to any messages if (!self::$initialized) { self::$initialized = true; self::$sendHeaders = $this->headersAccepted(); if (!self::$sendHeaders) { return; } foreach ($this->getInitHeaders() as $header => $content) { $this->sendHeader($header, $content); } } $header = $this->createRecordHeader($record); if (trim(current($header)) !== '') { $this->sendHeader(key($header), current($header)); } } /** * Verifies if the headers are accepted by the current user agent * * @return bool */ protected function headersAccepted() { if (!empty($_SERVER['HTTP_USER_AGENT']) && preg_match('{\bFirePHP/\d+\.\d+\b}', $_SERVER['HTTP_USER_AGENT'])) { return true; } return isset($_SERVER['HTTP_X_FIREPHP_VERSION']); } /** * BC getter for the sendHeaders property that has been made static */ public function __get($property) { if ('sendHeaders' !== $property) { throw new \InvalidArgumentException('Undefined property '.$property); } return static::$sendHeaders; } /** * BC setter for the sendHeaders property that has been made static */ public function __set($property, $value) { if ('sendHeaders' !== $property) { throw new \InvalidArgumentException('Undefined property '.$property); } static::$sendHeaders = $value; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog; use Monolog\Handler\HandlerInterface; use Monolog\Handler\StreamHandler; use Psr\Log\LoggerInterface; use Psr\Log\InvalidArgumentException; use Exception; /** * Monolog log channel * * It contains a stack of Handlers and a stack of Processors, * and uses them to store records that are added to it. * * @author Jordi Boggiano <j.boggiano@seld.be> */ class Logger implements LoggerInterface, ResettableInterface { /** * Detailed debug information */ const DEBUG = 100; /** * Interesting events * * Examples: User logs in, SQL logs. */ const INFO = 200; /** * Uncommon events */ const NOTICE = 250; /** * Exceptional occurrences that are not errors * * Examples: Use of deprecated APIs, poor use of an API, * undesirable things that are not necessarily wrong. */ const WARNING = 300; /** * Runtime errors */ const ERROR = 400; /** * Critical conditions * * Example: Application component unavailable, unexpected exception. */ const CRITICAL = 500; /** * Action must be taken immediately * * Example: Entire website down, database unavailable, etc. * This should trigger the SMS alerts and wake you up. */ const ALERT = 550; /** * Urgent alert. */ const EMERGENCY = 600; /** * Monolog API version * * This is only bumped when API breaks are done and should * follow the major version of the library * * @var int */ const API = 1; /** * Logging levels from syslog protocol defined in RFC 5424 * * @var array $levels Logging levels */ protected static $levels = array( self::DEBUG => 'DEBUG', self::INFO => 'INFO', self::NOTICE => 'NOTICE', self::WARNING => 'WARNING', self::ERROR => 'ERROR', self::CRITICAL => 'CRITICAL', self::ALERT => 'ALERT', self::EMERGENCY => 'EMERGENCY', ); /** * @var \DateTimeZone */ protected static $timezone; /** * @var string */ protected $name; /** * The handler stack * * @var HandlerInterface[] */ protected $handlers; /** * Processors that will process all log records * * To process records of a single handler instead, add the processor on that specific handler * * @var callable[] */ protected $processors; /** * @var bool */ protected $microsecondTimestamps = true; /** * @var callable */ protected $exceptionHandler; /** * @param string $name The logging channel * @param HandlerInterface[] $handlers Optional stack of handlers, the first one in the array is called first, etc. * @param callable[] $processors Optional array of processors */ public function __construct($name, array $handlers = array(), array $processors = array()) { $this->name = $name; $this->setHandlers($handlers); $this->processors = $processors; } /** * @return string */ public function getName() { return $this->name; } /** * Return a new cloned instance with the name changed * * @return static */ public function withName($name) { $new = clone $this; $new->name = $name; return $new; } /** * Pushes a handler on to the stack. * * @param HandlerInterface $handler * @return $this */ public function pushHandler(HandlerInterface $handler) { array_unshift($this->handlers, $handler); return $this; } /** * Pops a handler from the stack * * @return HandlerInterface */ public function popHandler() { if (!$this->handlers) { throw new \LogicException('You tried to pop from an empty handler stack.'); } return array_shift($this->handlers); } /** * Set handlers, replacing all existing ones. * * If a map is passed, keys will be ignored. * * @param HandlerInterface[] $handlers * @return $this */ public function setHandlers(array $handlers) { $this->handlers = array(); foreach (array_reverse($handlers) as $handler) { $this->pushHandler($handler); } return $this; } /** * @return HandlerInterface[] */ public function getHandlers() { return $this->handlers; } /** * Adds a processor on to the stack. * * @param callable $callback * @return $this */ public function pushProcessor($callback) { if (!is_callable($callback)) { throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given'); } array_unshift($this->processors, $callback); return $this; } /** * Removes the processor on top of the stack and returns it. * * @return callable */ public function popProcessor() { if (!$this->processors) { throw new \LogicException('You tried to pop from an empty processor stack.'); } return array_shift($this->processors); } /** * @return callable[] */ public function getProcessors() { return $this->processors; } /** * Control the use of microsecond resolution timestamps in the 'datetime' * member of new records. * * Generating microsecond resolution timestamps by calling * microtime(true), formatting the result via sprintf() and then parsing * the resulting string via \DateTime::createFromFormat() can incur * a measurable runtime overhead vs simple usage of DateTime to capture * a second resolution timestamp in systems which generate a large number * of log events. * * @param bool $micro True to use microtime() to create timestamps */ public function useMicrosecondTimestamps($micro) { $this->microsecondTimestamps = (bool) $micro; } /** * Adds a log record. * * @param int $level The logging level * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function addRecord($level, $message, array $context = array()) { if (!$this->handlers) { $this->pushHandler(new StreamHandler('php://stderr', static::DEBUG)); } $levelName = static::getLevelName($level); // check if any handler will handle this message so we can return early and save cycles $handlerKey = null; reset($this->handlers); while ($handler = current($this->handlers)) { if ($handler->isHandling(array('level' => $level))) { $handlerKey = key($this->handlers); break; } next($this->handlers); } if (null === $handlerKey) { return false; } if (!static::$timezone) { static::$timezone = new \DateTimeZone(date_default_timezone_get() ?: 'UTC'); } // php7.1+ always has microseconds enabled, so we do not need this hack if ($this->microsecondTimestamps && PHP_VERSION_ID < 70100) { $ts = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), static::$timezone); } else { $ts = new \DateTime(null, static::$timezone); } $ts->setTimezone(static::$timezone); $record = array( 'message' => (string) $message, 'context' => $context, 'level' => $level, 'level_name' => $levelName, 'channel' => $this->name, 'datetime' => $ts, 'extra' => array(), ); try { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); } while ($handler = current($this->handlers)) { if (true === $handler->handle($record)) { break; } next($this->handlers); } } catch (Exception $e) { $this->handleException($e, $record); } return true; } /** * Ends a log cycle and frees all resources used by handlers. * * Closing a Handler means flushing all buffers and freeing any open resources/handles. * Handlers that have been closed should be able to accept log records again and re-open * themselves on demand, but this may not always be possible depending on implementation. * * This is useful at the end of a request and will be called automatically on every handler * when they get destructed. */ public function close() { foreach ($this->handlers as $handler) { if (method_exists($handler, 'close')) { $handler->close(); } } } /** * Ends a log cycle and resets all handlers and processors to their initial state. * * Resetting a Handler or a Processor means flushing/cleaning all buffers, resetting internal * state, and getting it back to a state in which it can receive log records again. * * This is useful in case you want to avoid logs leaking between two requests or jobs when you * have a long running process like a worker or an application server serving multiple requests * in one process. */ public function reset() { foreach ($this->handlers as $handler) { if ($handler instanceof ResettableInterface) { $handler->reset(); } } foreach ($this->processors as $processor) { if ($processor instanceof ResettableInterface) { $processor->reset(); } } } /** * Adds a log record at the DEBUG level. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function addDebug($message, array $context = array()) { return $this->addRecord(static::DEBUG, $message, $context); } /** * Adds a log record at the INFO level. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function addInfo($message, array $context = array()) { return $this->addRecord(static::INFO, $message, $context); } /** * Adds a log record at the NOTICE level. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function addNotice($message, array $context = array()) { return $this->addRecord(static::NOTICE, $message, $context); } /** * Adds a log record at the WARNING level. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function addWarning($message, array $context = array()) { return $this->addRecord(static::WARNING, $message, $context); } /** * Adds a log record at the ERROR level. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function addError($message, array $context = array()) { return $this->addRecord(static::ERROR, $message, $context); } /** * Adds a log record at the CRITICAL level. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function addCritical($message, array $context = array()) { return $this->addRecord(static::CRITICAL, $message, $context); } /** * Adds a log record at the ALERT level. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function addAlert($message, array $context = array()) { return $this->addRecord(static::ALERT, $message, $context); } /** * Adds a log record at the EMERGENCY level. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function addEmergency($message, array $context = array()) { return $this->addRecord(static::EMERGENCY, $message, $context); } /** * Gets all supported logging levels. * * @return array Assoc array with human-readable level names => level codes. */ public static function getLevels() { return array_flip(static::$levels); } /** * Gets the name of the logging level. * * @param int $level * @return string */ public static function getLevelName($level) { if (!isset(static::$levels[$level])) { throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels))); } return static::$levels[$level]; } /** * Converts PSR-3 levels to Monolog ones if necessary * * @param string|int Level number (monolog) or name (PSR-3) * @return int */ public static function toMonologLevel($level) { if (is_string($level) && defined(__CLASS__.'::'.strtoupper($level))) { return constant(__CLASS__.'::'.strtoupper($level)); } return $level; } /** * Checks whether the Logger has a handler that listens on the given level * * @param int $level * @return bool */ public function isHandling($level) { $record = array( 'level' => $level, ); foreach ($this->handlers as $handler) { if ($handler->isHandling($record)) { return true; } } return false; } /** * Set a custom exception handler * * @param callable $callback * @return $this */ public function setExceptionHandler($callback) { if (!is_callable($callback)) { throw new \InvalidArgumentException('Exception handler must be valid callable (callback or object with an __invoke method), '.var_export($callback, true).' given'); } $this->exceptionHandler = $callback; return $this; } /** * @return callable */ public function getExceptionHandler() { return $this->exceptionHandler; } /** * Delegates exception management to the custom exception handler, * or throws the exception if no custom handler is set. */ protected function handleException(Exception $e, array $record) { if (!$this->exceptionHandler) { throw $e; } call_user_func($this->exceptionHandler, $e, $record); } /** * Adds a log record at an arbitrary level. * * This method allows for compatibility with common interfaces. * * @param mixed $level The log level * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function log($level, $message, array $context = array()) { $level = static::toMonologLevel($level); return $this->addRecord($level, $message, $context); } /** * Adds a log record at the DEBUG level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function debug($message, array $context = array()) { return $this->addRecord(static::DEBUG, $message, $context); } /** * Adds a log record at the INFO level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function info($message, array $context = array()) { return $this->addRecord(static::INFO, $message, $context); } /** * Adds a log record at the NOTICE level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function notice($message, array $context = array()) { return $this->addRecord(static::NOTICE, $message, $context); } /** * Adds a log record at the WARNING level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function warn($message, array $context = array()) { return $this->addRecord(static::WARNING, $message, $context); } /** * Adds a log record at the WARNING level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function warning($message, array $context = array()) { return $this->addRecord(static::WARNING, $message, $context); } /** * Adds a log record at the ERROR level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function err($message, array $context = array()) { return $this->addRecord(static::ERROR, $message, $context); } /** * Adds a log record at the ERROR level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function error($message, array $context = array()) { return $this->addRecord(static::ERROR, $message, $context); } /** * Adds a log record at the CRITICAL level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function crit($message, array $context = array()) { return $this->addRecord(static::CRITICAL, $message, $context); } /** * Adds a log record at the CRITICAL level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function critical($message, array $context = array()) { return $this->addRecord(static::CRITICAL, $message, $context); } /** * Adds a log record at the ALERT level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function alert($message, array $context = array()) { return $this->addRecord(static::ALERT, $message, $context); } /** * Adds a log record at the EMERGENCY level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function emerg($message, array $context = array()) { return $this->addRecord(static::EMERGENCY, $message, $context); } /** * Adds a log record at the EMERGENCY level. * * This method allows for compatibility with common interfaces. * * @param string $message The log message * @param array $context The log context * @return bool Whether the record has been processed */ public function emergency($message, array $context = array()) { return $this->addRecord(static::EMERGENCY, $message, $context); } /** * Set the timezone to be used for the timestamp of log records. * * This is stored globally for all Logger instances * * @param \DateTimeZone $tz Timezone object */ public static function setTimezone(\DateTimeZone $tz) { self::$timezone = $tz; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; use ReflectionExtension; /** * Monolog POSIX signal handler * * @author Robert Gust-Bardon <robert@gust-bardon.org> */ class SignalHandler { private $logger; private $previousSignalHandler = array(); private $signalLevelMap = array(); private $signalRestartSyscalls = array(); public function __construct(LoggerInterface $logger) { $this->logger = $logger; } public function registerSignalHandler($signo, $level = LogLevel::CRITICAL, $callPrevious = true, $restartSyscalls = true, $async = true) { if (!extension_loaded('pcntl') || !function_exists('pcntl_signal')) { return $this; } if ($callPrevious) { if (function_exists('pcntl_signal_get_handler')) { $handler = pcntl_signal_get_handler($signo); if ($handler === false) { return $this; } $this->previousSignalHandler[$signo] = $handler; } else { $this->previousSignalHandler[$signo] = true; } } else { unset($this->previousSignalHandler[$signo]); } $this->signalLevelMap[$signo] = $level; $this->signalRestartSyscalls[$signo] = $restartSyscalls; if (function_exists('pcntl_async_signals') && $async !== null) { pcntl_async_signals($async); } pcntl_signal($signo, array($this, 'handleSignal'), $restartSyscalls); return $this; } public function handleSignal($signo, array $siginfo = null) { static $signals = array(); if (!$signals && extension_loaded('pcntl')) { $pcntl = new ReflectionExtension('pcntl'); $constants = $pcntl->getConstants(); if (!$constants) { // HHVM 3.24.2 returns an empty array. $constants = get_defined_constants(true); $constants = $constants['Core']; } foreach ($constants as $name => $value) { if (substr($name, 0, 3) === 'SIG' && $name[3] !== '_' && is_int($value)) { $signals[$value] = $name; } } unset($constants); } $level = isset($this->signalLevelMap[$signo]) ? $this->signalLevelMap[$signo] : LogLevel::CRITICAL; $signal = isset($signals[$signo]) ? $signals[$signo] : $signo; $context = isset($siginfo) ? $siginfo : array(); $this->logger->log($level, sprintf('Program received signal %s', $signal), $context); if (!isset($this->previousSignalHandler[$signo])) { return; } if ($this->previousSignalHandler[$signo] === true || $this->previousSignalHandler[$signo] === SIG_DFL) { if (extension_loaded('pcntl') && function_exists('pcntl_signal') && function_exists('pcntl_sigprocmask') && function_exists('pcntl_signal_dispatch') && extension_loaded('posix') && function_exists('posix_getpid') && function_exists('posix_kill')) { $restartSyscalls = isset($this->signalRestartSyscalls[$signo]) ? $this->signalRestartSyscalls[$signo] : true; pcntl_signal($signo, SIG_DFL, $restartSyscalls); pcntl_sigprocmask(SIG_UNBLOCK, array($signo), $oldset); posix_kill(posix_getpid(), $signo); pcntl_signal_dispatch(); pcntl_sigprocmask(SIG_SETMASK, $oldset); pcntl_signal($signo, array($this, 'handleSignal'), $restartSyscalls); } } elseif (is_callable($this->previousSignalHandler[$signo])) { if (PHP_VERSION_ID >= 70100) { $this->previousSignalHandler[$signo]($signo, $siginfo); } else { $this->previousSignalHandler[$signo]($signo); } } } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; use Monolog\Handler\AbstractHandler; use Monolog\Registry; /** * Monolog error handler * * A facility to enable logging of runtime errors, exceptions and fatal errors. * * Quick setup: <code>ErrorHandler::register($logger);</code> * * @author Jordi Boggiano <j.boggiano@seld.be> */ class ErrorHandler { private $logger; private $previousExceptionHandler; private $uncaughtExceptionLevel; private $previousErrorHandler; private $errorLevelMap; private $handleOnlyReportedErrors; private $hasFatalErrorHandler; private $fatalLevel; private $reservedMemory; private $lastFatalTrace; private static $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR); public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * Registers a new ErrorHandler for a given Logger * * By default it will handle errors, exceptions and fatal errors * * @param LoggerInterface $logger * @param array|false $errorLevelMap an array of E_* constant to LogLevel::* constant mapping, or false to disable error handling * @param int|false $exceptionLevel a LogLevel::* constant, or false to disable exception handling * @param int|false $fatalLevel a LogLevel::* constant, or false to disable fatal error handling * @return ErrorHandler */ public static function register(LoggerInterface $logger, $errorLevelMap = array(), $exceptionLevel = null, $fatalLevel = null) { //Forces the autoloader to run for LogLevel. Fixes an autoload issue at compile-time on PHP5.3. See https://github.com/Seldaek/monolog/pull/929 class_exists('\\Psr\\Log\\LogLevel', true); $handler = new static($logger); if ($errorLevelMap !== false) { $handler->registerErrorHandler($errorLevelMap); } if ($exceptionLevel !== false) { $handler->registerExceptionHandler($exceptionLevel); } if ($fatalLevel !== false) { $handler->registerFatalHandler($fatalLevel); } return $handler; } public function registerExceptionHandler($level = null, $callPrevious = true) { $prev = set_exception_handler(array($this, 'handleException')); $this->uncaughtExceptionLevel = $level; if ($callPrevious && $prev) { $this->previousExceptionHandler = $prev; } } public function registerErrorHandler(array $levelMap = array(), $callPrevious = true, $errorTypes = -1, $handleOnlyReportedErrors = true) { $prev = set_error_handler(array($this, 'handleError'), $errorTypes); $this->errorLevelMap = array_replace($this->defaultErrorLevelMap(), $levelMap); if ($callPrevious) { $this->previousErrorHandler = $prev ?: true; } $this->handleOnlyReportedErrors = $handleOnlyReportedErrors; } public function registerFatalHandler($level = null, $reservedMemorySize = 20) { register_shutdown_function(array($this, 'handleFatalError')); $this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize); $this->fatalLevel = $level; $this->hasFatalErrorHandler = true; } protected function defaultErrorLevelMap() { return array( E_ERROR => LogLevel::CRITICAL, E_WARNING => LogLevel::WARNING, E_PARSE => LogLevel::ALERT, E_NOTICE => LogLevel::NOTICE, E_CORE_ERROR => LogLevel::CRITICAL, E_CORE_WARNING => LogLevel::WARNING, E_COMPILE_ERROR => LogLevel::ALERT, E_COMPILE_WARNING => LogLevel::WARNING, E_USER_ERROR => LogLevel::ERROR, E_USER_WARNING => LogLevel::WARNING, E_USER_NOTICE => LogLevel::NOTICE, E_STRICT => LogLevel::NOTICE, E_RECOVERABLE_ERROR => LogLevel::ERROR, E_DEPRECATED => LogLevel::NOTICE, E_USER_DEPRECATED => LogLevel::NOTICE, ); } /** * @private */ public function handleException($e) { $this->logger->log( $this->uncaughtExceptionLevel === null ? LogLevel::ERROR : $this->uncaughtExceptionLevel, sprintf('Uncaught Exception %s: "%s" at %s line %s', Utils::getClass($e), $e->getMessage(), $e->getFile(), $e->getLine()), array('exception' => $e) ); if ($this->previousExceptionHandler) { call_user_func($this->previousExceptionHandler, $e); } exit(255); } /** * @private */ public function handleError($code, $message, $file = '', $line = 0, $context = array()) { if ($this->handleOnlyReportedErrors && !(error_reporting() & $code)) { return; } // fatal error codes are ignored if a fatal error handler is present as well to avoid duplicate log entries if (!$this->hasFatalErrorHandler || !in_array($code, self::$fatalErrors, true)) { $level = isset($this->errorLevelMap[$code]) ? $this->errorLevelMap[$code] : LogLevel::CRITICAL; $this->logger->log($level, self::codeToString($code).': '.$message, array('code' => $code, 'message' => $message, 'file' => $file, 'line' => $line)); } else { // http://php.net/manual/en/function.debug-backtrace.php // As of 5.3.6, DEBUG_BACKTRACE_IGNORE_ARGS option was added. // Any version less than 5.3.6 must use the DEBUG_BACKTRACE_IGNORE_ARGS constant value '2'. $trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS); array_shift($trace); // Exclude handleError from trace $this->lastFatalTrace = $trace; } if ($this->previousErrorHandler === true) { return false; } elseif ($this->previousErrorHandler) { return call_user_func($this->previousErrorHandler, $code, $message, $file, $line, $context); } } /** * @private */ public function handleFatalError() { $this->reservedMemory = null; $lastError = error_get_last(); if ($lastError && in_array($lastError['type'], self::$fatalErrors, true)) { $this->logger->log( $this->fatalLevel === null ? LogLevel::ALERT : $this->fatalLevel, 'Fatal Error ('.self::codeToString($lastError['type']).'): '.$lastError['message'], array('code' => $lastError['type'], 'message' => $lastError['message'], 'file' => $lastError['file'], 'line' => $lastError['line'], 'trace' => $this->lastFatalTrace) ); if ($this->logger instanceof Logger) { foreach ($this->logger->getHandlers() as $handler) { if ($handler instanceof AbstractHandler) { $handler->close(); } } } } } private static function codeToString($code) { switch ($code) { case E_ERROR: return 'E_ERROR'; case E_WARNING: return 'E_WARNING'; case E_PARSE: return 'E_PARSE'; case E_NOTICE: return 'E_NOTICE'; case E_CORE_ERROR: return 'E_CORE_ERROR'; case E_CORE_WARNING: return 'E_CORE_WARNING'; case E_COMPILE_ERROR: return 'E_COMPILE_ERROR'; case E_COMPILE_WARNING: return 'E_COMPILE_WARNING'; case E_USER_ERROR: return 'E_USER_ERROR'; case E_USER_WARNING: return 'E_USER_WARNING'; case E_USER_NOTICE: return 'E_USER_NOTICE'; case E_STRICT: return 'E_STRICT'; case E_RECOVERABLE_ERROR: return 'E_RECOVERABLE_ERROR'; case E_DEPRECATED: return 'E_DEPRECATED'; case E_USER_DEPRECATED: return 'E_USER_DEPRECATED'; } return 'Unknown PHP error'; } } <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog; use InvalidArgumentException; /** * Monolog log registry * * Allows to get `Logger` instances in the global scope * via static method calls on this class. * * <code> * $application = new Monolog\Logger('application'); * $api = new Monolog\Logger('api'); * * Monolog\Registry::addLogger($application); * Monolog\Registry::addLogger($api); * * function testLogger() * { * Monolog\Registry::api()->addError('Sent to $api Logger instance'); * Monolog\Registry::application()->addError('Sent to $application Logger instance'); * } * </code> * * @author Tomas Tatarko <tomas@tatarko.sk> */ class Registry { /** * List of all loggers in the registry (by named indexes) * * @var Logger[] */ private static $loggers = array(); /** * Adds new logging channel to the registry * * @param Logger $logger Instance of the logging channel * @param string|null $name Name of the logging channel ($logger->getName() by default) * @param bool $overwrite Overwrite instance in the registry if the given name already exists? * @throws \InvalidArgumentException If $overwrite set to false and named Logger instance already exists */ public static function addLogger(Logger $logger, $name = null, $overwrite = false) { $name = $name ?: $logger->getName(); if (isset(self::$loggers[$name]) && !$overwrite) { throw new InvalidArgumentException('Logger with the given name already exists'); } self::$loggers[$name] = $logger; } /** * Checks if such logging channel exists by name or instance * * @param string|Logger $logger Name or logger instance */ public static function hasLogger($logger) { if ($logger instanceof Logger) { $index = array_search($logger, self::$loggers, true); return false !== $index; } else { return isset(self::$loggers[$logger]); } } /** * Removes instance from registry by name or instance * * @param string|Logger $logger Name or logger instance */ public static function removeLogger($logger) { if ($logger instanceof Logger) { if (false !== ($idx = array_search($logger, self::$loggers, true))) { unset(self::$loggers[$idx]); } } else { unset(self::$loggers[$logger]); } } /** * Clears the registry */ public static function clear() { self::$loggers = array(); } /** * Gets Logger instance from the registry * * @param string $name Name of the requested Logger instance * @throws \InvalidArgumentException If named Logger instance is not in the registry * @return Logger Requested instance of Logger */ public static function getInstance($name) { if (!isset(self::$loggers[$name])) { throw new InvalidArgumentException(sprintf('Requested "%s" logger instance is not in the registry', $name)); } return self::$loggers[$name]; } /** * Gets Logger instance from the registry via static method call * * @param string $name Name of the requested Logger instance * @param array $arguments Arguments passed to static method call * @throws \InvalidArgumentException If named Logger instance is not in the registry * @return Logger Requested instance of Logger */ public static function __callStatic($name, $arguments) { return self::getInstance($name); } } ### 1.25.2 (2019-11-13) * Fixed normalization of Traversables to avoid traversing them as not all of them are rewindable * Fixed setFormatter/getFormatter to forward to the nested handler in FilterHandler, FingersCrossedHandler, BufferHandler and SamplingHandler * Fixed BrowserConsoleHandler formatting when using multiple styles * Fixed normalization of exception codes to be always integers even for PDOException which have them as numeric strings * Fixed normalization of SoapFault objects containing non-strings as "detail" * Fixed json encoding across all handlers to always attempt recovery of non-UTF-8 strings instead of failing the whole encoding ### 1.25.1 (2019-09-06) * Fixed forward-compatible interfaces to be compatible with Monolog 1.x too. ### 1.25.0 (2019-09-06) * Deprecated SlackbotHandler, use SlackWebhookHandler or SlackHandler instead * Deprecated RavenHandler, use sentry/sentry 2.x and their Sentry\Monolog\Handler instead * Deprecated HipChatHandler, migrate to Slack and use SlackWebhookHandler or SlackHandler instead * Added forward-compatible interfaces and traits FormattableHandlerInterface, FormattableHandlerTrait, ProcessableHandlerInterface, ProcessableHandlerTrait. If you use modern PHP and want to make code compatible with Monolog 1 and 2 this can help. You will have to require at least Monolog 1.25 though. * Added support for RFC3164 (outdated BSD syslog protocol) to SyslogUdpHandler * Fixed issue in GroupHandler and WhatFailureGroupHandler where setting multiple processors would duplicate records * Fixed issue in SignalHandler restarting syscalls functionality * Fixed normalizers handling of exception backtraces to avoid serializing arguments in some cases * Fixed ZendMonitorHandler to work with the latest Zend Server versions * Fixed ChromePHPHandler to avoid sending more data than latest Chrome versions allow in headers (4KB down from 256KB). ### 1.24.0 (2018-11-05) * BC Notice: If you are extending any of the Monolog's Formatters' `normalize` method, make sure you add the new `$depth = 0` argument to your function signature to avoid strict PHP warnings. * Added a `ResettableInterface` in order to reset/reset/clear/flush handlers and processors * Added a `ProcessorInterface` as an optional way to label a class as being a processor (mostly useful for autowiring dependency containers) * Added a way to log signals being received using Monolog\SignalHandler * Added ability to customize error handling at the Logger level using Logger::setExceptionHandler * Added InsightOpsHandler to migrate users of the LogEntriesHandler * Added protection to NormalizerHandler against circular and very deep structures, it now stops normalizing at a depth of 9 * Added capture of stack traces to ErrorHandler when logging PHP errors * Added RavenHandler support for a `contexts` context or extra key to forward that to Sentry's contexts * Added forwarding of context info to FluentdFormatter * Added SocketHandler::setChunkSize to override the default chunk size in case you must send large log lines to rsyslog for example * Added ability to extend/override BrowserConsoleHandler * Added SlackWebhookHandler::getWebhookUrl and SlackHandler::getToken to enable class extensibility * Added SwiftMailerHandler::getSubjectFormatter to enable class extensibility * Dropped official support for HHVM in test builds * Fixed normalization of exception traces when call_user_func is used to avoid serializing objects and the data they contain * Fixed naming of fields in Slack handler, all field names are now capitalized in all cases * Fixed HipChatHandler bug where slack dropped messages randomly * Fixed normalization of objects in Slack handlers * Fixed support for PHP7's Throwable in NewRelicHandler * Fixed race bug when StreamHandler sometimes incorrectly reported it failed to create a directory * Fixed table row styling issues in HtmlFormatter * Fixed RavenHandler dropping the message when logging exception * Fixed WhatFailureGroupHandler skipping processors when using handleBatch and implement it where possible * Fixed display of anonymous class names ### 1.23.0 (2017-06-19) * Improved SyslogUdpHandler's support for RFC5424 and added optional `$ident` argument * Fixed GelfHandler truncation to be per field and not per message * Fixed compatibility issue with PHP <5.3.6 * Fixed support for headless Chrome in ChromePHPHandler * Fixed support for latest Aws SDK in DynamoDbHandler * Fixed support for SwiftMailer 6.0+ in SwiftMailerHandler ### 1.22.1 (2017-03-13) * Fixed lots of minor issues in the new Slack integrations * Fixed support for allowInlineLineBreaks in LineFormatter when formatting exception backtraces ### 1.22.0 (2016-11-26) * Added SlackbotHandler and SlackWebhookHandler to set up Slack integration more easily * Added MercurialProcessor to add mercurial revision and branch names to log records * Added support for AWS SDK v3 in DynamoDbHandler * Fixed fatal errors occuring when normalizing generators that have been fully consumed * Fixed RollbarHandler to include a level (rollbar level), monolog_level (original name), channel and datetime (unix) * Fixed RollbarHandler not flushing records automatically, calling close() explicitly is not necessary anymore * Fixed SyslogUdpHandler to avoid sending empty frames * Fixed a few PHP 7.0 and 7.1 compatibility issues ### 1.21.0 (2016-07-29) * Break: Reverted the addition of $context when the ErrorHandler handles regular php errors from 1.20.0 as it was causing issues * Added support for more formats in RotatingFileHandler::setFilenameFormat as long as they have Y, m and d in order * Added ability to format the main line of text the SlackHandler sends by explictly setting a formatter on the handler * Added information about SoapFault instances in NormalizerFormatter * Added $handleOnlyReportedErrors option on ErrorHandler::registerErrorHandler (default true) to allow logging of all errors no matter the error_reporting level ### 1.20.0 (2016-07-02) * Added FingersCrossedHandler::activate() to manually trigger the handler regardless of the activation policy * Added StreamHandler::getUrl to retrieve the stream's URL * Added ability to override addRow/addTitle in HtmlFormatter * Added the $context to context information when the ErrorHandler handles a regular php error * Deprecated RotatingFileHandler::setFilenameFormat to only support 3 formats: Y, Y-m and Y-m-d * Fixed WhatFailureGroupHandler to work with PHP7 throwables * Fixed a few minor bugs ### 1.19.0 (2016-04-12) * Break: StreamHandler will not close streams automatically that it does not own. If you pass in a stream (not a path/url), then it will not close it for you. You can retrieve those using getStream() if needed * Added DeduplicationHandler to remove duplicate records from notifications across multiple requests, useful for email or other notifications on errors * Added ability to use `%message%` and other LineFormatter replacements in the subject line of emails sent with NativeMailHandler and SwiftMailerHandler * Fixed HipChatHandler handling of long messages ### 1.18.2 (2016-04-02) * Fixed ElasticaFormatter to use more precise dates * Fixed GelfMessageFormatter sending too long messages ### 1.18.1 (2016-03-13) * Fixed SlackHandler bug where slack dropped messages randomly * Fixed RedisHandler issue when using with the PHPRedis extension * Fixed AmqpHandler content-type being incorrectly set when using with the AMQP extension * Fixed BrowserConsoleHandler regression ### 1.18.0 (2016-03-01) * Added optional reduction of timestamp precision via `Logger->useMicrosecondTimestamps(false)`, disabling it gets you a bit of performance boost but reduces the precision to the second instead of microsecond * Added possibility to skip some extra stack frames in IntrospectionProcessor if you have some library wrapping Monolog that is always adding frames * Added `Logger->withName` to clone a logger (keeping all handlers) with a new name * Added FluentdFormatter for the Fluentd unix socket protocol * Added HandlerWrapper base class to ease the creation of handler wrappers, just extend it and override as needed * Added support for replacing context sub-keys using `%context.*%` in LineFormatter * Added support for `payload` context value in RollbarHandler * Added setRelease to RavenHandler to describe the application version, sent with every log * Added support for `fingerprint` context value in RavenHandler * Fixed JSON encoding errors that would gobble up the whole log record, we now handle those more gracefully by dropping chars as needed * Fixed write timeouts in SocketHandler and derivatives, set to 10sec by default, lower it with `setWritingTimeout()` * Fixed PHP7 compatibility with regard to Exception/Throwable handling in a few places ### 1.17.2 (2015-10-14) * Fixed ErrorHandler compatibility with non-Monolog PSR-3 loggers * Fixed SlackHandler handling to use slack functionalities better * Fixed SwiftMailerHandler bug when sending multiple emails they all had the same id * Fixed 5.3 compatibility regression ### 1.17.1 (2015-08-31) * Fixed RollbarHandler triggering PHP notices ### 1.17.0 (2015-08-30) * Added support for `checksum` and `release` context/extra values in RavenHandler * Added better support for exceptions in RollbarHandler * Added UidProcessor::getUid * Added support for showing the resource type in NormalizedFormatter * Fixed IntrospectionProcessor triggering PHP notices ### 1.16.0 (2015-08-09) * Added IFTTTHandler to notify ifttt.com triggers * Added Logger::setHandlers() to allow setting/replacing all handlers * Added $capSize in RedisHandler to cap the log size * Fixed StreamHandler creation of directory to only trigger when the first log write happens * Fixed bug in the handling of curl failures * Fixed duplicate logging of fatal errors when both error and fatal error handlers are registered in monolog's ErrorHandler * Fixed missing fatal errors records with handlers that need to be closed to flush log records * Fixed TagProcessor::addTags support for associative arrays ### 1.15.0 (2015-07-12) * Added addTags and setTags methods to change a TagProcessor * Added automatic creation of directories if they are missing for a StreamHandler to open a log file * Added retry functionality to Loggly, Cube and Mandrill handlers so they retry up to 5 times in case of network failure * Fixed process exit code being incorrectly reset to 0 if ErrorHandler::registerExceptionHandler was used * Fixed HTML/JS escaping in BrowserConsoleHandler * Fixed JSON encoding errors being silently suppressed (PHP 5.5+ only) ### 1.14.0 (2015-06-19) * Added PHPConsoleHandler to send record to Chrome's PHP Console extension and library * Added support for objects implementing __toString in the NormalizerFormatter * Added support for HipChat's v2 API in HipChatHandler * Added Logger::setTimezone() to initialize the timezone monolog should use in case date.timezone isn't correct for your app * Added an option to send formatted message instead of the raw record on PushoverHandler via ->useFormattedMessage(true) * Fixed curl errors being silently suppressed ### 1.13.1 (2015-03-09) * Fixed regression in HipChat requiring a new token to be created ### 1.13.0 (2015-03-05) * Added Registry::hasLogger to check for the presence of a logger instance * Added context.user support to RavenHandler * Added HipChat API v2 support in the HipChatHandler * Added NativeMailerHandler::addParameter to pass params to the mail() process * Added context data to SlackHandler when $includeContextAndExtra is true * Added ability to customize the Swift_Message per-email in SwiftMailerHandler * Fixed SwiftMailerHandler to lazily create message instances if a callback is provided * Fixed serialization of INF and NaN values in Normalizer and LineFormatter ### 1.12.0 (2014-12-29) * Break: HandlerInterface::isHandling now receives a partial record containing only a level key. This was always the intent and does not break any Monolog handler but is strictly speaking a BC break and you should check if you relied on any other field in your own handlers. * Added PsrHandler to forward records to another PSR-3 logger * Added SamplingHandler to wrap around a handler and include only every Nth record * Added MongoDBFormatter to support better storage with MongoDBHandler (it must be enabled manually for now) * Added exception codes in the output of most formatters * Added LineFormatter::includeStacktraces to enable exception stack traces in logs (uses more than one line) * Added $useShortAttachment to SlackHandler to minify attachment size and $includeExtra to append extra data * Added $host to HipChatHandler for users of private instances * Added $transactionName to NewRelicHandler and support for a transaction_name context value * Fixed MandrillHandler to avoid outputing API call responses * Fixed some non-standard behaviors in SyslogUdpHandler ### 1.11.0 (2014-09-30) * Break: The NewRelicHandler extra and context data are now prefixed with extra_ and context_ to avoid clashes. Watch out if you have scripts reading those from the API and rely on names * Added WhatFailureGroupHandler to suppress any exception coming from the wrapped handlers and avoid chain failures if a logging service fails * Added MandrillHandler to send emails via the Mandrillapp.com API * Added SlackHandler to log records to a Slack.com account * Added FleepHookHandler to log records to a Fleep.io account * Added LogglyHandler::addTag to allow adding tags to an existing handler * Added $ignoreEmptyContextAndExtra to LineFormatter to avoid empty [] at the end * Added $useLocking to StreamHandler and RotatingFileHandler to enable flock() while writing * Added support for PhpAmqpLib in the AmqpHandler * Added FingersCrossedHandler::clear and BufferHandler::clear to reset them between batches in long running jobs * Added support for adding extra fields from $_SERVER in the WebProcessor * Fixed support for non-string values in PrsLogMessageProcessor * Fixed SwiftMailer messages being sent with the wrong date in long running scripts * Fixed minor PHP 5.6 compatibility issues * Fixed BufferHandler::close being called twice ### 1.10.0 (2014-06-04) * Added Logger::getHandlers() and Logger::getProcessors() methods * Added $passthruLevel argument to FingersCrossedHandler to let it always pass some records through even if the trigger level is not reached * Added support for extra data in NewRelicHandler * Added $expandNewlines flag to the ErrorLogHandler to create multiple log entries when a message has multiple lines ### 1.9.1 (2014-04-24) * Fixed regression in RotatingFileHandler file permissions * Fixed initialization of the BufferHandler to make sure it gets flushed after receiving records * Fixed ChromePHPHandler and FirePHPHandler's activation strategies to be more conservative ### 1.9.0 (2014-04-20) * Added LogEntriesHandler to send logs to a LogEntries account * Added $filePermissions to tweak file mode on StreamHandler and RotatingFileHandler * Added $useFormatting flag to MemoryProcessor to make it send raw data in bytes * Added support for table formatting in FirePHPHandler via the table context key * Added a TagProcessor to add tags to records, and support for tags in RavenHandler * Added $appendNewline flag to the JsonFormatter to enable using it when logging to files * Added sound support to the PushoverHandler * Fixed multi-threading support in StreamHandler * Fixed empty headers issue when ChromePHPHandler received no records * Fixed default format of the ErrorLogHandler ### 1.8.0 (2014-03-23) * Break: the LineFormatter now strips newlines by default because this was a bug, set $allowInlineLineBreaks to true if you need them * Added BrowserConsoleHandler to send logs to any browser's console via console.log() injection in the output * Added FilterHandler to filter records and only allow those of a given list of levels through to the wrapped handler * Added FlowdockHandler to send logs to a Flowdock account * Added RollbarHandler to send logs to a Rollbar account * Added HtmlFormatter to send prettier log emails with colors for each log level * Added GitProcessor to add the current branch/commit to extra record data * Added a Monolog\Registry class to allow easier global access to pre-configured loggers * Added support for the new official graylog2/gelf-php lib for GelfHandler, upgrade if you can by replacing the mlehner/gelf-php requirement * Added support for HHVM * Added support for Loggly batch uploads * Added support for tweaking the content type and encoding in NativeMailerHandler * Added $skipClassesPartials to tweak the ignored classes in the IntrospectionProcessor * Fixed batch request support in GelfHandler ### 1.7.0 (2013-11-14) * Added ElasticSearchHandler to send logs to an Elastic Search server * Added DynamoDbHandler and ScalarFormatter to send logs to Amazon's Dynamo DB * Added SyslogUdpHandler to send logs to a remote syslogd server * Added LogglyHandler to send logs to a Loggly account * Added $level to IntrospectionProcessor so it only adds backtraces when needed * Added $version to LogstashFormatter to allow using the new v1 Logstash format * Added $appName to NewRelicHandler * Added configuration of Pushover notification retries/expiry * Added $maxColumnWidth to NativeMailerHandler to change the 70 chars default * Added chainability to most setters for all handlers * Fixed RavenHandler batch processing so it takes the message from the record with highest priority * Fixed HipChatHandler batch processing so it sends all messages at once * Fixed issues with eAccelerator * Fixed and improved many small things ### 1.6.0 (2013-07-29) * Added HipChatHandler to send logs to a HipChat chat room * Added ErrorLogHandler to send logs to PHP's error_log function * Added NewRelicHandler to send logs to NewRelic's service * Added Monolog\ErrorHandler helper class to register a Logger as exception/error/fatal handler * Added ChannelLevelActivationStrategy for the FingersCrossedHandler to customize levels by channel * Added stack traces output when normalizing exceptions (json output & co) * Added Monolog\Logger::API constant (currently 1) * Added support for ChromePHP's v4.0 extension * Added support for message priorities in PushoverHandler, see $highPriorityLevel and $emergencyLevel * Added support for sending messages to multiple users at once with the PushoverHandler * Fixed RavenHandler's support for batch sending of messages (when behind a Buffer or FingersCrossedHandler) * Fixed normalization of Traversables with very large data sets, only the first 1000 items are shown now * Fixed issue in RotatingFileHandler when an open_basedir restriction is active * Fixed minor issues in RavenHandler and bumped the API to Raven 0.5.0 * Fixed SyslogHandler issue when many were used concurrently with different facilities ### 1.5.0 (2013-04-23) * Added ProcessIdProcessor to inject the PID in log records * Added UidProcessor to inject a unique identifier to all log records of one request/run * Added support for previous exceptions in the LineFormatter exception serialization * Added Monolog\Logger::getLevels() to get all available levels * Fixed ChromePHPHandler so it avoids sending headers larger than Chrome can handle ### 1.4.1 (2013-04-01) * Fixed exception formatting in the LineFormatter to be more minimalistic * Fixed RavenHandler's handling of context/extra data, requires Raven client >0.1.0 * Fixed log rotation in RotatingFileHandler to work with long running scripts spanning multiple days * Fixed WebProcessor array access so it checks for data presence * Fixed Buffer, Group and FingersCrossed handlers to make use of their processors ### 1.4.0 (2013-02-13) * Added RedisHandler to log to Redis via the Predis library or the phpredis extension * Added ZendMonitorHandler to log to the Zend Server monitor * Added the possibility to pass arrays of handlers and processors directly in the Logger constructor * Added `$useSSL` option to the PushoverHandler which is enabled by default * Fixed ChromePHPHandler and FirePHPHandler issue when multiple instances are used simultaneously * Fixed header injection capability in the NativeMailHandler ### 1.3.1 (2013-01-11) * Fixed LogstashFormatter to be usable with stream handlers * Fixed GelfMessageFormatter levels on Windows ### 1.3.0 (2013-01-08) * Added PSR-3 compliance, the `Monolog\Logger` class is now an instance of `Psr\Log\LoggerInterface` * Added PsrLogMessageProcessor that you can selectively enable for full PSR-3 compliance * Added LogstashFormatter (combine with SocketHandler or StreamHandler to send logs to Logstash) * Added PushoverHandler to send mobile notifications * Added CouchDBHandler and DoctrineCouchDBHandler * Added RavenHandler to send data to Sentry servers * Added support for the new MongoClient class in MongoDBHandler * Added microsecond precision to log records' timestamps * Added `$flushOnOverflow` param to BufferHandler to flush by batches instead of losing the oldest entries * Fixed normalization of objects with cyclic references ### 1.2.1 (2012-08-29) * Added new $logopts arg to SyslogHandler to provide custom openlog options * Fixed fatal error in SyslogHandler ### 1.2.0 (2012-08-18) * Added AmqpHandler (for use with AMQP servers) * Added CubeHandler * Added NativeMailerHandler::addHeader() to send custom headers in mails * Added the possibility to specify more than one recipient in NativeMailerHandler * Added the possibility to specify float timeouts in SocketHandler * Added NOTICE and EMERGENCY levels to conform with RFC 5424 * Fixed the log records to use the php default timezone instead of UTC * Fixed BufferHandler not being flushed properly on PHP fatal errors * Fixed normalization of exotic resource types * Fixed the default format of the SyslogHandler to avoid duplicating datetimes in syslog ### 1.1.0 (2012-04-23) * Added Monolog\Logger::isHandling() to check if a handler will handle the given log level * Added ChromePHPHandler * Added MongoDBHandler * Added GelfHandler (for use with Graylog2 servers) * Added SocketHandler (for use with syslog-ng for example) * Added NormalizerFormatter * Added the possibility to change the activation strategy of the FingersCrossedHandler * Added possibility to show microseconds in logs * Added `server` and `referer` to WebProcessor output ### 1.0.2 (2011-10-24) * Fixed bug in IE with large response headers and FirePHPHandler ### 1.0.1 (2011-08-25) * Added MemoryPeakUsageProcessor and MemoryUsageProcessor * Added Monolog\Logger::getName() to get a logger's channel name ### 1.0.0 (2011-07-06) * Added IntrospectionProcessor to get info from where the logger was called * Fixed WebProcessor in CLI ### 1.0.0-RC1 (2011-07-01) * Initial release { "name": "monolog/monolog", "description": "Sends your logs to files, sockets, inboxes, databases and various web services", "keywords": ["log", "logging", "psr-3"], "homepage": "http://github.com/Seldaek/monolog", "type": "library", "license": "MIT", "authors": [ { "name": "Jordi Boggiano", "email": "j.boggiano@seld.be", "homepage": "http://seld.be" } ], "require": { "php": ">=5.3.0", "psr/log": "~1.0" }, "require-dev": { "phpunit/phpunit": "~4.5", "graylog2/gelf-php": "~1.0", "sentry/sentry": "^0.13", "ruflin/elastica": ">=0.90 <3.0", "doctrine/couchdb": "~1.0@dev", "aws/aws-sdk-php": "^2.4.9 || ^3.0", "php-amqplib/php-amqplib": "~2.4", "swiftmailer/swiftmailer": "^5.3|^6.0", "php-console/php-console": "^3.1.3", "phpunit/phpunit-mock-objects": "2.3.0", "jakub-onderka/php-parallel-lint": "0.9" }, "_": "phpunit/phpunit-mock-objects required in 2.3.0 due to https://github.com/sebastianbergmann/phpunit-mock-objects/issues/223 - needs hhvm 3.8+ on travis", "suggest": { "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", "sentry/sentry": "Allow sending log messages to a Sentry server", "doctrine/couchdb": "Allow sending log messages to a CouchDB server", "ruflin/elastica": "Allow sending log messages to an Elastic Search server", "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", "ext-mongo": "Allow sending log messages to a MongoDB server", "mongodb/mongodb": "Allow sending log messages to a MongoDB server via PHP Driver", "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", "rollbar/rollbar": "Allow sending log messages to Rollbar", "php-console/php-console": "Allow sending log messages to Google Chrome" }, "autoload": { "psr-4": {"Monolog\\": "src/Monolog"} }, "autoload-dev": { "psr-4": {"Monolog\\": "tests/Monolog"} }, "provide": { "psr/log-implementation": "1.0.0" }, "extra": { "branch-alias": { "dev-master": "2.0.x-dev" } }, "scripts": { "test": [ "parallel-lint . --exclude vendor --exclude src/Monolog/Handler/FormattableHandlerInterface.php --exclude src/Monolog/Handler/FormattableHandlerTrait.php --exclude src/Monolog/Handler/ProcessableHandlerInterface.php --exclude src/Monolog/Handler/ProcessableHandlerTrait.php", "phpunit" ] } } # Monolog - Logging for PHP [](https://travis-ci.org/Seldaek/monolog) [](https://packagist.org/packages/monolog/monolog) [](https://packagist.org/packages/monolog/monolog) Monolog sends your logs to files, sockets, inboxes, databases and various web services. See the complete list of handlers below. Special handlers allow you to build advanced logging strategies. This library implements the [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) interface that you can type-hint against in your own libraries to keep a maximum of interoperability. You can also use it in your applications to make sure you can always use another compatible logger at a later time. As of 1.11.0 Monolog public APIs will also accept PSR-3 log levels. Internally Monolog still uses its own level scheme since it predates PSR-3. ## Installation Install the latest version with ```bash $ composer require monolog/monolog ``` ## Basic Usage ```php <?php use Monolog\Logger; use Monolog\Handler\StreamHandler; // create a log channel $log = new Logger('name'); $log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING)); // add records to the log $log->addWarning('Foo'); $log->addError('Bar'); ``` ## Documentation - [Usage Instructions](doc/01-usage.md) - [Handlers, Formatters and Processors](doc/02-handlers-formatters-processors.md) - [Utility classes](doc/03-utilities.md) - [Extending Monolog](doc/04-extending.md) ## Third Party Packages Third party handlers, formatters and processors are [listed in the wiki](https://github.com/Seldaek/monolog/wiki/Third-Party-Packages). You can also add your own there if you publish one. ## About ### Requirements - Monolog works with PHP 5.3 or above, and is also tested to work with HHVM. ### Submitting bugs and feature requests Bugs and feature request are tracked on [GitHub](https://github.com/Seldaek/monolog/issues) ### Framework Integrations - Frameworks and libraries using [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) can be used very easily with Monolog since it implements the interface. - [Symfony2](http://symfony.com) comes out of the box with Monolog. - [Silex](http://silex.sensiolabs.org/) comes out of the box with Monolog. - [Laravel 4 & 5](http://laravel.com/) come out of the box with Monolog. - [Lumen](http://lumen.laravel.com/) comes out of the box with Monolog. - [PPI](http://www.ppi.io/) comes out of the box with Monolog. - [CakePHP](http://cakephp.org/) is usable with Monolog via the [cakephp-monolog](https://github.com/jadb/cakephp-monolog) plugin. - [Slim](http://www.slimframework.com/) is usable with Monolog via the [Slim-Monolog](https://github.com/Flynsarmy/Slim-Monolog) log writer. - [XOOPS 2.6](http://xoops.org/) comes out of the box with Monolog. - [Aura.Web_Project](https://github.com/auraphp/Aura.Web_Project) comes out of the box with Monolog. - [Nette Framework](http://nette.org/en/) can be used with Monolog via [Kdyby/Monolog](https://github.com/Kdyby/Monolog) extension. - [Proton Micro Framework](https://github.com/alexbilbie/Proton) comes out of the box with Monolog. ### Author Jordi Boggiano - <j.boggiano@seld.be> - <http://twitter.com/seldaek><br /> See also the list of [contributors](https://github.com/Seldaek/monolog/contributors) which participated in this project. ### License Monolog is licensed under the MIT License - see the `LICENSE` file for details ### Acknowledgements This library is heavily inspired by Python's [Logbook](https://logbook.readthedocs.io/en/stable/) library, although most concepts have been adjusted to fit to the PHP world. <?php namespace ElementPack\Modules\UserLogin\Skins; use Elementor\Controls_Manager; use Elementor\Icons_Manager; use Elementor\Skin_Base as Elementor_Skin_Base; use ElementPack\Base\Module_Base; use ElementPack\Element_Pack_Loader; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Dropdown extends Elementor_Skin_Base { protected function _register_controls_actions() { parent::_register_controls_actions(); add_action( 'elementor/element/bdt-user-login/section_content_custom_nav/before_section_start', [ $this, 'register_dropdown_button_controls' ] ); } public function get_id() { return 'bdt-dropdown'; } public function get_title() { return __( 'Dropdown', 'bdthemes-element-pack' ); } public function register_dropdown_button_controls( Module_Base $widget ) { $this->parent = $widget; $this->start_controls_section( 'section_dropdown_button', [ 'label' => esc_html__( 'Dropdown Button', 'bdthemes-element-pack' ), ] ); $this->add_control( 'dropdown_button_text', [ 'label' => esc_html__( 'Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Log In', 'bdthemes-element-pack' ), ] ); $this->add_control( 'dropdown_btn_icon_only_on_mobile', [ 'label' => esc_html__( 'Show Icon Only on Mobile', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'dropdown_button_size', [ 'label' => esc_html__( 'Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'sm', 'options' => element_pack_button_sizes(), ] ); $this->add_responsive_control( 'dropdown_button_align', [ 'label' => esc_html__( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => esc_html__( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'prefix_class' => 'elementor%s-align-', 'default' => '', ] ); $this->add_control( 'user_login_dropdown_icon', [ 'label' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::ICONS, 'fa4compatibility' => 'dropdown_button_icon', ] ); $this->add_control( 'dropdown_button_icon_align', [ 'label' => esc_html__( 'Icon Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'right', 'options' => [ 'left' => esc_html__( 'Before', 'bdthemes-element-pack' ), 'right' => esc_html__( 'After', 'bdthemes-element-pack' ), ], 'condition' => [ $this->get_control_id( 'user_login_dropdown_icon[value]!' ) => '', ], ] ); $this->add_control( 'dropdown_button_icon_indent', [ 'label' => esc_html__( 'Icon Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 8, ], 'range' => [ 'px' => [ 'max' => 50, ], ], 'condition' => [ $this->get_control_id( 'user_login_dropdown_icon[value]!' ) => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-button-dropdown .bdt-button-dropdown-icon.elementor-align-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-button-dropdown .bdt-button-dropdown-icon.elementor-align-icon-left' => 'margin-right: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); } public function render() { $settings = $this->parent->get_settings(); $id = 'bdt-user-login-dropdown-' . $this->parent->get_id(); $current_url = remove_query_arg( 'fake_arg' ); $button_size = $this->get_instance_value( 'dropdown_button_size' ); $button_animation = $this->get_instance_value( 'dropdown_button_animation' ); if ( is_user_logged_in() && ! Element_Pack_Loader::elementor()->editor->is_edit_mode() ) { $this->parent->add_render_attribute( [ 'dropdown-button' => [ 'class' => [ 'elementor-button', 'bdt-button-dropdown', 'elementor-size-' . esc_attr( $button_size ), $button_animation ? 'elementor-animation-' . esc_attr( $button_animation ) : '' ], 'href' => '#', ] ] ); ?> <?php $current_user = wp_get_current_user(); ?> <div class="bdt-user-login bdt-user-login-skin-dropdown"> <?php if ( $settings['show_logged_in_content'] ) : ?> <a <?php $this->parent->print_render_attribute_string( 'dropdown-button' ); ?>> <span class="bdt-user-name bdt-visible@l"> <?php if ( $settings['show_logged_in_message'] ) : ?> <?php if ( $settings['logged_in_custom_message'] and $settings['custom_labels'] ) : ?> <?php echo esc_html( $settings['logged_in_custom_message'] ); ?> <?php else : ?> <?php esc_html_e( 'Hi', 'bdthemes-element-pack' ); ?>, <?php endif; ?> <?php endif; ?> <?php if ( $settings['show_user_name'] ) : ?> <?php echo esc_html( $current_user->display_name ); ?> <?php endif; ?> </span> <?php if ( 'yes' == $settings['show_avatar_icon_in_button'] ) { ?> <span class="bdt-user-login-button-avatar<?php echo ( 'yes' == $settings['show_avatar_icon_in_button'] ) ? '' : ' bdt-hidden@l'; ?>"> <?php Icons_Manager::render_icon( $settings['avatar_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); ?> </span> <?php } else { ?> <span class="bdt-user-login-button-avatar<?php echo ( 'yes' == $settings['show_avatar_in_button'] ) ? '' : ' bdt-hidden@l'; ?>"> <?php echo get_avatar( $current_user->user_email, 32 ); ?> </span> <?php } ?> </a> <?php $this->parent->user_dropdown_menu(); ?> <?php else : ?> <?php $logout_url = $current_url; if ( isset ( $settings['redirect_after_logOut'] ) && ! empty ( $settings['redirect_logOut_url']['url'] ) ) { $logout_url = $settings['redirect_logOut_url']['url']; } ?> <a class="bdt-logout-button bdt-button bdt-button-primary" href="<?php echo esc_url( wp_logout_url( $logout_url ) ); ?>" class="bdt-ul-logout-menu"> <?php echo esc_html( $settings['logout_text'] ); ?> </a> <?php endif; ?> </div> <?php return; } else { $dropdown_offset = $settings['dropdown_offset']; $this->parent->add_render_attribute( [ 'dropdown-settings' => [ 'data-bdt-dropdown' => [ wp_json_encode( array_filter( [ "mode" => $settings["dropdown_mode"], "pos" => $settings["dropdown_position"], "offset" => $dropdown_offset["size"] ] ) ) ] ] ] ); $this->parent->add_render_attribute( 'dropdown-settings', 'class', 'bdt-dropdown bdt-drop' ); } $this->parent->form_fields_render_attributes(); $this->parent->add_render_attribute( [ 'dropdown-button-settings' => [ 'class' => [ 'elementor-button', 'bdt-button-dropdown', 'elementor-size-' . esc_attr( $button_size ), $button_animation ? 'elementor-animation-' . esc_attr( $button_animation ) : '' ], 'href' => 'javascript:void(0)', ] ] ); ?> <div class="bdt-user-login bdt-user-login-skin-dropdown"> <a <?php $this->parent->print_render_attribute_string( 'dropdown-button-settings' ); ?>> <?php $this->render_text(); ?> </a> <div <?php $this->parent->print_render_attribute_string( 'dropdown-settings' ); ?>> <div class="elementor-form-fields-wrapper bdt-text-left"> <?php $this->parent->user_login_form(); ?> <?php $this->parent->social_login(); ?> </div> </div> </div> <?php } protected function render_text() { $settings = $this->parent->get_settings_for_display(); $this->parent->add_render_attribute( 'button-icon', 'class', [ 'bdt-button-dropdown-icon', 'elementor-button-icon', 'elementor-align-icon-' . esc_attr( $this->get_instance_value( 'dropdown_button_icon_align' ) ) ] ); if ( is_user_logged_in() && ! Element_Pack_Loader::elementor()->editor->is_edit_mode() ) { $button_text = esc_html__( 'Logout', 'bdthemes-element-pack' ); } else { $button_text = $this->get_instance_value( 'dropdown_button_text' ); } if ( ! isset ( $settings['dropdown_button_icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $settings['dropdown_button_icon'] = 'fas fa-user'; } $migrated = isset ( $settings['__fa4_migrated']['user_login_dropdown_icon'] ); $is_new = empty ( $settings['dropdown_button_icon'] ) && Icons_Manager::is_migration_allowed(); $user_login_dropdown_icon = $this->get_instance_value( 'user_login_dropdown_icon' ); $icon_visible = $this->get_instance_value( 'dropdown_btn_icon_only_on_mobile' ); ?> <span class="elementor-button-content-wrapper"> <?php if ( ! empty ( $user_login_dropdown_icon['value'] ) ) : ?> <span <?php $this->parent->print_render_attribute_string( 'button-icon' ); ?>> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( (array) $user_login_dropdown_icon, [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $settings['dropdown_button_icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </span> <?php else : ?> <?php if ( $icon_visible ) : ?> <?php $this->parent->add_render_attribute( 'button-icon', 'class', [ 'bdt-hidden@l' ] ); ?> <span <?php $this->parent->print_render_attribute_string( 'button-icon' ); ?>> <i class="ep-icon-lock" aria-hidden="true"></i> </span> <?php endif; ?> <?php endif; ?> <?php $text_visible = ( $this->get_instance_value( 'dropdown_btn_icon_only_on_mobile' ) ) ? ' bdt-visible@l' : ''; ?> <span class="elementor-button-text<?php echo esc_attr( $text_visible ); ?>"> <?php echo esc_html( $button_text ); ?> </span> </span> <?php } } <?php namespace ElementPack\Modules\UserLogin\Skins; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Typography; use Elementor\Icons_Manager; use Elementor\Skin_Base as Elementor_Skin_Base; use ElementPack\Element_Pack_Loader; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Modal extends Elementor_Skin_Base { protected function _register_controls_actions() { parent::_register_controls_actions(); add_action( 'elementor/element/bdt-user-login/section_style/before_section_start', [ $this, 'register_controls' ] ); add_action( 'elementor/element/bdt-user-login/section_forms_additional_options/before_section_start', [ $this, 'register_modal_button_controls' ] ); // add_action( 'elementor/element/bdt-user-login/section_style/before_section_start', [ $this, 'register_modal_button_style_controls' ] ); } public function get_id() { return 'bdt-modal'; } public function get_title() { return __( 'Modal', 'bdthemes-element-pack' ); } public function register_modal_button_controls( Module_Base $widget ) { $this->parent = $widget; $this->start_controls_section( 'section_modal_button', [ 'label' => esc_html__( 'Modal Button', 'bdthemes-element-pack' ), ] ); $this->add_control( 'modal_button_text', [ 'label' => esc_html__( 'Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Log In', 'bdthemes-element-pack' ), ] ); $this->add_control( 'modal_btn_icon_only_on_mobile', [ 'label' => esc_html__( 'Show Icon Only on Mobile', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'modal_button_size', [ 'label' => esc_html__( 'Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'sm', 'options' => element_pack_button_sizes(), ] ); $this->add_responsive_control( 'modal_button_align', [ 'label' => esc_html__( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => esc_html__( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'prefix_class' => 'elementor%s-align-', 'default' => '', ] ); $this->add_control( 'user_login_modal_icon', [ 'label' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::ICONS, 'fa4compatibility' => 'modal_button_icon', ] ); $this->add_control( 'modal_button_icon_align', [ 'label' => esc_html__( 'Icon Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'right', 'options' => [ 'left' => esc_html__( 'Before', 'bdthemes-element-pack' ), 'right' => esc_html__( 'After', 'bdthemes-element-pack' ), ], 'condition' => [ $this->get_control_id( 'user_login_modal_icon[value]!' ) => '', ], ] ); $this->add_control( 'modal_button_icon_indent', [ 'label' => esc_html__( 'Icon Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 8, ], 'range' => [ 'px' => [ 'max' => 50, ], ], 'condition' => [ $this->get_control_id( 'user_login_modal_icon[value]!' ) => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-button-modal .bdt-modal-button-icon.elementor-align-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-button-modal .bdt-modal-button-icon.elementor-align-icon-left' => 'margin-right: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); } public function register_controls( Module_Base $widget ) { $this->parent = $widget; $this->start_controls_section( 'section_modal_style', [ 'label' => esc_html__( 'Modal Style', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'modal_text_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#modal{{ID}} .bdt-modal-dialog .bdt-modal-header *' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'modal_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '#modal{{ID}} .bdt-modal-dialog' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'modal_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '#modal{{ID}} .bdt-modal-dialog', 'separator' => 'before', ] ); $this->add_control( 'modal_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '#modal{{ID}} .bdt-modal-dialog' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'modal_text_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '#modal{{ID}} .bdt-modal-dialog .bdt-modal-body' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'modal_close_button', [ 'label' => esc_html__( 'Close Button', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'modal_header', [ 'label' => esc_html__( 'Modal Header', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'modal_custom_width', [ 'label' => esc_html__( 'Modal Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'options' => [ 'default' => esc_html__( 'Default', 'bdthemes-element-pack' ), 'full' => esc_html__( 'Full', 'bdthemes-element-pack' ), 'container' => esc_html__( 'Container', 'bdthemes-element-pack' ), 'custom' => esc_html__( 'Custom', 'bdthemes-element-pack' ), ], 'default' => 'default', 'separator' => 'before', ] ); $this->add_responsive_control( 'modal_custom_width_custom', [ 'label' => esc_html__( 'Custom Width(px)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 200, 'max' => 1200, ], ], 'selectors' => [ '#modal{{ID}}.bdt-modal-custom .bdt-modal-dialog' => 'width: {{SIZE}}{{UNIT}};', ], 'condition' => [ $this->get_control_id( 'modal_custom_width[value]' ) => 'custom', ], ] ); $this->end_controls_section(); } public function render() { $settings = $this->parent->get_settings_for_display(); $id = 'modal' . $this->parent->get_id(); $current_url = remove_query_arg( 'fake_arg' ); $button_size = $this->get_instance_value( 'modal_button_size' ); $button_animation = $this->get_instance_value( 'modal_button_animation' ); $modal_width = $this->get_instance_value( 'modal_custom_width' ); $this->parent->add_render_attribute( [ 'modal-button' => [ 'class' => [ 'elementor-button', 'bdt-button-modal', 'elementor-size-' . esc_attr( $button_size ), $this->get_instance_value( 'modal_button_animation' ) ? 'elementor-animation-' . esc_attr( $button_animation ) : '' ], 'href' => '#', ] ] ); if ( is_user_logged_in() && ! Element_Pack_Loader::elementor()->editor->is_edit_mode() ) { ?> <?php $current_user = wp_get_current_user(); ?> <div id="<?php echo esc_attr( $id ); ?>" class="bdt-user-login bdt-user-login-skin-dropdown"> <?php if ( $settings['show_logged_in_content'] ) : ?> <a <?php $this->parent->print_render_attribute_string( 'modal-button' ); ?>> <span class="bdt-user-name bdt-visible@l"> <?php if ( $settings['show_logged_in_message'] ) : ?> <?php if ( $settings['logged_in_custom_message'] and $settings['custom_labels'] ) : ?> <?php echo esc_html( $settings['logged_in_custom_message'] ); ?> <?php else : ?> <?php esc_html_e( 'Hi', 'bdthemes-element-pack' ); ?>, <?php endif; ?> <?php endif; ?> <?php if ( $settings['show_user_name'] ) : ?> <?php echo esc_html( $current_user->display_name ); ?> <?php endif; ?> </span> <span class="bdt-user-login-button-avatar<?php echo ( '' == $settings['show_avatar_in_button'] ) ? ' bdt-hidden@l' : ''; ?>"> <?php echo get_avatar( $current_user->user_email, 32 ); ?> </span> </a> <?php $this->parent->user_dropdown_menu(); ?> <?php else : ?> <?php $logout_url = $current_url; if ( isset ( $settings['redirect_after_logOut'] ) && ! empty ( $settings['redirect_logOut_url']['url'] ) ) { $logout_url = $settings['redirect_logOut_url']['url']; } ?> <a class="bdt-logout-button bdt-button bdt-button-primary" href="<?php echo esc_url( wp_logout_url( $logout_url ) ); ?>" class="bdt-ul-logout-menu"> <?php echo esc_html( $settings['logout_text'] ); ?> </a> <?php endif; ?> </div> <?php return; } $this->parent->form_fields_render_attributes(); $this->parent->add_render_attribute( [ 'modal-button-settings' => [ 'class' => [ 'elementor-button', 'bdt-button-modal', 'elementor-size-' . esc_attr( $button_size ), $this->get_instance_value( 'modal_button_animation' ) ? 'elementor-animation-' . esc_attr( $button_animation ) : '' ], 'href' => 'javascript:void(0)', 'data-bdt-toggle' => 'target: #' . esc_attr( $id ), ] ] ); ?> <div class="bdt-user-login bdt-user-login-skin-modal"> <a <?php $this->parent->print_render_attribute_string( 'modal-button-settings' ); ?>> <?php $this->render_text(); ?> </a> <div id="<?php echo esc_attr( $id ); ?>" class="bdt-flex-top bdt-user-login-modal bdt-modal-<?php echo esc_attr( $modal_width ); ?>" data-bdt-modal> <div class="bdt-modal-dialog bdt-margin-auto-vertical"> <?php if ( $this->get_instance_value( 'modal_close_button' ) ) : ?> <button class="bdt-modal-close-default" type="button" data-bdt-close></button> <?php endif; ?> <?php if ( $this->get_instance_value( 'modal_header' ) ) : ?> <div class="bdt-modal-header"> <h2 class="bdt-modal-title"><span class="ep-icon-user-circle-o"></span> <?php esc_html_e( 'User Login!', 'bdthemes-element-pack' ); ?> </h2> </div> <?php endif; ?> <div class="elementor-form-fields-wrapper bdt-modal-body"> <?php $this->parent->user_login_form(); ?> <?php $this->parent->social_login(); ?> </div> <div class="bdt-recaptcha-text bdt-text-center"> This site is protected by reCAPTCHA and the Google <br class="bdt-visible@s"> <a href="https://policies.google.com/privacy">Privacy Policy</a> and <a href="https://policies.google.com/terms">Terms of Service</a> apply. </div> </div> </div> </div> <?php } protected function render_text() { $settings = $this->parent->get_settings_for_display(); $icon_align = $this->get_instance_value( 'modal_button_icon_align' ); $this->parent->add_render_attribute( 'button-icon', 'class', [ 'bdt-modal-button-icon', 'elementor-button-icon', 'elementor-align-icon-' . esc_attr( $icon_align ) ] ); if ( is_user_logged_in() && ! Element_Pack_Loader::elementor()->editor->is_edit_mode() ) { $button_text = esc_html__( 'Logout', 'bdthemes-element-pack' ); } else { $button_text = $this->get_instance_value( 'modal_button_text' ); } if ( ! isset ( $settings['modal_button_icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $settings['modal_button_icon'] = 'fas fa-user'; } $migrated = isset ( $settings['__fa4_migrated']['user_login_modal_icon'] ); $is_new = empty ( $settings['modal_button_icon'] ) && Icons_Manager::is_migration_allowed(); $user_login_modal_icon[] = $this->get_instance_value( 'user_login_modal_icon' ); $icon_visible = $this->get_instance_value( 'modal_btn_icon_only_on_mobile' ); ?> <span class="elementor-button-content-wrapper"> <?php if ( ! empty ( $user_login_modal_icon['value'] ) ) : ?> <span <?php $this->parent->print_render_attribute_string( 'button-icon' ); ?>> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $user_login_modal_icon, [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $settings['modal_button_icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </span> <?php else : ?> <?php if ( $icon_visible ) : ?> <?php $this->parent->add_render_attribute( 'button-icon', 'class', [ 'bdt-hidden@l' ] ); ?> <span <?php $this->parent->print_render_attribute_string( 'button-icon' ); ?>> <i class="ep-icon-lock" aria-hidden="true"></i> </span> <?php endif; ?> <?php endif; ?> <?php $text_visible = ( $this->get_instance_value( 'modal_btn_icon_only_on_mobile' ) ) ? ' bdt-visible@l' : ''; ?> <span class="elementor-button-text<?php echo esc_attr( $text_visible ); ?>"> <?php echo esc_html( $button_text ); ?> </span> </span> <?php } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'User Login', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\TestimonialCarousel\Widgets; use Elementor\Controls_Manager; use Elementor\Group_Control_Background; use Elementor\Group_Control_Border; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Typography; use ElementPack\Base\Module_Base; use ElementPack\Includes\Controls\GroupQuery\Group_Control_Query; use ElementPack\Modules\TestimonialCarousel\Skins; use ElementPack\Traits\Global_Swiper_Controls; use ElementPack\Traits\Global_Widget_Controls; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Testimonial_Carousel extends Module_Base { use Group_Control_Query; use Global_Widget_Controls; use Global_Swiper_Controls; private $_query = null; public function get_name() { return 'bdt-testimonial-carousel'; } public function get_title() { return BDTEP . esc_html__( 'Testimonial Carousel', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-testimonial-carousel'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'testimonial', 'carousel' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-styles' ]; } else { return [ 'ep-font', 'ep-testimonial-carousel' ]; } } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-scripts' ]; } else { return [ 'ep-testimonial-carousel' ]; } } public function get_custom_help_url() { return 'https://youtu.be/VbojVJzayvE'; } public function get_query() { return $this->_query; } protected function register_skins() { $this->add_skin( new Skins\Skin_Twyla( $this ) ); $this->add_skin( new Skins\Skin_Vyxo( $this ) ); } protected function register_controls() { $slides_per_view = range( 1, 10 ); $slides_per_view = array_combine( $slides_per_view, $slides_per_view ); $this->start_controls_section( 'section_content_layout', [ 'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ), ] ); $this->add_control( 'layout_style', [ 'label' => esc_html__( 'Layout Style', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SELECT, 'default' => 'style-1', 'options' => [ 'style-1' => '01', 'style-2' => '02', 'style-3' => '03', ], 'condition' => [ '_skin' => 'bdt-twyla', ], ] ); $this->add_responsive_control( 'columns', [ 'label' => esc_html__( 'Columns', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => '3', 'tablet_default' => '2', 'mobile_default' => '1', 'options' => [ '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', ], 'frontend_available' => true, ] ); $this->add_control( 'item_gap', [ 'label' => __( 'Item Gap', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 35, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], ] ); $this->add_control( 'show_image', [ 'label' => esc_html__( 'Testimonial Image', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before', ] ); $this->add_control( 'show_title', [ 'label' => esc_html__( 'Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_address', [ 'label' => esc_html__( 'Address', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'meta_multi_line', [ 'label' => esc_html__( 'Meta Multiline', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_comma', [ 'label' => esc_html__( 'Show Comma After Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'show_text', [ 'label' => esc_html__( 'Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before', ] ); $this->add_control( 'text_limit', [ 'label' => esc_html__( 'Text Limit', 'bdthemes-element-pack' ), 'description' => esc_html__( 'It\'s just work for main content, but not working with excerpt. If you set 0 so you will get full main content.', 'bdthemes-element-pack' ), 'type' => Controls_Manager::NUMBER, 'default' => 40, 'condition' => [ 'show_text' => 'yes', ], ] ); $this->add_control( 'strip_shortcode', [ 'label' => esc_html__( 'Strip Shortcode', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'show_text' => 'yes', ], ] ); $this->add_control( 'show_rating', [ 'label' => esc_html__( 'Rating', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before', ] ); $this->add_control( 'rating_bullet', [ 'label' => esc_html__( 'Rating Bullet', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'prefix_class' => 'bdt-rating-bullet--', 'render_type' => 'template', 'condition' => [ 'show_rating' => 'yes', // '_skin' => 'bdt-vyxo', ], ] ); $this->add_control( 'rating_position', [ 'label' => esc_html__( 'Rating Position', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SELECT, 'default' => 'bottom', 'options' => [ 'top' => __( 'Top', 'bdthemes-element-pack' ), 'bottom' => __( 'Bottom', 'bdthemes-element-pack' ), ], 'condition' => [ 'show_rating' => 'yes', '_skin' => 'bdt-vyxo', ], ] ); $this->add_control( 'show_review_platform', [ 'label' => esc_html__( 'Review Platform', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'separator' => 'before', ] ); $this->add_responsive_control( 'content_alignment', [ 'label' => esc_html__( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item-wrapper, {{WRAPPER}} .bdt-testimonial-carousel.skin-vyxo .bdt-testimonial-carousel-item' => 'text-align: {{VALUE}}', ], ] ); $this->add_control( 'item_match_height', [ 'label' => esc_html__( 'Item Match Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'render_type' => 'template', ] ); $this->end_controls_section(); //New Query Builder Settings $this->start_controls_section( 'section_post_query_builder', [ 'label' => __( 'Query', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->register_query_builder_controls(); $this->update_control( 'posts_source', [ 'label' => __( 'Source', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HIDDEN, 'options' => $this->getGroupControlQueryPostTypes(), 'default' => 'bdthemes-testimonial', ] ); $this->update_control( 'posts_per_page', [ 'default' => 10, ] ); $this->end_controls_section(); //Navigation Controls $this->start_controls_section( 'section_content_navigation', [ 'label' => __( 'Navigation', 'bdthemes-element-pack' ), ] ); //Global Navigation Controls $this->register_navigation_controls(); $this->end_controls_section(); //Global Carousel Settings Controls $this->register_carousel_settings_controls(); //Style $this->start_controls_section( 'section_style_item', [ 'label' => esc_html__( 'Items', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); // content padding $this->add_responsive_control( 'content_padding', [ 'label' => esc_html__( 'Content Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .skin-twyla .bdt-twyla-content-wrap' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ '_skin' => 'bdt-twyla', ], ] ); $this->start_controls_tabs( 'tabs_item_style' ); $this->start_controls_tab( 'tab_item_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'item_background', [ 'label' => esc_html__( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item-wrapper' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'item_border', 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item', 'separator' => 'before', ] ); $this->add_responsive_control( 'item_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item, {{WRAPPER}} .bdt-testimonial-carousel .swiper-carousel' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_shadow', 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item', ] ); $this->add_responsive_control( 'item_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'shadow_mode', [ 'label' => esc_html__( 'Shadow Mode', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'prefix_class' => 'bdt-ep-shadow-mode-', ] ); $this->add_control( 'shadow_color', [ 'label' => esc_html__( 'Shadow Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'shadow_mode' => 'yes', ], 'selectors' => [ '{{WRAPPER}} .elementor-widget-container:before' => is_rtl() ? 'background: linear-gradient(to left, {{VALUE}} 5%,rgba(255,255,255,0) 100%);' : 'background: linear-gradient(to right, {{VALUE}} 5%,rgba(255,255,255,0) 100%);', '{{WRAPPER}} .elementor-widget-container:after' => is_rtl() ? 'background: linear-gradient(to left, rgba(255,255,255,0) 0%, {{VALUE}} 95%);' : 'background: linear-gradient(to right, rgba(255,255,255,0) 0%, {{VALUE}} 95%);', ], ] ); $this->add_control( 'item_opacity', [ 'label' => esc_html__( 'Opacity', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'step' => 0.1, 'max' => 1, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item' => 'opacity: {{SIZE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_item_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'item_hover_background', [ 'label' => esc_html__( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item-wrapper:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'item_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'item_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item:hover' => 'border-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_hover_shadow', 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item:hover', ] ); $this->add_responsive_control( 'item_shadow_padding', [ 'label' => __( 'Match Padding', 'bdthemes-element-pack' ), 'description' => __( 'You have to add padding for matching overlaping hover shadow', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'step' => 1, 'max' => 50, ], ], 'default' => [ 'size' => 10, ], 'selectors' => [ '{{WRAPPER}} .swiper-carousel' => 'padding: {{SIZE}}{{UNIT}}; margin: 0 -{{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'item_hover_opacity', [ 'label' => esc_html__( 'Opacity', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'step' => 0.1, 'max' => 1, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item:hover' => 'opacity: {{SIZE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_item_active', [ 'label' => __( 'Active', 'bdthemes-element-pack' ) . BDTEP_NC, ] ); $this->add_control( 'item_active_background', [ 'label' => __( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item.swiper-slide-active .bdt-testimonial-carousel-item-wrapper' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'item_active_border_color', [ 'label' => __( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'item_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item.swiper-slide-active' => 'border-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_active_shadow', 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item.swiper-slide-active', ] ); $this->add_control( 'item_active_opacity', [ 'label' => esc_html__( 'Opacity', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'step' => 0.1, 'max' => 1, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-item.swiper-slide-active' => 'opacity: {{SIZE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_image', [ 'label' => esc_html__( 'Image', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_image' => 'yes', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'image_background_color', 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-img-wrapper', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'image_border', 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-img-wrapper', 'separator' => 'before', ] ); $this->add_control( 'image_hover_border_color', [ 'label' => esc_html__( 'Hover Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'image_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-img-wrapper:hover' => 'border-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'image_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-img-wrapper, {{WRAPPER}} .bdt-testimonial-carousel-img-wrapper img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_responsive_control( 'image_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-img-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'image_box_shadow', 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-img-wrapper', ] ); $this->add_responsive_control( 'image_size', [ 'label' => esc_html__( 'Size', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-img-wrapper' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};', ], 'separator' => 'before', ] ); $this->add_responsive_control( 'image_offset', [ 'label' => esc_html__( 'Vertical Spacing', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -100, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-img-wrapper' => 'transform: translateY({{SIZE}}px);', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_title', [ 'label' => esc_html__( 'Title', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_title' => 'yes', ], ] ); $this->add_control( 'title_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-title' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'title_active_color', [ 'label' => esc_html__( 'Active Color', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .swiper-slide-active .bdt-testimonial-carousel-title' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-title', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_address', [ 'label' => esc_html__( 'Address', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_address' => 'yes', ], ] ); $this->add_control( 'address_color', [ 'label' => esc_html__( 'Company Name/Address Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-address' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'address_active_color', [ 'label' => esc_html__( 'Company Name/Address Active Color', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .swiper-slide-active .bdt-testimonial-carousel-address' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'address_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel-address' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'address_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-address', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_text', [ 'label' => esc_html__( 'Text', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_text' => 'yes', ], ] ); $this->add_control( 'text_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-text' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'text_active_color', [ 'label' => esc_html__( 'Active Color', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .swiper-slide-active .bdt-testimonial-carousel-text' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'text_top_border_color', [ 'label' => esc_html__( 'Top Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-text' => 'border-top-color: {{VALUE}};', ], 'condition' => [ '_skin' => '', ], ] ); $this->add_control( 'active_text_top_border_color', [ 'label' => esc_html__( 'Top Border Active Color', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .swiper-slide-active .bdt-testimonial-carousel-text' => 'border-top-color: {{VALUE}};', ], 'condition' => [ '_skin' => '', ], ] ); $this->add_responsive_control( 'text_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-text' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'text_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-text' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'text_typography', 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-text', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_rating', [ 'label' => esc_html__( 'Rating', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_rating' => 'yes', ], ] ); $this->add_control( 'original_color', [ 'label' => esc_html__( 'Enable Original Color', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'show_review_platform' => 'yes', ], ] ); $this->add_control( 'rating_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#e7e7e7', 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-rating .bdt-rating-item' => 'color: {{VALUE}};', ], 'condition' => [ 'original_color' => '', ], ] ); $this->add_control( 'active_rating_color', [ 'label' => esc_html__( 'Active Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#FFCC00', 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-rating.bdt-rating-1 .bdt-rating-item:nth-child(1)' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-testimonial-carousel .bdt-rating.bdt-rating-2 .bdt-rating-item:nth-child(-n+2)' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-testimonial-carousel .bdt-rating.bdt-rating-3 .bdt-rating-item:nth-child(-n+3)' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-testimonial-carousel .bdt-rating.bdt-rating-4 .bdt-rating-item:nth-child(-n+4)' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-testimonial-carousel .bdt-rating.bdt-rating-5 .bdt-rating-item:nth-child(-n+5)' => 'color: {{VALUE}};', ], 'condition' => [ 'original_color' => '', ], ] ); $this->add_responsive_control( 'rating_size', [ 'label' => esc_html__( 'Size', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .elementor-widget-container .bdt-rating .bdt-rating-item' => 'font-size: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'rating_spacing', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .elementor-widget-container .bdt-rating .bdt-rating-item' => 'margin-right: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'rating_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-rating' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_review_platform', [ 'label' => __( 'Review Platform', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_review_platform' => 'yes', ], ] ); $this->start_controls_tabs( 'tabs_platform_style' ); $this->start_controls_tab( 'tab_platform_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'platform_text_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-review-platform i' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'platform_background_color', 'selector' => '{{WRAPPER}} .bdt-review-platform', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'platform_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-review-platform', ] ); $this->add_responsive_control( 'platform_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-review-platform' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'platform_text_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-review-platform' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'platform_text_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-review-platform' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'platform_shadow', 'selector' => '{{WRAPPER}} .bdt-review-platform', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'platform_typography', 'selector' => '{{WRAPPER}} .bdt-review-platform', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_platform_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'platform_hover_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-review-platform:hover i' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'platform_background_hover_color', 'selector' => '{{WRAPPER}} .bdt-review-platform:hover', ] ); $this->add_control( 'platform_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'platform_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-review-platform:hover' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_quatation', [ 'label' => esc_html__( 'Quatation', 'bdthemes-element-pack' ) . BDTEP_NC, 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => 'bdt-twyla', 'layout_style!' => 'style-1', ], ] ); $this->add_control( 'quatation_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .skin-twyla .testimonial-item-header::after' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'quatation_background_color', 'selector' => '{{WRAPPER}} .skin-twyla .testimonial-item-header::after', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'quatation_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .skin-twyla .testimonial-item-header::after', ] ); $this->add_responsive_control( 'quatation_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .skin-twyla .testimonial-item-header::after' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'testimonial_quatation_size', [ 'label' => esc_html__( 'Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 10, 'max' => 500, ], ], 'selectors' => [ '{{WRAPPER}} .skin-twyla .testimonial-item-header::after' => 'height: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}}; line-height: calc(20px + {{SIZE}}{{UNIT}});', ], ] ); $this->add_responsive_control( 'quatation_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .skin-twyla .testimonial-item-header::after' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'quatation_typography', 'selector' => '{{WRAPPER}} .skin-twyla .testimonial-item-header::after', ] ); $this->add_control( 'quatation_offset_toggle', [ 'label' => __( 'Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::POPOVER_TOGGLE, 'label_off' => __( 'None', 'bdthemes-element-pack' ), 'label_on' => __( 'Custom', 'bdthemes-element-pack' ), 'return_value' => 'yes', 'separator' => 'before', ] ); $this->start_popover(); $this->add_responsive_control( 'quatation_horizontal_offset', [ 'label' => __( 'Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -300, 'step' => 2, 'max' => 300, ], ], 'condition' => [ 'quatation_offset_toggle' => 'yes' ], 'render_type' => 'ui', 'selectors' => [ '{{WRAPPER}}' => '--ep-testimonial-carousel-quatation-h-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'quatation_vertical_offset', [ 'label' => __( 'Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -300, 'step' => 2, 'max' => 300, ], ], 'condition' => [ 'quatation_offset_toggle' => 'yes' ], 'render_type' => 'ui', 'selectors' => [ '{{WRAPPER}}' => '--ep-testimonial-carousel-quatation-v-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'quatation_rotate', [ 'label' => esc_html__( 'Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -360, 'max' => 360, 'step' => 5, ], ], 'condition' => [ 'quatation_offset_toggle' => 'yes' ], 'render_type' => 'ui', 'selectors' => [ '{{WRAPPER}}' => '--ep-testimonial-carousel-quatation-rotate: {{SIZE}}deg;' ], ] ); $this->end_popover(); $this->end_controls_section(); //Navigation Style $this->start_controls_section( 'section_style_navigation', [ 'label' => __( 'Navigation', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'conditions' => [ 'relation' => 'or', 'terms' => [ [ 'name' => 'navigation', 'operator' => '!=', 'value' => 'none', ], [ 'name' => 'show_scrollbar', 'value' => 'yes', ], ], ], ] ); //Global Navigation Style Controls $this->register_navigation_style_controls( 'swiper-carousel' ); $this->end_controls_section(); } public function render_review_platform( $post_id ) { $settings = $this->get_settings_for_display(); if ( ! $settings['show_review_platform'] ) { return; } $platform = get_post_meta( $post_id, 'bdthemes_tm_platform', true ); $review_link = get_post_meta( $post_id, 'bdthemes_tm_review_link', true ); if ( ! $platform ) { $platform = 'self'; } if ( ! $review_link ) { $review_link = '#'; } ?> <a href="<?php echo esc_url( $review_link ); ?>" class="bdt-review-platform bdt-flex-inline" bdt-tooltip="<?php echo wp_kses_post( $platform ); ?>"> <i class="ep-icon-<?php echo esc_attr( strtolower( $platform ) ); ?> bdt-platform-icon bdt-flex bdt-flex-middle bdt-flex-center" aria-hidden="true"></i> </a> <?php } public function render_image( $image_id ) { $settings = $this->get_settings_for_display(); if ( 'yes' != $settings['show_image'] ) { return; } $testimonial_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $image_id ), 'medium' ); if ( ! $testimonial_thumb ) { $testimonial_thumb = BDTEP_ASSETS_URL . 'images/member.svg'; } else { $testimonial_thumb = $testimonial_thumb[0]; } ?> <div class="bdt-width-auto bdt-flex bdt-position-relative"> <div class="bdt-testimonial-carousel-img-wrapper bdt-overflow-hidden bdt-border-circle bdt-background-cover"> <img src="<?php echo esc_url( $testimonial_thumb ); ?>" alt="<?php echo esc_attr( get_the_title() ); ?>" /> </div> <?php $this->render_review_platform( get_the_ID() ); ?> </div> <?php } public function render_title( $post_id ) { $settings = $this->get_settings_for_display(); if ( 'yes' != $settings['show_title'] ) { return; } ?> <h4 class="bdt-testimonial-carousel-title bdt-margin-remove-bottom" itemprop="name"> <?php echo esc_attr( get_the_title( $post_id ) ); ?> <?php if ( $settings['show_comma'] ) { echo ( ( $settings['show_title'] ) and ( $settings['show_address'] ) ) ? ', ' : ''; } ?> </h4> <?php } public function render_address( $post_id ) { $settings = $this->get_settings_for_display(); if ( ! $settings['show_address'] ) { return; } ?> <p class="bdt-testimonial-carousel-address bdt-text-meta"> <?php echo wp_kses_post(get_post_meta( $post_id, 'bdthemes_tm_company_name', true )); ?> </p> <?php } public function render_excerpt() { if ( ! $this->get_settings( 'show_text' ) ) { return; } $strip_shortcode = $this->get_settings_for_display( 'strip_shortcode' ); ?> <div class="bdt-testimonial-carousel-text" itemprop="description"> <?php if ( has_excerpt() ) { the_excerpt(); } else { echo wp_kses_post(element_pack_custom_excerpt( $this->get_settings_for_display( 'text_limit' ), $strip_shortcode )); } ?> </div> <?php } public function render_rating( $post_id ) { $settings = $this->get_settings_for_display(); if ( 'yes' != $settings['show_rating'] ) { return; } ?> <meta itemprop="datePublished" content="<?php echo esc_html( get_the_date() ); ?>"> <ul class="bdt-rating bdt-rating-<?php echo wp_kses_post( get_post_meta( $post_id, 'bdthemes_tm_rating', true ) ); ?> bdt-grid bdt-grid-collapse" data-bdt-grid itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating"> <li class="bdt-rating-item"><i class="ep-icon-star-full" aria-hidden="true"></i></li> <li class="bdt-rating-item"><i class="ep-icon-star-full" aria-hidden="true"></i></li> <li class="bdt-rating-item"><i class="ep-icon-star-full" aria-hidden="true"></i></li> <li class="bdt-rating-item"><i class="ep-icon-star-full" aria-hidden="true"></i></li> <li class="bdt-rating-item"><i class="ep-icon-star-full" aria-hidden="true"></i></li> </ul> <meta itemprop="worstRating" content="1"> <meta itemprop="ratingValue" content="<?php echo wp_kses_post( get_post_meta( $post_id, 'bdthemes_tm_rating', true ) ); ?>"> <meta itemprop="bestRating" content="5"> <?php } public function render_header( $skin = 'default' ) { $settings = $this->get_settings_for_display(); //Global Function $this->render_swiper_header_attribute( 'testimonial-carousel' ); $this->add_render_attribute( 'carousel', 'class', 'bdt-testimonial-carousel bdt-testimonials-twyla-' . $settings['layout_style'] . ' skin-' . $skin ); if ( 'yes' == $settings['item_match_height'] ) { $this->add_render_attribute( 'carousel', 'data-bdt-height-match', 'target: > div > div > div > div > .bdt-testimonial-carousel-text' ); } ?> <div <?php $this->print_render_attribute_string( 'carousel' ); ?>> <div <?php $this->print_render_attribute_string( 'swiper' ); ?>> <div class="swiper-wrapper"> <?php } public function render_query( $posts_per_page ) { $args = []; $args['posts_per_page'] = $posts_per_page; $args['paged'] = max( 1, get_query_var( 'paged' ), get_query_var( 'page' ) ); $default = $this->getGroupControlQueryArgs(); $args = array_merge( $default, $args ); return $this->_query = new \WP_Query( $args ); } public function render_loop_item() { $settings = $this->get_settings_for_display(); // TODO need to delete after v6.5 if ( isset( $settings['posts'] ) and $settings['posts_per_page'] == 10 ) { $limit = $settings['posts']; } else { $limit = $settings['posts_per_page']; } $wp_query = $this->render_query( $limit ); if ( $wp_query->have_posts() ) { while ( $wp_query->have_posts() ) : $wp_query->the_post(); $platform = get_post_meta( get_the_ID(), 'bdthemes_tm_platform', true ); ?> <div class="swiper-slide bdt-testimonial-carousel-item bdt-review-<?php echo esc_attr( strtolower( $platform ) ); ?>" itemprop="review" itemscope itemtype="http://schema.org/Review"> <div class="bdt-testimonial-carousel-item-wrapper"> <div class="testimonial-item-header"> <div class="bdt-grid bdt-grid-small bdt-flex-middle" data-bdt-grid> <?php $this->render_image( get_the_ID() ); if ( $settings['show_rating'] || $settings['show_text'] || $settings['show_address'] ) : ?> <div class="bdt-width-expand"> <div class="bdt-testimonial-meta <?php echo ( $settings['meta_multi_line'] ) ? '' : 'bdt-meta-multi-line'; ?>"> <?php $this->render_title( get_the_ID() ); $this->render_address( get_the_ID() ); if ( $settings['show_rating'] && ( 'yes' != $settings['show_text'] ) ) : ?> <div class="bdt-testimonial-carousel-rating bdt-margin-small-top bdt-padding-remove"> <?php $this->render_rating( get_the_ID() ); ?> </div> <?php endif; ?> </div> </div> <?php endif; ?> </div> </div> <?php $this->render_excerpt(); ?> <?php if ( $settings['show_rating'] && $settings['show_text'] ) : ?> <div class="bdt-testimonial-carousel-rating"> <?php $this->render_rating( get_the_ID() ); ?> </div> <?php endif; ?> </div> </div> <?php endwhile; wp_reset_postdata(); } else { echo '<div class="bdt-alert-warning" bdt-alert>Oppps!! There is no post, please select actual post or categories.<div>'; } } public function render() { $this->render_header(); $this->render_loop_item(); $this->render_footer(); } } <?php namespace ElementPack\Modules\TestimonialCarousel; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'testimonial-carousel'; } public function get_widgets() { $widgets = ['Testimonial_Carousel']; return $widgets; } } <?php namespace ElementPack\Modules\TestimonialCarousel\Skins; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Border; use Elementor\Group_Control_Background; use Elementor\Skin_Base as Elementor_Skin_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Skin_Vyxo extends Elementor_Skin_Base { public function get_id() { return 'bdt-vyxo'; } public function get_title() { return __('Vyxo', 'bdthemes-element-pack'); } public function _register_controls_actions() { parent::_register_controls_actions(); add_action('elementor/element/bdt-testimonial-carousel/section_style_text/after_section_start', [$this, 'register_vyxo_style_controls']); } public function register_vyxo_style_controls(Module_Base $widget) { $this->parent = $widget; $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'text_background_color', 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel .bdt-testimonial-carousel-text-wrap', // 'separator' => 'after', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'text_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel-text-wrap', ] ); $this->add_responsive_control( 'text_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel-text-wrap' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'text_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel-text-wrap' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'text_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-testimonial-carousel-text-wrap' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'text_box_shadow', 'selector' => '{{WRAPPER}} .bdt-testimonial-carousel-text-wrap', ] ); $this->add_control( 'text_divider', [ 'label' => esc_html__('Divider', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIVIDER, ] ); } public function render() { $settings = $this->parent->get_settings(); // TODO need to delete after v6.5 if (isset($settings['posts']) and $settings['posts_per_page'] == 10) { $limit = $settings['posts']; } else { $limit = $settings['posts_per_page']; } $wp_query = $this->parent->render_query($limit); if ($wp_query->have_posts()) : ?> <?php $this->parent->render_header('vyxo'); ?> <?php while ($wp_query->have_posts()) : $wp_query->the_post(); $platform = get_post_meta(get_the_ID(), 'bdthemes_tm_platform', true); ?> <div class="swiper-slide bdt-testimonial-carousel-item bdt-review-<?php echo esc_attr(strtolower($platform)); ?>"> <div class="bdt-testimonial-carousel-text-wrap bdt-padding bdt-background-primary"> <?php if ($settings['rating_position'] == 'top') : ?> <?php if ($settings['show_rating']) : ?> <div class="bdt-testimonial-carousel-rating bdt-display-inline-block"> <?php $this->parent->render_rating(get_the_ID()); ?> </div> <?php endif; ?> <?php endif; ?> <?php $this->parent->render_excerpt(); ?> </div> <div class="bdt-testimonial-carousel-item-wrapper"> <div class="testimonial-item-header bdt-position-top-center"> <?php $this->parent->render_image(get_the_ID()); ?> </div> <div class="bdt-testimonial-meta <?php echo ($settings['meta_multi_line']) ? '' : 'bdt-meta-multi-line'; ?>"> <?php $this->parent->render_title(get_the_ID()); $this->parent->render_address(get_the_ID()); ?> </div> <?php if ($settings['rating_position'] == 'bottom') : ?> <?php if ($settings['show_rating']) : ?> <div class="bdt-testimonial-carousel-rating bdt-display-inline-block"> <?php $this->parent->render_rating(get_the_ID()); ?> </div> <?php endif; ?> <?php endif; ?> </div> </div> <?php endwhile; wp_reset_postdata(); ?> <?php $this->parent->render_footer(); endif; } } <?php namespace ElementPack\Modules\TestimonialCarousel\Skins; use Elementor\Skin_Base as Elementor_Skin_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Skin_Twyla extends Elementor_Skin_Base { public function get_id() { return 'bdt-twyla'; } public function get_title() { return __('Twyla', 'bdthemes-element-pack'); } public function render() { $settings = $this->parent->get_settings(); // TODO need to delete after v6.5 if (isset($settings['posts']) and $settings['posts_per_page'] == 10) { $limit = $settings['posts']; } else { $limit = $settings['posts_per_page']; } $wp_query = $this->parent->render_query($limit); if ($wp_query->have_posts()) : ?> <?php $this->parent->render_header('twyla'); ?> <?php while ($wp_query->have_posts()) : $wp_query->the_post(); $platform = get_post_meta(get_the_ID(), 'bdthemes_tm_platform', true); ?> <div class="swiper-slide bdt-testimonial-carousel-item bdt-review-<?php echo esc_attr(strtolower($platform)); ?>"> <div class="bdt-testimonial-carousel-item-wrapper"> <div class="testimonial-item-header"> <?php $this->parent->render_image(get_the_ID()); ?> </div> <div class="bdt-twyla-content-wrap"> <?php $this->parent->render_excerpt(); ?> <div class="bdt-testimonial-meta <?php echo ($settings['meta_multi_line']) ? '' : 'bdt-meta-multi-line'; ?>"> <?php $this->parent->render_title(get_the_ID()); $this->parent->render_address(get_the_ID()); ?> </div> <?php if (($settings['show_rating']) && ($settings['show_text'])) : ?> <div class="bdt-testimonial-carousel-rating bdt-display-inline-block"> <?php $this->parent->render_rating(get_the_ID()); ?> </div> <?php endif; ?> </div> </div> </div> <?php endwhile; wp_reset_postdata(); ?> <?php $this->parent->render_footer(); endif; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Testimonial Carousel', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\GiveDonationHistory\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Border; use Elementor\Group_Control_Text_Shadow; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Give_Donation_History extends Module_Base { public function get_name() { return 'bdt-give-donation-history'; } public function get_title() { return BDTEP . __('Give Donation History', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-give-donation-history'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['give', 'charity', 'donation', 'donor', 'history']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-give-donation-history']; } } public function get_custom_help_url() { return 'https://youtu.be/n2Cnlubi-E8'; } public function register_controls() { $this->start_controls_section( 'donation_history_settings', [ 'label' => __('Donation History', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'form_id', [ 'label' => __('ID', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] ); $this->add_control( 'date', [ 'label' => __('Date', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] ); $this->add_control( 'donor', [ 'label' => __('Donor', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'amount', [ 'label' => __('Amount', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] ); $this->add_control( 'status', [ 'label' => __('Status', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'method', [ 'label' => __('Payment Method', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'section_style_header', [ 'label' => __('Header', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'header_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#e7ebef', 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table th' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'header_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#333', 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table th' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'header_border_style', [ 'label' => __('Border Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'none' => __('None', 'bdthemes-element-pack'), 'solid' => __('Solid', 'bdthemes-element-pack'), 'double' => __('Double', 'bdthemes-element-pack'), 'dotted' => __('Dotted', 'bdthemes-element-pack'), 'dashed' => __('Dashed', 'bdthemes-element-pack'), 'groove' => __('Groove', 'bdthemes-element-pack'), ], 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table th' => 'border-style: {{VALUE}};', ], ] ); $this->add_control( 'header_border_width', [ 'label' => __('Border Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 20, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table th' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'header_border_color', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#ccc', 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table th' => 'border-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'header_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'default' => [ 'top' => 1, 'bottom' => 1, 'left' => 1, 'right' => 2, 'unit' => 'em' ], 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table th' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'header_text_typography', 'selector' => '{{WRAPPER}} .bdt-give-donation-history .give-table th', ] ); $this->add_responsive_control( 'header_alignment', [ 'label' => esc_html__('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'default' => 'center', 'options' => [ 'left' => [ 'title' => esc_html__('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html__('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-right', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table th' => 'text-align: {{VALUE}}', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_body', [ 'label' => __('Body', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'cell_border_style', [ 'label' => __('Border Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'none' => __('None', 'bdthemes-element-pack'), 'solid' => __('Solid', 'bdthemes-element-pack'), 'double' => __('Double', 'bdthemes-element-pack'), 'dotted' => __('Dotted', 'bdthemes-element-pack'), 'dashed' => __('Dashed', 'bdthemes-element-pack'), 'groove' => __('Groove', 'bdthemes-element-pack'), ], 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table td' => 'border-style: {{VALUE}};', ], ] ); $this->add_control( 'cell_border_width', [ 'label' => __('Border Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 20, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table td' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'cell_padding', [ 'label' => __('Cell Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'default' => [ 'top' => 1, 'bottom' => 1, 'left' => 2, 'right' => 2, 'unit' => 'em' ], 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table td' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'body_text_typography', 'selector' => '{{WRAPPER}} .bdt-give-donation-history .give-table td', ] ); $this->add_responsive_control( 'body_alignment', [ 'label' => esc_html__('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'default' => 'center', 'options' => [ 'left' => [ 'title' => esc_html__('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html__('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-right', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table td' => 'text-align: {{VALUE}}', ], ] ); $this->start_controls_tabs('tabs_body_style'); $this->start_controls_tab( 'tab_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'normal_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#fff', 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table td' => 'background-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'normal_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table td' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'normal_border_color', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#ccc', 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table td' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_hover', [ 'label' => __('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'row_hover_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table tr:hover td' => 'background-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'row_hover_text_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table tr:hover td' => 'color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_stripe', [ 'label' => __('Stripe', 'bdthemes-element-pack'), ] ); $this->add_control( 'stripe_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#f5f5f5', 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table tr:nth-child(even) td' => 'background-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'stripe_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table tr:nth-child(even) td' => 'color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_link_text', [ 'label' => __('Link', 'bdthemes-element-pack'), ] ); $this->add_control( 'link_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table td a' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'link_hover_color', [ 'label' => __('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-donation-history .give-table td a:hover' => 'color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } private function get_shortcode() { $settings = $this->get_settings_for_display(); if (!$settings['form_id']) { return '<div class="bdt-alert bdt-alert-warning">' . __('Please select a Give Forms From Setting!', 'bdthemes-element-pack') . '</div>'; } $attributes = [ 'id' => $settings['form_id'], 'donor' => $settings['donor'], 'date' => $settings['date'], 'amount' => $settings['amount'], 'status' => $settings['status'], 'payment_method' => $settings['method'], ]; $this->add_render_attribute('shortcode', $attributes); $shortcode = []; $shortcode[] = sprintf('[donation_history %s]', $this->get_render_attribute_string('shortcode')); return implode("", $shortcode); } public function render() { $this->add_render_attribute('give_wrapper', 'class', 'bdt-give-donation-history'); ?> <div <?php $this->print_render_attribute_string('give_wrapper'); ?>> <?php echo do_shortcode($this->get_shortcode()); ?> </div> <?php } public function render_plain_content() { echo wp_kses_post($this->get_shortcode()); } } <?php namespace ElementPack\Modules\GiveDonationHistory; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'give-donation-history'; } public function get_widgets() { $widgets = ['Give_Donation_History']; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Give Donation History', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, 'has_style' => true, ]; <?php namespace ElementPack\Modules\FluentForms\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Background; use Elementor\Group_Control_Box_Shadow; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Fluent_Forms extends Module_Base { public function get_name() { return 'bdt-fluent-forms'; } public function get_title() { return BDTEP . esc_html__( 'Fluent Forms', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-fluent-forms'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'fluent', 'ninja', 'form', 'contact', 'custom', 'builder' ]; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return [ 'ep-fluent-forms' ]; } } public function get_custom_help_url() { return 'https://youtu.be/BWPuKe4PfQ4'; } protected function register_controls() { $this->start_controls_section( 'section_content_layout', [ 'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ), ] ); $this->add_control( 'fluent_form', [ 'label' => esc_html__( 'Select Form', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'options' => element_pack_fluent_forms_options(), ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'section_label_style', [ 'label' => __('Labels', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'text_color_label', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group label' => 'color: {{VALUE}}', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'typography_label', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-fluent-forms .ff-el-group label', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_fields_style', [ 'label' => __('Input & Textarea', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'input_alignment', [ 'label' => __('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-right', ], ], 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea, {{WRAPPER}} .bdt-fluent-forms .ff-el-group select' => 'text-align: {{VALUE}};', ], ] ); $this->start_controls_tabs('tabs_fields_style'); $this->start_controls_tab( 'tab_fields_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'field_bg_color', [ 'label' => __('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea, {{WRAPPER}} .bdt-fluent-forms .ff-el-group select' => 'background-color: {{VALUE}}', ], ] ); $this->add_control( 'field_text_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea, {{WRAPPER}} .bdt-fluent-forms .ff-el-group select' => 'color: {{VALUE}}', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'field_border', 'label' => __('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea, {{WRAPPER}} .bdt-fluent-forms .ff-el-group select', 'separator' => 'before', ] ); $this->add_control( 'field_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea, {{WRAPPER}} .bdt-fluent-forms .ff-el-group select' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'field_text_indent', [ 'label' => __('Text Indent', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 60, 'step' => 1, ], '%' => [ 'min' => 0, 'max' => 30, 'step' => 1, ], ], 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea, {{WRAPPER}} .bdt-fluent-forms .ff-el-group select' => 'text-indent: {{SIZE}}{{UNIT}}', ], 'separator' => 'before', ] ); $this->add_responsive_control( 'input_width', [ 'label' => __('Input Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 1200, 'step' => 1, ], ], 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group select' => 'width: {{SIZE}}{{UNIT}}', ], ] ); $this->add_responsive_control( 'input_height', [ 'label' => __('Input Height', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 80, 'step' => 1, ], ], 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group select' => 'height: {{SIZE}}{{UNIT}}', ], ] ); $this->add_responsive_control( 'textarea_width', [ 'label' => __('Textarea Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 1200, 'step' => 1, ], ], 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea' => 'width: {{SIZE}}{{UNIT}}', ], ] ); $this->add_responsive_control( 'textarea_height', [ 'label' => __('Textarea Height', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 400, 'step' => 1, ], ], 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea' => 'height: {{SIZE}}{{UNIT}}', ], ] ); $this->add_responsive_control( 'field_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea, {{WRAPPER}} .bdt-fluent-forms .ff-el-group select' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'field_spacing', [ 'label' => __('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, 'step' => 1, ], ], 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group' => 'margin-bottom: {{SIZE}}{{UNIT}}', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'field_typography', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea, {{WRAPPER}} .bdt-fluent-forms .ff-el-group select', 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'field_box_shadow', 'selector' => '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]), {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea, {{WRAPPER}} .bdt-fluent-forms .ff-el-group select', 'separator' => 'before', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_fields_focus', [ 'label' => __('Focus', 'bdthemes-element-pack'), ] ); $this->add_control( 'field_bg_color_focus', [ 'label' => __('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]):focus, {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea:focus' => 'background-color: {{VALUE}}', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'focus_input_border', 'label' => __('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]):focus, {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea:focus', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'focus_box_shadow', 'selector' => '{{WRAPPER}} .bdt-fluent-forms input:not([type=radio]):not([type=checkbox]):not([type=submit]):not([type=button]):not([type=image]):not([type=file]):focus, {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea:focus', 'separator' => 'before', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_placeholder_style', [ 'label' => __('Placeholder', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'text_color_placeholder', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group input::-webkit-input-placeholder, {{WRAPPER}} .bdt-fluent-forms .ff-el-group textarea::-webkit-input-placeholder' => 'color: {{VALUE}}', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_radio_checkbox_style', [ 'label' => __('Radio & Checkbox', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'custom_radio_checkbox', [ 'label' => __('Custom Styles', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => __('Yes', 'bdthemes-element-pack'), 'label_off' => __('No', 'bdthemes-element-pack'), 'return_value' => 'yes', ] ); $this->add_responsive_control( 'radio_checkbox_size', [ 'label' => __('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => '15', 'unit' => 'px', ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 80, 'step' => 1, ], ], 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="checkbox"], {{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="radio"]' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}}', ], 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->start_controls_tabs('tabs_radio_checkbox_style'); $this->start_controls_tab( 'radio_checkbox_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->add_control( 'radio_checkbox_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="checkbox"], {{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="radio"]' => 'background: {{VALUE}}', ], 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->add_responsive_control( 'checkbox_border_width', [ 'label' => __('Border Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 15, 'step' => 1, ], ], 'size_units' => ['px'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="checkbox"], {{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="radio"]' => 'border-width: {{SIZE}}{{UNIT}}', ], 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->add_control( 'checkbox_border_color', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="checkbox"], {{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="radio"]' => 'border-color: {{VALUE}}', ], 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->add_control( 'checkbox_heading', [ 'label' => __('Checkbox', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->add_control( 'checkbox_border_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="checkbox"], {{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="checkbox"]:before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->add_control( 'radio_heading', [ 'label' => __('Radio Buttons', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->add_control( 'radio_border_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="radio"], {{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="radio"]:before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'radio_checkbox_checked', [ 'label' => __('Checked', 'bdthemes-element-pack'), 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->add_control( 'radio_checkbox_color_checked', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="checkbox"]:checked:before, {{WRAPPER}} .bdt-fluent-forms.bdt-custom-radio-checkbox input[type="radio"]:checked:before' => 'background: {{VALUE}}', ], 'condition' => [ 'custom_radio_checkbox' => 'yes', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_break_style', [ 'label' => __('Section Break Style', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'section_break_label', [ 'label' => __('Label', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING ] ); $this->add_control( 'section_break_label_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-section-break .ff-el-section-title' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'section_break_label_typography', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '.bdt-fluent-forms .ff-el-section-break .ff-el-section-title', 'separator' => 'before', ] ); $this->add_responsive_control( 'section_break_label_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-section-break .ff-el-section-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'section_break_label_margin', [ 'label' => __('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-section-break .ff-el-section-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'section_break_description', [ 'label' => __('Description', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] ); $this->add_control( 'section_break_description_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-section-break div' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'section_break_description_typography', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-fluent-forms .ff-el-section-break div', 'separator' => 'before', ] ); $this->add_responsive_control( 'section_break_description_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-section-break div' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'section_break_description_margin', [ 'label' => __('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-section-break div' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'section_break_alignment', [ 'label' => __('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-h-align-left', ], 'center' => [ 'title' => __('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-h-align-center', ], 'right' => [ 'title' => __('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-h-align-right', ], ], 'prefix_class' => 'bdt-fluentform-section-break-content-' ] ); $this->end_controls_section(); $this->start_controls_section( 'section_address_line_style', [ 'label' => __('Address Line Style', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'address_line_label_color', [ 'label' => __('Label Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .fluent-address label' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'address_line_label_typography', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .fluent-address label', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_submit_button_style', [ 'label' => __('Submit Button', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'button_align', [ 'label' => __('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-h-align-left', ], 'center' => [ 'title' => __('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-h-align-center', ], 'right' => [ 'title' => __('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-h-align-right', ], ], 'default' => '', 'prefix_class' => 'bdt-fluentform-form-button-', 'condition' => [ 'button_width_type' => 'custom', ], ] ); $this->add_control( 'button_width_type', [ 'label' => __('Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'custom', 'options' => [ 'full-width' => __('Full Width', 'bdthemes-element-pack'), 'custom' => __('Custom', 'bdthemes-element-pack'), ], 'prefix_class' => 'bdt-fluentform-form-button-', ] ); $this->add_responsive_control( 'button_width', [ 'label' => __('Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 1200, 'step' => 1, ], ], 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit' => 'width: {{SIZE}}{{UNIT}}', ], 'condition' => [ 'button_width_type' => 'custom', ], ] ); $this->start_controls_tabs('tabs_button_style'); $this->start_controls_tab( 'tab_button_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_bg_color_normal', [ 'label' => __('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#409EFF', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit' => 'background-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'button_text_color_normal', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#ffffff', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit' => 'color: {{VALUE}} !important;', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'button_border_normal', 'label' => __('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit', ] ); $this->add_control( 'button_border_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_margin', [ 'label' => __('Margin Top', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, 'step' => 1, ], ], 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit' => 'margin-top: {{SIZE}}{{UNIT}}', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'button_typography', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit', 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_box_shadow', 'selector' => '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit', 'separator' => 'before', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_button_hover', [ 'label' => __('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_bg_color_hover', [ 'label' => __('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit:hover' => 'background-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'button_text_color_hover', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit:hover' => 'color: {{VALUE}} !important;', ], ] ); $this->add_control( 'button_border_color_hover', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-el-group .ff-btn-submit:hover' => 'border-color: {{VALUE}}', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); // Style Tab: Success Message if( defined("FLUENTFORMPRO") ) { $this->start_controls_section( 'section_pagination_style', [ 'label' => __('Pagination', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('form_progressbar_style_tabs'); $this->start_controls_tab( 'form_progressbar_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'pagination_progressbar_label', [ 'label' => __('Label', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING ] ); $this->add_control( 'show_label', [ 'label' => __( 'Show Label', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'label_on' => __( 'Show', 'bdthemes-element-pack' ), 'label_off' => __( 'Hide', 'bdthemes-element-pack' ), 'return_value' => 'yes', 'default' => 'yes', 'prefix_class' => 'bdt-ff-step-header-' ] ); $this->add_control( 'label_color', [ 'label' => __( 'Label Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .ff-el-progress-status' => 'color: {{VALUE}}', ], 'condition' => [ 'show_label' => 'yes' ] ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'label_typography', 'label' => __( 'Typography', 'bdthemes-element-pack' ), 'selector' => '{{WRAPPER}} .ff-el-progress-status', 'condition' => [ 'show_label' => 'yes' ] ] ); $this->add_control( 'label_space', [ 'label' => __( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%', 'em' ], 'selectors' => [ '{{WRAPPER}} .ff-el-progress-status' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'show_label' => 'yes' ], 'separator' => 'after' ] ); $this->add_control( 'pagination_progressbar', [ 'label' => __('Progressbar', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, ] ); $this->add_control( 'show_progressbar', [ 'label' => __( 'Show Progressbar', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'label_on' => __( 'Show', 'bdthemes-element-pack' ), 'label_off' => __( 'Hide', 'bdthemes-element-pack' ), 'return_value' => 'yes', 'default' => 'yes', 'prefix_class' => 'bdt-ff-step-progressbar-' ] ); $this->add_control( 'progressbar_height', [ 'label' => __( 'Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px' ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, 'step' => 1, ] ], 'selectors' => [ '{{WRAPPER}} .ff-el-progress' => 'height: {{SIZE}}{{UNIT}};', ], 'condition' => [ 'show_progressbar' => 'yes' ] ] ); $this->add_control( 'progressbar_color', [ 'label' => __( 'Title Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .ff-el-progress-bar span' => 'color: {{VALUE}};', ], 'condition' => [ 'show_progressbar' => 'yes' ] ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'progressbar_border', 'label' => __( 'Border', 'bdthemes-element-pack' ), 'selector' => '{{WRAPPER}} .ff-el-progress', 'condition' => [ 'show_progressbar' => 'yes' ] ] ); $this->add_control( 'progressbar_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%', 'em' ], 'selectors' => [ '{{WRAPPER}} .ff-el-progress' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'show_progressbar' => 'yes' ] ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'progressbar_bg', 'label' => __( 'Background', 'bdthemes-element-pack' ), 'types' => [ 'classic', 'gradient' ], 'selector' => '{{WRAPPER}} .ff-el-progress', 'condition' => [ 'show_progressbar' => 'yes' ], 'exclude' => [ 'image' ] ] ); $this->end_controls_tab(); $this->start_controls_tab( 'form_progressbar_filled', [ 'label' => __('Filled', 'bdthemes-element-pack'), ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'progressbar_bg_filled', 'label' => __( 'Background', 'bdthemes-element-pack' ), 'types' => [ 'classic', 'gradient' ], 'selector' => '{{WRAPPER}} .ff-el-progress-bar', 'condition' => [ 'show_progressbar' => 'yes' ], 'exclude' => [ 'image' ] ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->start_controls_tabs( 'form_pagination_button_style_tabs', [ 'separator' => 'before' ] ); $this->start_controls_tab( 'form_pagination_button', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'pagination_button_style', [ 'label' => __('Button', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING ] ); $this->add_control( 'pagination_button_color', [ 'label' => __( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .step-nav button' => 'color: {{VALUE}};', ] ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'pagination_button_typography', 'label' => __( 'Typography', 'bdthemes-element-pack' ), 'selector' => '{{WRAPPER}} .step-nav button', ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'pagination_button_bg', 'label' => __( 'Background', 'bdthemes-element-pack' ), 'types' => [ 'classic', 'gradient' ], 'selector' => '{{WRAPPER}} .step-nav button', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'pagination_button_border', 'label' => __( 'Border', 'bdthemes-element-pack' ), 'selector' => '{{WRAPPER}} .step-nav button', ] ); $this->add_control( 'pagination_button_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%', 'em' ], 'selectors' => [ '{{WRAPPER}} .step-nav button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'pagination_button_padding', [ 'label' => __( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%', 'em' ], 'selectors' => [ '{{WRAPPER}} .step-nav button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'form_pagination_button_hover', [ 'label' => __('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'pagination_button_hover_color', [ 'label' => __( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .step-nav button:hover' => 'color: {{VALUE}};', ] ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'pagination_button_hover_bg', 'label' => __( 'Background', 'bdthemes-element-pack' ), 'types' => [ 'classic', 'gradient' ], 'selector' => '{{WRAPPER}} .step-nav button:hover', ] ); $this->add_control( 'pagination_button_border_hover_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%', 'em' ], 'selectors' => [ '{{WRAPPER}} .step-nav button:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } $this->start_controls_section( 'section_success_message_style', [ 'label' => __('Success Message', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'success_message_bg_color', [ 'label' => __('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-message-success' => 'background-color: {{VALUE}}', ], ] ); $this->add_control( 'success_message_text_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .ff-message-success' => 'color: {{VALUE}}', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'success_message_border', 'label' => __('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-fluent-forms .ff-message-success', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'success_message_typography', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-fluent-forms .ff-message-success', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_error_style', [ 'label' => __('Error', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'error_messages_heading', [ 'label' => __('Error Messages', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, 'condition' => [ 'error_messages' => 'show', ], ] ); $this->add_control( 'error_message_text_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .error.text-danger' => 'color: {{VALUE}}', ], 'condition' => [ 'error_messages' => 'show', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'error_message_typography', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-fluent-forms .error.text-danger', ] ); $this->add_responsive_control( 'error_message_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .error.text-danger' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'error_message_margin', [ 'label' => __('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-fluent-forms .error.text-danger' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); } private function get_shortcode() { $settings = $this->get_settings_for_display(); if (!$settings['fluent_form']) { return '<div class="bdt-alert bdt-alert-warning">'.__('Please select a Fluent Forms From Setting!', 'bdthemes-element-pack').'</div>'; } $attributes = [ 'id' => $settings['fluent_form'], ]; $this->add_render_attribute( 'shortcode', $attributes ); $shortcode = []; $shortcode[] = sprintf( '[fluentform %s]', $this->get_render_attribute_string( 'shortcode' ) ); return implode("", $shortcode); } public function render() { $settings = $this->get_settings_for_display(); $this->add_render_attribute( 'fluent_wrapper', 'class', 'bdt-fluent-forms' ); if ( $settings['custom_radio_checkbox'] == 'yes' ) { $this->add_render_attribute( 'fluent_wrapper', 'class', 'bdt-custom-radio-checkbox' ); } ?> <div <?php $this->print_render_attribute_string('fluent_wrapper'); ?>> <?php echo do_shortcode( $this->get_shortcode() ); ?> </div> <?php } public function render_plain_content() { echo wp_kses_post($this->get_shortcode()); } } <?php namespace ElementPack\Modules\FluentForms; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'fluent-forms'; } public function get_widgets() { $widgets = ['Fluent_Forms']; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Fluent Forms', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_style' => true, ]; <?php namespace ElementPack\Modules\Calendly\Widgets; use Elementor\Plugin; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Calendly extends Module_Base { public function get_name() { return 'bdt-calendly'; } public function get_title() { return BDTEP . esc_html__( 'Calendly', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-calendly'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'calendly', 'calender', 'booking', 'booked', 'appointment' ]; } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'calendly', 'ep-scripts' ]; } else { return [ 'calendly' ]; } } public function get_custom_help_url() { return 'https://youtu.be/nl4zC46SrhY'; } protected function register_controls() { $this->start_controls_section( 'section_calendly', [ 'label' => esc_html__( 'Calendly', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'calendly_username', [ 'label' => esc_html__( 'Username', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => '', 'placeholder' => esc_html__( 'Type calendly username here', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], 'render_type' => 'template' ] ); $this->add_control( 'calendly_time', [ 'label' => esc_html__( 'Select Time', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'options' => [ '15min' => esc_html__( '15 Minutes', 'bdthemes-element-pack' ), '30min' => esc_html__( '30 Minutes', 'bdthemes-element-pack' ), '60min' => esc_html__( '60 Minutes', 'bdthemes-element-pack' ), '' => esc_html__( 'All', 'bdthemes-element-pack' ), ], 'default' => '15min' ] ); $this->add_control( 'event_type_details', [ 'label' => esc_html__( 'Hide Event Type Details', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_responsive_control( 'height', [ 'label' => esc_html__( 'Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px', '%' ], 'range' => [ 'px' => [ 'min' => 10, 'max' => 1000, 'step' => 5, ], '%' => [ 'min' => 5, 'max' => 100, ], ], 'default' => [ 'unit' => 'px', 'size' => '680', ], 'selectors' => [ '{{WRAPPER}} .calendly-inline-widget' => 'height: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .calendly-wrapper' => 'height: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_calendly', [ 'label' => esc_html__( 'Calendly', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'calendly_pro_notice', [ 'type' => Controls_Manager::RAW_HTML, 'raw' => sprintf( esc_html__( 'Style option only works with %s. Basic plan user can\'t change the color style. For more details please %s.', 'bdthemes-element-pack' ), '<a href="https://calendly.com/pages/pricing" target="_blank">Calendly Pro plan</a>', '<a href="https://calendly.com/pages/pricing" target="_blank">check here</a>' ), 'content_classes' => 'elementor-panel-alert elementor-panel-alert-warning', ] ); $this->add_control( 'text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'alpha' => false, ] ); $this->add_control( 'button_link_color', [ 'label' => esc_html__( 'Button & Link Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'alpha' => false, ] ); $this->add_control( 'background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'alpha' => false, ] ); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); $calendly_time = $settings['calendly_time'] != '' ? "/{$settings['calendly_time']}" : ''; $calendly_event = ''; if ( 'yes' === $settings['event_type_details'] ) { $calendly_event = 'hide_event_type_details=1'; } $parameters = [ 'text_color' => ( $settings['text_color'] ) ? str_replace( '#', '', $settings['text_color'] ) : null, 'primary_color' => ( $settings['button_link_color'] ) ? str_replace( '#', '', $settings['button_link_color'] ) : null, 'background_color' => ( $settings['background_color'] ) ? str_replace( '#', '', $settings['background_color'] ) : null, ]; $requestUrl = 'https://calendly.com/'; $requestUrl .= esc_attr( $settings['calendly_username'] ); $requestUrl .= esc_attr( $calendly_time ); $requestUrl .= '/?'; $requestUrl .= esc_attr( $calendly_event ); $final_url = $requestUrl . http_build_query( $parameters ); ?> <?php if ( $settings['calendly_username'] ) : ?> <div class="calendly-inline-widget" data-url="<?php echo esc_url( $final_url ); ?>" style="min-width:320px;"></div> <script type="text/javascript" src="https://assets.calendly.com/assets/external/widget.js"></script> <?php if ( Plugin::$instance->editor->is_edit_mode() ) : ?> <div class="calendly-wrapper" style="width:100%; position:absolute; top:0; left:0; z-index:100;"></div> <?php endif; ?> <?php endif; ?> <?php } } <?php namespace ElementPack\Modules\Calendly; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'calendly'; } public function get_widgets() { $widgets = [ 'Calendly', ]; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Calendly', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, ]; <?php namespace ElementPack\Modules\EddLogin\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Background; use ElementPack\Element_Pack_Loader; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class EDD_Login extends Module_Base { public function get_name() { return 'bdt-edd-login'; } public function get_title() { return BDTEP . esc_html__('EDD Login', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-edd-login bdt-new'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['user', 'login', 'form']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-font', 'ep-edd-login']; } } public function get_script_depends() { if ($this->ep_is_edit_mode()) { return ['recaptcha', 'ep-google-login', 'ep-scripts']; } else { return ['recaptcha', 'ep-google-login', 'ep-edd-login']; } } public function get_custom_help_url() { return 'https://youtu.be/JLdKfv_-R6c'; } protected function register_controls() { $this->register_form_controls_layout(); $this->register_form_controls_style(); $this->register_form_controls_label(); $this->register_form_controls_fields(); $this->register_form_submit_button(); } protected function register_form_controls_layout() { $this->start_controls_section( 'section_layout', [ 'label' => __('Layout', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'edd_login_form_input_fullwidth', [ 'label' => esc_html__('Fullwidth Input', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => esc_html__('On', 'bdthemes-element-pack'), 'label_off' => esc_html__('Off', 'bdthemes-element-pack'), 'selectors' => [ '{{WRAPPER}} #edd_login_form input[type*="text"]' => 'width: 100%;', // '{{WRAPPER}} #edd_login_form input[type*="email"]' => 'width: 100%;', // '{{WRAPPER}} #edd_login_form input[type*="url"]' => 'width: 100%;', // '{{WRAPPER}} #edd_login_form input[type*="number"]' => 'width: 100%;', // '{{WRAPPER}} #edd_login_form input[type*="tel"]' => 'width: 100%;', // '{{WRAPPER}} #edd_login_form input[type*="date"]' => 'width: 100%;', '{{WRAPPER}} #edd_login_form input[type*="password"]' => 'width: 100%;', // '{{WRAPPER}} #edd_login_form .select.edd-select' => 'width: 100%;', ], ] ); $this->add_control( 'edd_login_form_button_fullwidth', [ 'label' => esc_html__('Fullwidth Button', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => esc_html__('On', 'bdthemes-element-pack'), 'label_off' => esc_html__('Off', 'bdthemes-element-pack'), 'selectors' => [ '{{WRAPPER}} #edd_login_form .edd-submit' => 'width: 100%;', ], ] ); $this->end_controls_section(); } protected function register_form_controls_style() { $this->start_controls_section( 'section_style', [ 'label' => esc_html__('Form Style', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'form_style_title', [ 'label' => __('T I T L E', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, 'separator' => 'before', ] ); $this->add_control( 'form_title_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form legend' => 'color: {{VALUE}}', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'form_title_typography', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_login_form legend', ] ); $this->add_control( 'links_heading', [ 'label' => esc_html__('L I N K S', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] ); $this->add_control( 'links_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form .edd-lost-password a' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'links_hover_color', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form .edd-lost-password a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'links_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_login_form .edd-lost-password a', ] ); $this->end_controls_section(); } protected function register_form_controls_label() { $this->start_controls_section( 'section_style_labels', [ 'label' => esc_html__('Form Label', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'label_spacing', [ 'label' => esc_html__('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} #edd_login_form .edd-input' => 'margin-top: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'label_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form label' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'label_typography', 'selector' => '{{WRAPPER}} #edd_login_form label', ] ); $this->end_controls_section(); } protected function register_form_controls_fields() { $this->start_controls_section( 'section_field_style', [ 'label' => esc_html__('Form Fields', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_field_style'); $this->start_controls_tab( 'tab_field_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'field_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form input[type="text"], {{WRAPPER}} #edd_login_form input[type="password"]' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_placeholder_color', [ 'label' => esc_html__('Placeholder Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form .edd-input::placeholder' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_login_form .edd-input::-moz-placeholder' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_background_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form .edd-input' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'field_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'default' => '1px', 'selector' => '{{WRAPPER}} #edd_login_form .edd-input', 'separator' => 'before', ] ); $this->add_responsive_control( 'field_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #edd_login_form .edd-input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'field_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_login_form .edd-input' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; height: auto;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'field_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_login_form .edd-input', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'field_box_shadow', 'selector' => '{{WRAPPER}} #edd_login_form .edd-input', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_field_hover', [ 'label' => esc_html__('Focus', 'bdthemes-element-pack'), ] ); $this->add_control( 'field_text_color_focus', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form input:focus' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_placeholder_color_focus', [ 'label' => esc_html__('Placeholder Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form input:focus::placeholder' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_login_form input:focus::-moz-placeholder' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_background_color_focus', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form input:focus' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'field_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form input:focus' => 'border-color: {{VALUE}}; outline:none;', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } protected function register_form_submit_button() { $this->start_controls_section( 'section_submit_button_style', [ 'label' => esc_html__('Form Submit Button', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_button_style'); $this->start_controls_tab( 'tab_button_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form #edd_login_submit' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'button_background_color', 'label' => __('Background Color', 'bdthemes-element-pack'), 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} #edd_login_form #edd_login_submit', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'button_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #edd_login_form #edd_login_submit', 'separator' => 'before', ] ); $this->add_responsive_control( 'button_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #edd_login_form #edd_login_submit' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_text_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_login_form #edd_login_submit' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_text_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_login_form #edd_login_submit' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'button_typography', 'selector' => '{{WRAPPER}} #edd_login_form #edd_login_submit', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_box_shadow', 'label' => esc_html__('Box Shadow', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_login_form #edd_login_submit', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_button_hover', [ 'label' => esc_html__('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_hover_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form #edd_login_submit:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'button_background_hover_color', 'label' => __('Background Color', 'bdthemes-element-pack'), 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} #edd_login_form #edd_login_submit:hover', ] ); $this->add_control( 'button_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_login_form #edd_login_submit:hover' => 'border-color: {{VALUE}};', ], 'condition' => [ 'button_border_border!' => '', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_hover_box_shadow', 'label' => esc_html__('Box Shadow', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_login_form #edd_login_submit:hover', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } public function render() { $settings = $this->get_settings_for_display(); if (is_user_logged_in() && Element_Pack_Loader::elementor()->editor->is_edit_mode()) { global $edd_login_redirect; edd_print_errors(); ?> <div class="bdt-edd-login"> <form id="edd_login_form" class="edd_form" action="" method="post"> <fieldset> <legend><?php esc_html_e('Log into Your Account', 'easy-digital-downloads'); ?></legend> <?php do_action('edd_login_fields_before'); ?> <p class="edd-login-username"> <label for="edd_user_login"><?php esc_html_e('Username or Email', 'easy-digital-downloads'); ?></label> <input name="edd_user_login" id="edd_user_login" class="edd-required edd-input" type="text" /> </p> <p class="edd-login-password"> <label for="edd_user_pass"><?php esc_html_e('Password', 'easy-digital-downloads'); ?></label> <input name="edd_user_pass" id="edd_user_pass" class="edd-password edd-required edd-input" type="password" /> </p> <p class="edd-login-remember"> <label><input name="rememberme" type="checkbox" id="rememberme" value="forever" /> <?php esc_html_e('Remember Me', 'easy-digital-downloads'); ?></label> </p> <p class="edd-login-submit"> <input type="hidden" name="edd_redirect" value="<?php echo esc_url($edd_login_redirect); ?>" /> <input type="hidden" name="edd_login_nonce" value="<?php echo esc_attr(wp_create_nonce('edd-login-nonce')); ?>" /> <input type="hidden" name="edd_action" value="user_login" /> <input id="edd_login_submit" type="submit" class="edd-submit" value="<?php esc_html_e('Log In', 'easy-digital-downloads'); ?>" /> </p> <p class="edd-lost-password"> <a href="<?php echo esc_url(edd_get_lostpassword_url()); ?>"> <?php esc_html_e('Lost Password?', 'easy-digital-downloads'); ?> </a> </p> <?php do_action('edd_login_fields_after'); ?> </fieldset> </form> </div> <?php } else { ?> <div class="bdt-edd-login"> <?php echo do_shortcode('[edd_login]'); ?> </div> <?php } } } <?php namespace ElementPack\Modules\EddLogin; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'bdt-edd-login'; } public function get_widgets() { $widgets = [ 'EDD_Login', ]; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('EDD Login', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, ]; <?php namespace ElementPack\Modules\Timeline\Widgets; use Elementor\Repeater; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Image_Size; use Elementor\Group_Control_Background; use ElementPack\Utils; use Elementor\Icons_Manager; use ElementPack\Includes\Controls\GroupQuery\Group_Control_Query; use ElementPack\Modules\Timeline\Skins; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Timeline extends Module_Base { use Group_Control_Query; private $_query = null; public function get_name() { return 'bdt-timeline'; } public function get_title() { return BDTEP . esc_html__( 'Timeline', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-timeline'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'timeline', 'history', 'statistics' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-styles' ]; } else { return [ 'ep-timeline', 'ep-font' ]; } } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'timeline', 'ep-scripts' ]; } else { return [ 'timeline', 'ep-timeline' ]; } } public function get_custom_help_url() { return 'https://youtu.be/lp4Zqn6niXU'; } public function register_skins() { $this->add_skin( new Skins\Skin_Olivier( $this ) ); } public function get_query() { return $this->_query; } protected function register_controls() { $this->start_controls_section( 'section_content_layout', [ 'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ), ] ); $this->add_control( 'timeline_source', [ 'label' => esc_html__( 'Source', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'post', 'options' => [ 'post' => __( 'Post', 'bdthemes-element-pack' ), 'custom' => __( 'Custom Content', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'timeline_align', [ 'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'default' => 'center', 'toggle' => false, 'options' => [ 'left' => [ 'title' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-left', ], 'center' => [ 'title' => esc_html__( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-center', ], 'right' => [ 'title' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-right', ], ], 'condition' => [ '_skin' => '', ] ] ); $this->add_control( 'visible_items', [ 'label' => esc_html__( 'Visible Items', 'bdthemes-element-pack' ), 'type' => Controls_Manager::NUMBER, 'default' => 4, 'condition' => [ '_skin' => 'bdt-olivier', ] ] ); $this->end_controls_section(); //New Query Builder Settings $this->start_controls_section( 'section_post_query_builder', [ 'label' => __( 'Query', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_CONTENT, 'condition' => [ 'timeline_source' => 'post' ] ] ); $this->register_query_builder_controls(); $this->update_control( 'posts_per_page', [ 'default' => 4, ] ); $this->end_controls_section(); $this->start_controls_section( 'section_custom_content', [ 'label' => esc_html__( 'Custom Content', 'bdthemes-element-pack' ), 'condition' => [ 'timeline_source' => 'custom' ] ] ); $repeater = new Repeater(); $repeater->add_control( 'timeline_title', [ 'label' => esc_html__( 'Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'This is Timeline Item 1 Title', 'bdthemes-element-pack' ), ] ); $repeater->add_control( 'timeline_date', [ 'label' => esc_html__( 'Date', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => '31 December 2018', ] ); $repeater->add_control( 'timeline_image', [ 'label' => esc_html__( 'Image', 'bdthemes-element-pack' ), 'type' => Controls_Manager::MEDIA, 'default' => [ 'url' => Utils::get_placeholder_image_src(), ], ] ); $repeater->add_control( 'timeline_text', [ 'label' => esc_html__( 'Content', 'bdthemes-element-pack' ), 'type' => Controls_Manager::WYSIWYG, 'default' => esc_html__( 'I am timeline item content. Click edit button to change this text. A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.', 'bdthemes-element-pack' ), ] ); $repeater->add_control( 'timeline_link', [ 'label' => esc_html__( 'Button Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'placeholder' => 'https://bdthemes.com', 'default' => 'https://bdthemes.com', ] ); $repeater->add_control( 'timeline_select_icon', [ 'label' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::ICONS, 'fa4compatibility' => 'timeline_icon', 'default' => [ 'value' => 'fas fa-file-alt', 'library' => 'fa-solid', ], ] ); $this->add_control( 'timeline_items', [ 'label' => esc_html__( 'Timeline Items', 'bdthemes-element-pack' ), 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(), 'default' => [ [ 'timeline_title' => esc_html__( 'This is Timeline Item 1 Title', 'bdthemes-element-pack' ), 'timeline_text' => esc_html__( 'I am timeline item content. Click edit button to change this text. A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.', 'bdthemes-element-pack' ), 'timeline_select_icon' => [ 'value' => 'fas fa-file-alt', 'library' => 'fa-solid' ], ], [ 'timeline_title' => esc_html__( 'This is Timeline Item 2 Title', 'bdthemes-element-pack' ), 'timeline_text' => esc_html__( 'I am timeline item content. Click edit button to change this text. A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.', 'bdthemes-element-pack' ), 'timeline_select_icon' => [ 'value' => 'fas fa-file-alt', 'library' => 'fa-solid' ], ], [ 'timeline_title' => esc_html__( 'This is Timeline Item 3 Title', 'bdthemes-element-pack' ), 'timeline_text' => esc_html__( 'I am timeline item content. Click edit button to change this text. A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.', 'bdthemes-element-pack' ), 'timeline_select_icon' => [ 'value' => 'fas fa-file-alt', 'library' => 'fa-solid' ], ], [ 'timeline_title' => esc_html__( 'This is Timeline Item 4 Title', 'bdthemes-element-pack' ), 'timeline_text' => esc_html__( 'I am timeline item content. Click edit button to change this text. A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.', 'bdthemes-element-pack' ), 'timeline_select_icon' => [ 'value' => 'fas fa-file-alt', 'library' => 'fa-solid' ], ], ], 'title_field' => '{{{ timeline_title }}}', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_button', [ 'label' => esc_html__( 'Readmore Button', 'bdthemes-element-pack' ), 'condition' => [ 'show_readmore' => 'yes', ], ] ); $this->add_control( 'readmore_text', [ 'label' => esc_html__( 'Read More Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'label_block' => true, 'default' => esc_html__( 'Read More', 'bdthemes-element-pack' ), 'placeholder' => esc_html__( 'Read More', 'bdthemes-element-pack' ), ] ); $this->add_control( 'button_size', [ 'label' => __( 'Button Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'sm', 'options' => [ 'xs' => __( 'Extra Small', 'bdthemes-element-pack' ), 'sm' => __( 'Small', 'bdthemes-element-pack' ), 'md' => __( 'Medium', 'bdthemes-element-pack' ), 'lg' => __( 'Large', 'bdthemes-element-pack' ), 'xl' => __( 'Extra Large', 'bdthemes-element-pack' ), ] ] ); $this->add_control( 'button_icon', [ 'label' => esc_html__( 'Button Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::ICONS, 'fa4compatibility' => 'icon', 'label_block' => false, 'skin' => 'inline' ] ); $this->add_control( 'icon_align', [ 'label' => esc_html__( 'Icon Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'right', 'options' => [ 'left' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'right' => esc_html__( 'Right', 'bdthemes-element-pack' ), ], 'condition' => [ 'button_icon[value]!' => '', ], ] ); $this->add_control( 'icon_indent', [ 'label' => esc_html__( 'Icon Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 8, ], 'range' => [ 'px' => [ 'max' => 50, ], ], 'condition' => [ 'button_icon[value]!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-button-icon-align-right' => is_rtl() ? 'margin-right: {{SIZE}}{{UNIT}};' : 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-timeline .bdt-button-icon-align-left' => is_rtl() ? 'margin-left: {{SIZE}}{{UNIT}};' : 'margin-right: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_additional', [ 'label' => esc_html__( 'Additional Options', 'bdthemes-element-pack' ) ] ); $this->add_control( 'show_image', [ 'label' => esc_html__( 'Image', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_title', [ 'label' => esc_html__( 'Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'title_tags', [ 'label' => __( 'Title HTML Tag', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'h4', 'options' => element_pack_title_tags(), 'condition' => [ 'show_title' => 'yes' ] ] ); $this->add_control( 'title_link', [ 'label' => esc_html__( 'Title Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'show_title' => 'yes', ], ] ); $this->add_control( 'show_meta', [ 'label' => esc_html__( 'Meta Data', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_excerpt', [ 'label' => esc_html__( 'Show Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'excerpt_length', [ 'label' => esc_html__( 'Text Limit', 'bdthemes-element-pack' ), 'description' => esc_html__( 'It\'s just work for main content, but not working with excerpt. If you set 0 so you will get full main content.', 'bdthemes-element-pack' ), 'type' => Controls_Manager::NUMBER, 'default' => 15, 'condition' => [ 'show_excerpt' => 'yes', 'timeline_source' => 'post', ], ] ); $this->add_control( 'strip_shortcode', [ 'label' => esc_html__( 'Strip Shortcode', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'show_excerpt' => 'yes', 'timeline_source' => 'post', ], ] ); $this->add_control( 'show_readmore', [ 'label' => esc_html__( 'Read More', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'item_animation', [ 'label' => esc_html__( 'Scroll Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_item', [ 'label' => esc_html__( 'Item', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'item_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#f3f3f3', 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-item-main' => 'background-color: {{VALUE}};', '{{WRAPPER}} .bdt-timeline .bdt-timeline-arrow' => 'background-color: {{VALUE}};', '{{WRAPPER}} .bdt-timeline-item--top .bdt-timeline-content:after' => 'border-top-color: {{VALUE}};', '{{WRAPPER}} .bdt-timeline-item--bottom .bdt-timeline-content:after' => 'border-bottom-color: {{VALUE}};', '{{WRAPPER}} .bdt-timeline--mobile .bdt-timeline-content:after' => 'border-right-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_shadow', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-item-main', ] ); $this->add_control( 'timeline_line_color', [ 'label' => esc_html__( 'Line Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline-divider, {{WRAPPER}} .bdt-timeline .bdt-timeline-line span, {{WRAPPER}} .bdt-timeline:not(.bdt-timeline--horizontal):before' => 'background-color: {{VALUE}};', '{{WRAPPER}} .bdt-timeline .bdt-timeline-item:after, {{WRAPPER}} .bdt-timeline.bdt-timeline-skin-default .bdt-timeline-item-main-wrapper .bdt-timeline-icon span' => 'border-color: {{VALUE}};', ], ] ); $this->add_control( 'timeline_line_width', [ 'label' => __( 'Line Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 20, ], ], 'default' => [ 'size' => 4, ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline-divider' => 'height: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-timeline .bdt-timeline-line span' => 'width: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-timeline-skin-olivier .bdt-timeline-item:after, {{WRAPPER}} .bdt-timeline.bdt-timeline-skin-default .bdt-timeline-item-main-wrapper .bdt-timeline-icon span' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'item_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-desc' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'item_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-item-main', ] ); $this->add_responsive_control( 'item_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-item-main' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_icon', [ 'label' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => '' ] ] ); $this->start_controls_tabs( 'tabs_icon_style' ); $this->start_controls_tab( 'tab_icon_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'icon_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-icon' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'icon_background', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#ffffff', 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-icon span' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'icon_shadow', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-icon span' ] ); $this->add_responsive_control( 'icon_width', [ 'label' => __( 'Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 60, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-icon span' => 'height: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'icon_show', [ 'label' => esc_html__( 'Show Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_responsive_control( 'icon_size', [ 'label' => __( 'Icon Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 35, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-icon span i, {{WRAPPER}} .bdt-timeline .bdt-timeline-icon span' => 'font-size: {{SIZE}}{{UNIT}};', ], 'condition' => [ 'icon_show' => 'yes', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'icon_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-icon span', ] ); $this->add_responsive_control( 'icon_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ '%' => [ 'min' => 0, 'max' => 100, ], ], 'default' => [ 'size' => 50, 'unit' => '%', ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-icon span' => 'border-radius: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_icon_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'icon_hover_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-icon:hover, {{WRAPPER}} .bdt-timeline .bdt-timeline-icon span:hover' => 'color: {{VALUE}} !important;', ], ] ); $this->add_control( 'icon_hover_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-icon span:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'icon_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-icon span:hover' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_date', [ 'label' => esc_html__( 'Date', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => '', ] ] ); $this->add_control( 'date_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-date span' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'date_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#f3f3f3;', 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-date span' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'date_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-date span', 'separator' => 'before', ] ); $this->add_responsive_control( 'date_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'default' => [ 'top' => '2', 'right' => '2', 'bottom' => '2', 'left' => '2', ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-date span' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_responsive_control( 'date_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'default' => [ 'top' => '10', 'right' => '15', 'bottom' => '10', 'left' => '15', ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-date span' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'date_typography', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-date', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'date_shadow', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-date span', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_image', [ 'label' => esc_html__( 'Image', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_image' => 'yes', ], ] ); $this->add_group_control( Group_Control_Image_Size::get_type(), [ 'name' => 'thumbnail_size', 'label' => esc_html__( 'Image Size', 'bdthemes-element-pack' ), 'exclude' => [ 'custom' ], 'default' => 'medium', 'prefix_class' => 'bdt-timeline-thumbnail-size-', ] ); $this->add_responsive_control( 'image_ratio', [ 'label' => esc_html__( 'Image Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 265, ], 'range' => [ 'px' => [ 'min' => 50, 'max' => 500, 'step' => 5, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-thumbnail img' => 'height: {{SIZE}}px', ], ] ); $this->add_control( 'image_opacity', [ 'label' => esc_html__( 'Opacity', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'min' => 0.1, 'max' => 1, 'step' => 0.1, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-thumbnail img' => 'opacity: {{SIZE}};', ], ] ); $this->add_responsive_control( 'image_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'default' => [ 'top' => '20', 'right' => '20', 'bottom' => '0', 'left' => '20', ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-thumbnail' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'image_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-thumbnail', ] ); $this->add_responsive_control( 'image_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-thumbnail' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};overflow: hidden;', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_title', [ 'label' => esc_html__( 'Title', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_title' => 'yes', ], ] ); $this->add_control( 'title_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-title *' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'title_hover_color', [ 'label' => esc_html__( 'Hover Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-title a:hover' => 'color: {{VALUE}};', ], 'condition' => [ 'title_link' => 'yes', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'title_bg_color', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-title *', 'fields_options' => [ 'background' => [ 'label' => esc_html__( 'Background Type', 'bdthemes-element-pack' ), ], ], ] ); $this->add_responsive_control( 'title_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-title *' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'title_padding', [ 'label' => __( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%', 'em' ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-title *' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-title', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_meta', [ 'label' => esc_html__( 'Meta', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_meta' => 'yes', ], ] ); $this->add_control( 'meta_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#bbbbbb', 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-meta *' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'meta_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-meta *', ] ); $this->add_control( 'meta_spacing', [ 'label' => __( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'default' => [ 'size' => 10, ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-meta' => 'margin-top: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_excerpt', [ 'label' => esc_html__( 'Text', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_excerpt' => 'yes', ], ] ); $this->add_control( 'excerpt_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#888888', 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-excerpt' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'excerpt_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-excerpt', ] ); $this->add_control( 'excerpt_spacing', [ 'label' => __( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'default' => [ 'size' => 20, ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-excerpt' => 'margin-top: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_readmore', [ 'label' => esc_html__( 'Readmore Button', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_readmore' => 'yes', ], ] ); $this->start_controls_tabs( 'tabs_readmore_style' ); $this->start_controls_tab( 'tab_readmore_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'readmore_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore svg' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'readmore_background', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'readmore_shadow', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore', ] ); $this->add_control( 'readmore_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'readmore_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore', ] ); $this->add_responsive_control( 'readmore_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};overflow: hidden;', ], ] ); $this->add_control( 'readmore_spacing', [ 'label' => __( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'default' => [ 'size' => 20, ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore' => 'margin-top: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'readmore_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_readmore_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'readmore_hover_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore:hover' => 'color: {{VALUE}} !important;', '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore:hover svg' => 'fill: {{VALUE}} !important;', ], ] ); $this->add_control( 'readmore_hover_background', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'readmore_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'readmore_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-readmore:hover' => 'border-color: {{VALUE}};', ], ] ); $this->add_control( 'readmore_hover_animation', [ 'label' => esc_html__( 'Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HOVER_ANIMATION, ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_navigation_button', [ 'label' => esc_html__( 'Navigation Button', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => 'bdt-olivier', ], ] ); $this->start_controls_tabs( 'tabs_navigation_button_style' ); $this->start_controls_tab( 'tab_navigation_button_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'navigation_button_color', [ 'label' => esc_html__( 'Icon Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-nav-button:before' => 'border-top-color: {{VALUE}}; border-left-color: {{VALUE}};', ], ] ); $this->add_control( 'navigation_button_background', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline-nav-button' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'navigation_button_shadow', 'selector' => '{{WRAPPER}} .bdt-timeline-nav-button', ] ); $this->add_responsive_control( 'navigation_button_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline-nav-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'navigation_button_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-timeline-nav-button', ] ); $this->add_responsive_control( 'navigation_button_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline-nav-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};overflow: hidden;', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_navigation_button_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'navigation_button_hover_color', [ 'label' => esc_html__( 'Icon Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline .bdt-timeline-nav-button:hover:before' => 'border-top-color: {{VALUE}}; border-left-color: {{VALUE}};', ], ] ); $this->add_control( 'navigation_button_hover_background', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-timeline-nav-button:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'navigation_button_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'navigation_button_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-timeline-nav-button:hover' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } public function render_excerpt( $item = [] ) { if ( ! $this->get_settings( 'show_excerpt' ) ) { return; } $settings = $this->get_settings_for_display(); if ( 'post' == $settings['timeline_source'] ) { $strip_shortcode = $this->get_settings_for_display( 'strip_shortcode' ); ?> <div class="bdt-timeline-excerpt"> <?php if ( has_excerpt() ) { the_excerpt(); } else { echo wp_kses_post(element_pack_custom_excerpt( $this->get_settings_for_display( 'excerpt_length' ), $strip_shortcode )); } ?> </div> <?php } else { ?> <div class="bdt-timeline-excerpt"> <?php echo do_shortcode( $item['timeline_text'] ); ?> </div> <?php } } public function render_readmore( $item = [] ) { if ( ! $this->get_settings( 'show_readmore' ) ) { return; } $settings = $this->get_settings_for_display(); if ( 'post' == $settings['timeline_source'] ) { $readmore_link = get_permalink(); } else { $readmore_link = $item['timeline_link']; } $this->add_render_attribute( [ 'timeline-readmore' => [ 'href' => esc_url( $readmore_link ), 'class' => [ 'bdt-timeline-readmore', 'elementor-button', 'elementor-size-' . esc_attr( $settings['button_size'] ), $settings['readmore_hover_animation'] ? 'elementor-animation-' . $settings['readmore_hover_animation'] : '' ], ] ], '', '', true ); if ( ! isset( $settings['icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $settings['icon'] = 'fas fa-arrow-right'; } $migrated = isset( $settings['__fa4_migrated']['button_icon'] ); $is_new = empty( $settings['icon'] ) && Icons_Manager::is_migration_allowed(); ?> <a <?php $this->print_render_attribute_string( 'timeline-readmore' ); ?>> <?php echo esc_html( $settings['readmore_text'] ); ?> <?php if ( $settings['button_icon']['value'] ) : ?> <span class="bdt-button-icon-align-<?php echo esc_attr( $settings['icon_align'] ); ?>"> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $settings['button_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $settings['icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </span> <?php endif; ?> </a> <?php } public function render_image( $item = [] ) { if ( ! $this->get_settings( 'show_image' ) ) { return; } $settings = $this->get_settings_for_display(); if ( 'post' == $settings['timeline_source'] ) { $image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); if ( is_array( $image_url ) ) { $image_url = $image_url[0]; } $title = get_the_title(); } else { $image_url = ( $item['timeline_image']['url'] ) ?: ''; $title = $item['timeline_title']; } if ( $image_url ) { ?> <div class="bdt-timeline-thumbnail"> <img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $title ); ?>"> </div> <?php } } public function render_title( $item = [] ) { if ( ! $this->get_settings( 'show_title' ) ) { return; } $settings = $this->get_settings_for_display(); if ( 'post' == $settings['timeline_source'] ) { $title_link = get_permalink(); $title = get_the_title(); } else { $title_link = $item['timeline_link']; $title = $item['timeline_title']; } $this->add_render_attribute( 'bdt-timeline-title', 'class', 'bdt-timeline-title', true ); ?> <<?php echo esc_attr( Utils::get_valid_html_tag( $settings['title_tags'] ) ); ?> <?php $this->print_render_attribute_string( 'bdt-timeline-title' ); ?>> <?php if ( $settings['title_link'] ) : ?> <a href="<?php echo esc_url( $title_link ); ?>" title="<?php echo esc_html( $title ); ?>"> <?php echo esc_html( $title ); ?> </a> <?php else : ?> <span> <?php echo esc_html( $title ); ?> </span> <?php endif; ?> </<?php echo esc_attr( Utils::get_valid_html_tag( $settings['title_tags'] ) ); ?>> <?php } public function render_meta( $align, $item = [] ) { if ( ! $this->get_settings( 'show_meta' ) ) { return; } $settings = $this->get_settings_for_display(); $hidden_class = ( 'center' == $align ) ? 'bdt-hidden@m' : ''; $meta_date = '<li class="' . $hidden_class . '">' . esc_attr( get_the_date( 'd F Y' ) ) . '</li>'; $meta_list = '<li>' . get_the_category_list( ', ' ) . '</li>'; ?> <ul class="bdt-timeline-meta bdt-subnav bdt-flex-middle"> <?php if ( 'post' == $settings['timeline_source'] ) { echo wp_kses_post( $meta_date ); echo wp_kses_post( $meta_list ); } else { ?> <li> <?php echo esc_attr( $item['timeline_date'] ); ?> </li> <?php } ?> </ul> <?php } public function render_item( $item_parallax, $align, $item = [] ) { ?> <div class="bdt-timeline-item-main" <?php echo wp_kses_post( $item_parallax ); ?>> <span class="bdt-timeline-arrow"></span> <?php $this->render_image( $item ); ?> <div class="bdt-timeline-desc bdt-padding"> <?php $this->render_title( $item ); ?> <?php $this->render_meta( $align, $item ); ?> <?php $this->render_excerpt( $item ); ?> <?php $this->render_readmore( $item ); ?> </div> </div> <?php } public function render_date( $align = 'left', $item = [] ) { $settings = $this->get_settings_for_display(); $date_parallax = ''; if ( 'post' == $settings['timeline_source'] ) { $timeline_date = get_the_date( 'd F Y' ); } else { $timeline_date = $item['timeline_date']; } if ( $settings['item_animation'] ) { if ( $align == 'right' ) { $date_parallax = ' bdt-parallax="opacity: 0,1;x: -200,0;viewport: 0.5;"'; } else { $date_parallax = ' bdt-parallax="opacity: 0,1;x: 200,0;viewport: 0.5;"'; } } ?> <div class="bdt-timeline-item bdt-width-1-2@m bdt-visible@m"> <div class="bdt-timeline-date bdt-text-<?php echo esc_attr( $align ); ?>" <?php echo esc_attr( $date_parallax ); ?>> <span> <?php echo esc_attr( $timeline_date ); ?> </span> </div> </div> <?php } /** * Get post query builder arguments */ public function query_posts( $posts_per_page ) { $settings = $this->get_settings(); $args = []; if ( $posts_per_page ) { $args['posts_per_page'] = $posts_per_page; $args['paged'] = max( 1, get_query_var( 'paged' ), get_query_var( 'page' ) ); } $default = $this->getGroupControlQueryArgs(); $args = array_merge( $default, $args ); $this->_query = new \WP_Query( $args ); } function render_post_format() { $settings = $this->get_settings_for_display(); //$icon_parallax = ($settings['item_animation']) ? ' bdt-parallax="scale: 0.5,1; viewport: 0.5;"' : ''; $this->add_render_attribute( 'timeline-icon', 'class', 'bdt-timeline-icon' ); if ( $settings['item_animation'] ) { $this->add_render_attribute( 'timeline-icon', 'bdt-parallax', 'scale: 0.5,1; viewport: 0.5;' ); } ?> <div <?php $this->print_render_attribute_string( 'timeline-icon' ); ?>> <?php if ( has_post_format( 'aside' ) ) : ?> <span><i class="ep-icon-aside" aria-hidden="true"></i></span> <?php elseif ( has_post_format( 'gallery' ) ) : ?> <span><i class="ep-icon-gallery" aria-hidden="true"></i></span> <?php elseif ( has_post_format( 'link' ) ) : ?> <span><i class="ep-icon-link" aria-hidden="true"></i></span> <?php elseif ( has_post_format( 'image' ) ) : ?> <span><i class="ep-icon-image" aria-hidden="true"></i></span> <?php elseif ( has_post_format( 'quote' ) ) : ?> <span><i class="ep-icon-quote" aria-hidden="true"></i></span> <?php elseif ( has_post_format( 'status' ) ) : ?> <span><i class="ep-icon-status" aria-hidden="true"></i></span> <?php elseif ( has_post_format( 'video' ) ) : ?> <span><i class="ep-icon-video" aria-hidden="true"></i></span> <?php elseif ( has_post_format( 'audio' ) ) : ?> <span><i class="ep-icon-music" aria-hidden="true"></i></span> <?php elseif ( has_post_format( 'chat' ) ) : ?> <span><i class="ep-icon-chat" aria-hidden="true"></i></span> <?php else : ?> <span><i class="ep-icon-post" aria-hidden="true"></i></span> <?php endif; ?> </div> <?php } public function render_post() { $settings = $this->get_settings_for_display(); $id = $this->get_id(); $align = $settings['timeline_align']; // $vertical_line_parallax = ($settings['item_animation']) ? ' bdt-parallax="opacity: 0,1;viewport: 0.5;"' : ''; if ( $settings['item_animation'] ) { $this->add_render_attribute( 'vertical_line_parallax', 'bdt-parallax', 'opacity: 0,1;viewport: 0.5;"' ); } // TODO need to delete after v6.5 if ( isset( $settings['posts_limit'] ) and $settings['posts_per_page'] == 6 ) { $limit = $settings['posts_limit']; } else { $limit = $settings['posts_per_page']; } $this->query_posts( $limit ); $wp_query = $this->get_query(); if ( ! $wp_query->found_posts ) { return; } if ( $wp_query->have_posts() ) : $this->add_render_attribute( [ 'bdt-timeline' => [ 'id' => 'bdt-timeline-' . esc_attr( $id ), 'class' => [ 'bdt-timeline', 'bdt-timeline-skin-default', 'bdt-timeline-' . esc_attr( $align ) ] ] ] ); if ( 'yes' == $settings['icon_show'] ) { $this->add_render_attribute( 'bdt-timeline', 'class', 'bdt-timeline-icon-yes' ); } ?> <div <?php $this->print_render_attribute_string( 'bdt-timeline' ); ?>> <div class="bdt-grid bdt-grid-collapse"> <?php $bdt_count = 0; while ( $wp_query->have_posts() ) : $wp_query->the_post(); $bdt_count++; $post_format = get_post_format() ?: 'standard'; $item_part = ( $bdt_count % 2 === 0 ) ? 'right' : 'left'; if ( 'center' == $align ) { $parallax_direction = ( $bdt_count % 2 === 0 ) ? '' : '-'; $item_parallax = ( $settings['item_animation'] ) ? ' bdt-parallax="opacity:0,1;x:' . $parallax_direction . '200,0;viewport: 0.5;"' : ''; } elseif ( 'right' == $align ) { $item_parallax = ( $settings['item_animation'] ) ? ' bdt-parallax="opacity: 0,1;x: -200,0;viewport: 0.5;"' : ''; } else { $item_parallax = ( $settings['item_animation'] ) ? ' bdt-parallax="opacity: 0,1;x: 200,0;viewport: 0.5;"' : ''; } if ( $bdt_count % 2 === 0 and 'center' == $align ) : ?> <?php $this->render_date( 'right', '' ); ?> <?php endif; ?> <div class="<?php echo ( 'center' == $align ) ? ' bdt-width-1-2@m ' : 'bdt-width-1-1 '; ?>bdt-timeline-item <?php echo esc_attr( $item_part ) . '-part'; ?>"> <div class="bdt-timeline-item-main-wrapper"> <div class="bdt-timeline-line"> <span <?php $this->print_render_attribute_string( 'vertical_line_parallax' ); ?>></span> </div> <div class="bdt-timeline-item-main-container"> <?php $this->render_post_format(); ?> <?php $this->render_item( $item_parallax, $align, '' ); ?> </div> </div> </div> <?php if ( $bdt_count % 2 === 1 and ( 'center' == $align ) ) : ?> <?php $this->render_date( 'left', '' ); ?> <?php endif; ?> <?php endwhile; wp_reset_postdata(); ?> </div> </div> <?php endif; } public function render_custom() { $id = $this->get_id(); $settings = $this->get_settings_for_display(); $timeline_items = $settings['timeline_items']; $align = $settings['timeline_align']; $vertical_line_parallax = ( $settings['item_animation'] ) ? ' bdt-parallax="opacity: 0,1;viewport: 0.2;"' : ''; $this->add_render_attribute( 'bdt-timeline-custom', 'id', 'bdt-timeline-' . esc_attr( $id ) ); $this->add_render_attribute( 'bdt-timeline-custom', 'class', 'bdt-timeline bdt-timeline-skin-default' ); $this->add_render_attribute( 'bdt-timeline-custom', 'class', 'bdt-timeline-' . esc_attr( $align ) ); ?> <div <?php $this->print_render_attribute_string( 'bdt-timeline-custom' ); ?>> <div class="bdt-grid bdt-grid-collapse" bdt-grid> <?php $bdt_count = 0; foreach ( $timeline_items as $item ) : $bdt_count++; if ( ! isset( $item['timeline_icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $item['timeline_icon'] = 'fas fa-file-alt'; } $migrated = isset( $item['__fa4_migrated']['timeline_select_icon'] ); $is_new = empty( $item['timeline_icon'] ) && Icons_Manager::is_migration_allowed(); if ( 'center' == $align ) { $parallax_direction = ( $bdt_count % 2 === 0 ) ? '' : '-'; $item_parallax = ( $settings['item_animation'] ) ? ' bdt-parallax="opacity:0,1;x:' . $parallax_direction . '200,0;viewport: 0.5;"' : ''; } elseif ( 'right' == $align ) { $item_parallax = ( $settings['item_animation'] ) ? ' bdt-parallax="opacity: 0,1;x: -200,0;viewport: 0.5;"' : ''; } else { $item_parallax = ( $settings['item_animation'] ) ? ' bdt-parallax="opacity: 0,1;x: 200,0;viewport: 0.5;"' : ''; } $item_part = ( $bdt_count % 2 === 0 ) ? 'right' : 'left'; if ( $bdt_count % 2 === 0 and 'center' == $align ) : ?> <?php $this->render_date( 'right', $item ); ?> <?php endif; ?> <div class="<?php echo ( 'center' == $align ) ? ' bdt-width-1-2@m ' : ' '; ?>bdt-timeline-item <?php echo esc_attr( $item_part ) . '-part'; ?>"> <div class="bdt-timeline-item-main-wrapper"> <div class="bdt-timeline-line"> <span <?php echo esc_attr( $vertical_line_parallax ); ?>></span> </div> <div class="bdt-timeline-item-main-container"> <?php $item_scrollspy = ( $settings['item_animation'] ) ? ' bdt-scrollspy="cls: bdt-animation-scale-up;"' : ''; ?> <div class="bdt-timeline-icon" <?php echo esc_attr( $item_scrollspy ); ?>> <span> <?php if ( 'yes' == $settings['icon_show'] ) : ?> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $item['timeline_select_icon'], [ 'aria-hidden' => 'true' ] ); else : ?> <i class="<?php echo esc_attr( $item['timeline_icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> <?php endif; ?> </span> </div> <?php $this->render_item( $item_parallax, $align, $item ); ?> </div> </div> </div> <?php if ( $bdt_count % 2 === 1 and ( 'center' == $align ) ) : ?> <?php $this->render_date( '', $item ); ?> <?php endif; ?> <?php endforeach; ?> <?php wp_reset_postdata(); ?> </div> </div> <?php } public function render() { $settings = $this->get_settings_for_display(); if ( 'post' === $settings['timeline_source'] ) { $this->render_post(); } else if ( 'custom' === $settings['timeline_source'] ) { $this->render_custom(); } else { return; } } } <?php namespace ElementPack\Modules\Timeline; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'timeline'; } public function get_widgets() { $widgets = [ 'Timeline', ]; return $widgets; } } <?php namespace ElementPack\Modules\Timeline\Skins; use Elementor\Skin_Base as Elementor_Skin_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Olivier extends Elementor_Skin_Base { public function get_id() { return 'bdt-olivier'; } public function get_title() { return __( 'Olivier', 'bdthemes-element-pack' ); } public function render_script() { $settings = $this->parent->get_settings_for_display(); $this->parent->add_render_attribute( 'timeline', 'class', [ 'bdt-timeline', 'bdt-timeline-skin-olivier' ] ); $this->parent->add_render_attribute( 'timeline', 'data-visible_items', $settings['visible_items'] ); } public function render_custom() { $id = $this->parent->get_id(); $settings = $this->parent->get_settings_for_display(); $timeline_items = $settings['timeline_items']; ?> <div <?php $this->parent->print_render_attribute_string( 'timeline' ); ?>> <div class="bdt-timeline-wrapper"> <div class="bdt-timeline-items"> <?php foreach ( $timeline_items as $item ) : ?> <div class="bdt-timeline-item"> <div class="bdt-timeline-content"> <?php $this->parent->render_item( '', '', $item ); ?> </div> </div> <?php endforeach; ?> </div> </div> </div> <?php } public function render_post() { $settings = $this->parent->get_settings_for_display(); // TODO need to delete after v6.5 if (isset($settings['posts_limit']) and $settings['posts_per_page'] == 6) { $limit = $settings['posts_limit']; } else { $limit = $settings['posts_per_page']; } $this->parent->query_posts($limit); $wp_query = $this->parent->get_query(); if ( ! $wp_query->found_posts ) { return; } if( $wp_query->have_posts() ) : ?> <div <?php $this->parent->print_render_attribute_string( 'timeline' ); ?>> <div class="bdt-timeline-wrapper"> <div class="bdt-timeline-items"> <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <div class="bdt-timeline-item"> <div class="bdt-timeline-content"> <?php $this->parent->render_item( '', '', '' ); ?> </div> </div> <?php endwhile; wp_reset_postdata(); ?> </div> </div> </div> <?php endif; } public function render() { $settings = $this->parent->get_settings_for_display(); $this->render_script(); if ( 'post' === $settings['timeline_source'] ) { $this->render_post(); } else if ( 'custom' === $settings['timeline_source'] ) { $this->render_custom(); } else { return; } } }<?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Timeline', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\ParallaxEffects; use Elementor\Controls_Manager; use ElementPack; use ElementPack\Base\Element_Pack_Module_Base; if ( !defined('ABSPATH') ) { exit; } // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function __construct() { parent::__construct(); $this->add_actions(); } public function get_name() { return 'bdt-parallax-effects'; } public function register_widget_control($widget, $args) { $widget->add_control( 'ep_parallax_effects_show', [ 'label' => BDTEP_CP . esc_html__('Parallax/Scrolling Effects', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => '', 'return_value' => 'yes', 'frontend_available' => true, 'prefix_class' => 'ep-parallax-effects-', ] ); $widget->add_control( 'ep_parallax_effects_hr', [ 'type' => Controls_Manager::DIVIDER, ] ); $widget->add_control( 'ep_parallax_effects_x', [ 'label' => __('Horizontal Parallax(X)', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', //'render_type' => 'none', 'frontend_available' => true, ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_x_start', [ 'label' => esc_html__('Start', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px', 'vw'], 'range' => [ 'px' => [ 'min' => -500, 'max' => 500, 'step' => 10, ], 'vw' => [ 'min' => -100, 'max' => 100, 'step' => 1, ], ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_x' => 'yes', 'ep_parallax_effects_x_custom_show' => '', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->add_control( 'ep_parallax_effects_x_end', [ 'label' => esc_html__('End', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px', 'vw'], 'range' => [ 'px' => [ 'min' => -500, 'max' => 500, 'step' => 10, ], 'vw' => [ 'min' => -100, 'max' => 100, 'step' => 1, ], ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_x' => 'yes', 'ep_parallax_effects_x_custom_show' => '', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->add_control( 'ep_parallax_effects_x_custom_show', [ 'label' => esc_html__('Custom', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'default' => '', 'return_value' => 'yes', 'frontend_available' => true, 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_x' => 'yes', ], ] ); $widget->add_control( 'ep_parallax_effects_x_custom_value', [ 'label' => esc_html__('Value', 'bdthemes-element-pack'), 'description' => esc_html__('Define multiple stops for a property by using a comma separated list of values.', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'condition' => [ 'ep_parallax_effects_x_custom_show' => 'yes', 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_x' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, 'label_block' => true ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_y', [ 'label' => __('Vertical Parallax(Y)', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', //'render_type' => 'none', 'frontend_available' => true, ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_y_start', [ 'label' => esc_html__('Start', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px', 'vw'], 'range' => [ 'px' => [ 'min' => -500, 'max' => 500, 'step' => 10, ], 'vw' => [ 'min' => -100, 'max' => 100, 'step' => 1, ], ], 'default' => [ 'unit' => 'px', 'size' => 50, ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_y' => 'yes', 'ep_parallax_effects_y_custom_show' => '', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->add_control( 'ep_parallax_effects_y_end', [ 'label' => esc_html__('End', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px', 'vw'], 'range' => [ 'px' => [ 'min' => -500, 'max' => 500, 'step' => 10, ], 'vw' => [ 'min' => -100, 'max' => 100, 'step' => 1, ], ], 'default' => [ 'unit' => 'px', 'size' => 0, ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_y' => 'yes', 'ep_parallax_effects_y_custom_show' => '', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->add_control( 'ep_parallax_effects_y_custom_show', [ 'label' => esc_html__('Custom', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'default' => '', 'return_value' => 'yes', 'frontend_available' => true, 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_y' => 'yes', ], ] ); $widget->add_control( 'ep_parallax_effects_y_custom_value', [ 'label' => esc_html__('Value', 'bdthemes-element-pack'), 'description' => esc_html__('Define multiple stops for a property by using a comma separated list of values.', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'condition' => [ 'ep_parallax_effects_y_custom_show' => 'yes', 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_y' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, 'label_block' => true ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_opacity_toggole', [ 'label' => __('Opacity', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', //'render_type' => 'none', 'frontend_available' => true, ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_opacity', [ 'label' => __('Select', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'htov' => [ 'title' => __('Hidden to Visible', 'bdthemes-element-pack'), 'icon' => 'eicon-v-align-bottom', ], 'vtoh' => [ 'title' => __('Visible to Hidden', 'bdthemes-element-pack'), 'icon' => 'eicon-v-align-top', ], ], 'toggle' => true, 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_opacity_custom_show' => '', 'ep_parallax_effects_opacity_toggole' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->add_control( 'ep_parallax_effects_opacity_custom_show', [ 'label' => esc_html__('Custom', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'default' => '', 'return_value' => 'yes', 'frontend_available' => true, 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_opacity_toggole' => 'yes', ], ] ); $widget->add_control( 'ep_parallax_effects_opacity_custom_value', [ 'label' => esc_html__('Value', 'bdthemes-element-pack'), 'description' => esc_html__('Define multiple stops for a property by using a comma separated list of values.', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'condition' => [ 'ep_parallax_effects_opacity_custom_show' => 'yes', 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_opacity_toggole' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, 'label_block' => true ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_blur', [ 'label' => __('Blur', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', //'render_type' => 'none', 'frontend_available' => true, ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_blur_start', [ 'label' => esc_html__('Start', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, //'size_units' => ['px', 'vw'], 'range' => [ 'px' => [ 'min' => 0, 'max' => 20, 'step' => 1, ], ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_blur' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->add_control( 'ep_parallax_effects_blur_end', [ 'label' => esc_html__('End', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, //'size_units' => ['px', 'vw'], 'range' => [ 'px' => [ 'min' => 0, 'max' => 20, 'step' => 1, ], ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_blur' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_rotate', [ 'label' => __('Rotate', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', //'render_type' => 'none', 'frontend_available' => true, ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_rotate_start', [ 'label' => esc_html__('Start', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -360, 'max' => 360, 'step' => 5, ], ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->add_control( 'ep_parallax_effects_rotate_end', [ 'label' => esc_html__('End', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -360, 'max' => 360, 'step' => 5, ], ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_scale', [ 'label' => __('Scale', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', //'render_type' => 'none', 'frontend_available' => true, ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_scale_start', [ 'label' => esc_html__('Start', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -10, 'max' => 10, 'step' => 0.1, ], ], 'default' => [ 'unit' => 'px', 'size' => 1, ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->add_control( 'ep_parallax_effects_scale_end', [ 'label' => esc_html__('End', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -10, 'max' => 10, 'step' => 0.1, ], ], 'default' => [ 'unit' => 'px', 'size' => 1, ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_hue', [ 'label' => __('Hue', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', //'render_type' => 'none', 'frontend_available' => true, ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_hue_value', [ 'label' => esc_html__('Value', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 360, 'step' => 1, ], ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_sepia', [ 'label' => __('Sepia', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', //'render_type' => 'none', 'frontend_available' => true, ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_sepia_value', [ 'label' => esc_html__('Value', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, 'step' => 1, ], ], 'default' => [ 'unit' => 'px', 'size' => 1, ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_easing', [ 'label' => __('Easing', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', //'render_type' => 'none', 'frontend_available' => true, ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_easing_value', [ 'label' => esc_html__('Value', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -10, 'max' => 10, 'step' => 0.5, ], ], 'default' => [ 'unit' => 'px', 'size' => 1, ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_transition', [ 'label' => __('Transition', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_transition_for', [ 'label' => esc_html__('Transition For', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'default' => 'all', 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_transition' => 'yes', ], 'selectors' => [ '{{WRAPPER}}' => 'transition-property: {{VALUE||all}};', ], ] ); $widget->add_control( 'ep_parallax_effects_transition_duration', [ 'label' => esc_html__('Duration (ms)', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'default' => '100', 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_transition' => 'yes', ], 'selectors' => [ '{{WRAPPER}}' => 'transition-duration: {{VALUE||100}}ms;', ], ] ); $widget->add_control( 'ep_parallax_effects_transition_easing', [ 'label' => esc_html__('Easing', 'bdthemes-element-pack'), 'description' => sprintf(__('If you want use Cubic Bezier easing, Go %1s HERE %2s', 'bdthemes-element-pack'), '<a href="https://cubic-bezier.com/" target="_blank">', '</a>'), 'type' => Controls_Manager::TEXT, 'default' => 'linear', 'condition' => [ 'ep_parallax_effects_show' => 'yes', 'ep_parallax_effects_transition' => 'yes', ], 'selectors' => [ '{{WRAPPER}}' => 'transition-timing-function: {{VALUE||linear}};', ], ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_viewport', [ 'label' => __('Animation Viewport', 'bdthemes-element-pack'), 'type' => Controls_Manager::POPOVER_TOGGLE, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'return_value' => 'yes', //'render_type' => 'none', 'frontend_available' => true, ] ); $widget->start_popover(); $widget->add_control( 'ep_parallax_effects_viewport_start', [ 'label' => esc_html__('Start', 'bdthemes-element-pack'), 'description' => esc_html__('Start offset. The value can be in vh, % and px. It supports basic mathematics operands + and -.', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->add_control( 'ep_parallax_effects_viewport_end', [ 'label' => esc_html__('End', 'bdthemes-element-pack'), 'description' => esc_html__('End offset. The value can be in vh, % and px. It supports basic mathematics operands + and -.', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, ] ); $widget->end_popover(); $widget->add_control( 'ep_parallax_effects_media_query', [ 'label' => __('Parallax Start From', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => [ '' => __('All Device', 'bdthemes-element-pack'), '@xl' => __('Retina to Larger', 'bdthemes-element-pack'), '@l' => __('Desktop to Larger', 'bdthemes-element-pack'), '@m' => __('Tablet to Larger', 'bdthemes-element-pack'), '@s' => __('Mobile to Larger', 'bdthemes-element-pack'), ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, //'separator' => 'after', ] ); $widget->add_control( 'ep_parallax_effects_target', [ 'label' => __('Target', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::SELECT, 'default' => 'self', 'options' => [ 'self' => __('Self', 'bdthemes-element-pack'), 'section' => __('Section', 'bdthemes-element-pack'), ], 'condition' => [ 'ep_parallax_effects_show' => 'yes', ], 'render_type' => 'none', 'frontend_available' => true, 'separator' => 'after', ] ); } public function section_parallax_effects_before_render($widget) { $settings = $widget->get_settings_for_display(); if ( $settings['ep_parallax_effects_show'] == 'yes' ) { wp_enqueue_script('ep-parallax-effects'); } } protected function add_actions() { add_action('elementor/element/section/section_effects/after_section_start', [$this, 'register_widget_control'], 10, 11); add_action('elementor/element/column/section_effects/after_section_start', [$this, 'register_widget_control'], 10, 11); add_action('elementor/element/common/section_effects/after_section_start', [$this, 'register_widget_control'], 10, 11); add_action('elementor/frontend/section/before_render', [$this, 'section_parallax_effects_before_render'], 10, 1); add_action('elementor/frontend/widget/before_render', [$this, 'section_parallax_effects_before_render'], 10, 1); } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Parallax Effects', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\AcfAccordion\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Icons_Manager; use ElementPack\Utils; use ElementPack\Includes\Controls\SelectInput\Dynamic_Select; use ElementPack\Includes\ACF_Global; use ElementPack\Traits\Global_Widget_Controls; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class Acf_Accordion extends Module_Base { use Global_Widget_Controls; public function get_name() { return 'bdt-acf-accordion'; } public function get_title() { return BDTEP . esc_html__('ACF Accordion', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-acf-accordion'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['acf', 'accordion', 'acf-accordion']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-accordion']; } } public function get_script_depends() { if ($this->ep_is_edit_mode()) { return ['ep-scripts']; } else { return ['ep-accordion']; } } public function get_custom_help_url() { return ''; } protected function register_controls() { $this->start_controls_section( 'section_title', [ 'label' => __('ACF Accordion', 'bdthemes-element-pack'), ] ); $this->add_control( 'field', [ 'label' => __('Repeater Field', 'bdthemes-element-pack'), 'dynamic' => ['active' => false], 'type' => Dynamic_Select::TYPE, 'label_block' => true, 'placeholder' => __('Type and select the repeater field...', 'bdthemes-element-pack'), 'description' => sprintf(__('Supported field type: <b>%1s</b>', 'bdthemes-element-pack'), 'Repeater'), 'query_args' => [ 'query' => 'acf', 'field_type' => ['repeater'], ], ] ); $this->add_control( 'title', [ 'label' => __('Title', 'bdthemes-element-pack'), 'dynamic' => ['active' => false], 'type' => Dynamic_Select::TYPE, 'label_block' => true, 'placeholder' => __('Type repeater sub field for accordion title', 'bdthemes-element-pack'), 'description' => sprintf(__('Supported field type: <b>%1s</b>, <b>%2s</b>, <b>%3s</b>', 'bdthemes-element-pack'), 'Text','Textarea','WYSIWYG'), 'query_args' => [ 'query' => 'acf', 'field_type' => ['text', 'textarea', 'wysiwyg'], ], ] ); $this->add_control( 'content', [ 'label' => __('Content', 'bdthemes-element-pack'), 'dynamic' => ['active' => false], 'type' => Dynamic_Select::TYPE, 'label_block' => true, 'placeholder' => __('Type repeater sub field for accordion content', 'bdthemes-element-pack'), 'description' => sprintf(__('Supported field type: <b>%1s</b>, <b>%2s</b>, <b>%3s</b>', 'bdthemes-element-pack'), 'Text','Textarea','WYSIWYG'), 'query_args' => [ 'query' => 'acf', 'field_type' => ['text', 'textarea', 'wysiwyg'], ], ] ); $this->add_control( 'title_html_tag', [ 'label' => __('Title HTML Tag', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => element_pack_title_tags(), 'default' => 'div', ] ); $this->add_control( 'accordion_icon', [ 'label' => __('Icon', 'bdthemes-element-pack'), 'type' => Controls_Manager::ICONS, 'fa4compatibility' => 'icon', 'default' => [ 'value' => 'fas fa-plus', 'library' => 'fa-solid', ], 'recommended' => [ 'fa-solid' => [ 'chevron-down', 'angle-down', 'angle-double-down', 'caret-down', 'caret-square-down', ], 'fa-regular' => [ 'caret-square-down', ], ], 'skin' => 'inline', 'label_block' => false, ] ); $this->add_control( 'accordion_active_icon', [ 'label' => __('Active Icon', 'bdthemes-element-pack'), 'type' => Controls_Manager::ICONS, 'fa4compatibility' => 'icon_active', 'default' => [ 'value' => 'fas fa-minus', 'library' => 'fa-solid', ], 'recommended' => [ 'fa-solid' => [ 'chevron-up', 'angle-up', 'angle-double-up', 'caret-up', 'caret-square-up', ], 'fa-regular' => [ 'caret-square-up', ], ], 'skin' => 'inline', 'label_block' => false, 'condition' => [ 'accordion_icon[value]!' => '', ], ] ); $this->add_control( 'show_custom_icon', [ 'label' => esc_html__('Show Title Icon', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'separator' => 'before' ] ); $this->end_controls_section(); // Global controls from trait $this->register_accordion_controls(); } protected function render() { $settings = $this->get_settings_for_display(); $id = 'bdt-ep-accordion-' . $this->get_id(); $this->add_render_attribute( [ 'accordion' => [ 'id' => $id, 'class' => 'bdt-ep-accordion bdt-accordion', 'data-bdt-accordion' => [ wp_json_encode([ "collapsible" => $settings["collapsible"] ? true : false, "multiple" => $settings["multiple"] ? true : false, "transition" => "ease-in-out", ]) ] ] ] ); $this->add_render_attribute( [ 'accordion_data' => [ 'data-settings' => [ wp_json_encode([ "id" => 'bdt-ep-accordion-' . $this->get_id(), 'activeHash' => $settings['active_hash'], 'activeScrollspy' => $settings['active_scrollspy'], 'hashTopOffset' => isset($settings['hash_top_offset']['size']) ? $settings['hash_top_offset']['size'] : false, 'hashScrollspyTime' => isset($settings['hash_scrollspy_time']['size']) ? $settings['hash_scrollspy_time']['size'] : false, ]), ], ], ] ); $migrated = isset($settings['__fa4_migrated']['accordion_icon']); $is_new = empty($settings['icon']) && Icons_Manager::is_migration_allowed(); $active_migrated = isset($settings['__fa4_migrated']['accordion_active_icon']); $active_is_new = empty($settings['icon_active']) && Icons_Manager::is_migration_allowed(); if ($settings['schema_activity'] == 'yes') { $this->add_render_attribute('accordion', 'itemscope'); $this->add_render_attribute('accordion', ['itemtype' => 'https://schema.org/FAQPage']); } ?> <div class="bdt-ep-accordion-container"> <div <?php $this->print_render_attribute_string('accordion'); ?> <?php $this->print_render_attribute_string('accordion_data'); ?>> <?php $repeater_field = get_field_object( $settings['field'] ); if (empty($settings['field'] && $repeater_field)) { return; } $acf_helper = new ACF_Global(); $field_values = $acf_helper->get_acf_field_value( $settings['field'], $repeater_field['parent'] ); if (empty($field_values)) { return; } $title = $settings['title']; $content = $settings['content']; foreach ($field_values as $index => $value) : $acc_count = $index + 1; $field_title = isset($value[$title]) ? $value[$title] : ''; $field_content = isset($value[$content]) ? $value[$content] : ''; if (!empty($field_title) or !empty($field_content)) : $acc_id = ($field_title) ? element_pack_string_id($field_title) : $id . $acc_count; $acc_id = 'bdt-ep-accordion-' . $acc_id; $tab_title_setting_key = 'tab_title'.$index; $tab_content_setting_key = 'tab_content'.$index; $this->add_render_attribute($tab_title_setting_key, [ 'class' => ['bdt-ep-accordion-title bdt-accordion-title bdt-flex bdt-flex-middle'] ]); $this->add_render_attribute($tab_title_setting_key, 'class', ('right' == $settings['icon_align']) ? 'bdt-flex-between' : ''); $this->add_render_attribute($tab_content_setting_key, [ 'class' => ['bdt-ep-accordion-content bdt-accordion-content'], ]); $item_key = 'bdt-item-' . $index; $this->add_render_attribute($item_key, [ 'class' => ($acc_count === $settings['active_item']) ? 'bdt-ep-accordion-item bdt-open' : 'bdt-ep-accordion-item', ]); if ($settings['schema_activity'] == 'yes') { $this->add_render_attribute($item_key, 'itemscope'); $this->add_render_attribute($item_key, 'itemprop', 'mainEntity'); $this->add_render_attribute($item_key, 'itemtype', 'https://schema.org/Question'); $this->add_render_attribute($tab_content_setting_key, 'itemscope'); $this->add_render_attribute($tab_content_setting_key, 'itemprop', 'acceptedAnswer', true); $this->add_render_attribute($tab_content_setting_key, 'itemtype', 'https://schema.org/Answer', true); } ?> <div <?php $this->print_render_attribute_string($item_key); ?>> <<?php echo esc_attr(Utils::get_valid_html_tag($settings['title_html_tag'])); ?> <?php $this->print_render_attribute_string($tab_title_setting_key); ?> id="<?php echo esc_attr(strtolower(preg_replace('#[ -]+#', '-', trim(preg_replace("![^a-z0-9]+!i", " ", esc_attr($acc_id)))))); ?>" data-accordion-index="<?php echo esc_attr($index); ?>" data-title="<?php echo esc_attr(strtolower(preg_replace('#[ -]+#', '-', trim(preg_replace("![^a-z0-9]+!i", " ", esc_html($field_title)))))); ?>"> <?php if ($settings['accordion_icon']['value']) : ?> <span class="bdt-ep-accordion-icon bdt-flex-align-<?php echo esc_attr($settings['icon_align']); ?>" aria-hidden="true"> <?php if ($is_new || $migrated) : ?> <span class="bdt-ep-accordion-icon-closed"> <?php Icons_Manager::render_icon($settings['accordion_icon'], ['aria-hidden' => 'true', 'class' => 'fa-fw']); ?> </span> <?php else : ?> <i class="bdt-ep-accordion-icon-closed <?php echo esc_attr($settings['icon']); ?>" aria-hidden="true"></i> <?php endif; ?> <?php if ($active_is_new || $active_migrated) : ?> <span class="bdt-ep-accordion-icon-opened"> <?php Icons_Manager::render_icon($settings['accordion_active_icon'], ['aria-hidden' => 'true', 'class' => 'fa-fw']); ?> </span> <?php else : ?> <i class="bdt-ep-accordion-icon-opened <?php echo esc_attr($settings['icon_active']); ?>" aria-hidden="true"></i> <?php endif; ?> </span> <?php endif; ?> <span role="heading" class="bdt-ep-title-text bdt-display-inline-block"> <?php if (!empty($item['repeater_icon']['value']) and $settings['show_custom_icon'] == 'yes') : ?> <span class="bdt-ep-accordion-custom-icon"> <?php Icons_Manager::render_icon($item['repeater_icon'], ['aria-hidden' => 'true', 'class' => 'fa-fw']); ?> </span> <?php endif; ?> <?php echo esc_html($field_title); ?> </span> </<?php echo esc_attr(Utils::get_valid_html_tag($settings['title_html_tag'])); ?>> <div <?php $this->print_render_attribute_string($tab_content_setting_key); ?>> <?php echo wp_kses_post($field_content); ?> </div> </div> <?php endif; endforeach; ?> </div> </div> <?php } } <?php namespace ElementPack\Modules\AcfAccordion; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'acf-accordion'; } public function get_widgets() { $widgets = ['Acf_Accordion']; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'ACF Accordion', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => false, 'has_style' => false, 'has_script' => false, ]; <?php namespace ElementPack\Modules\ImageHoverEffects; use Elementor\Controls_Manager; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function __construct() { parent::__construct(); $this->add_actions(); } public function get_name() { return 'bdt-image-hover-effects'; } public function register_section($element) { $element->start_controls_section( 'ep_image_hover_effects_controls', [ 'tab' => Controls_Manager::TAB_STYLE, 'label' => BDTEP_CP . esc_html__('Image Hover Effects', 'bdthemes-element-pack') . BDTEP_NC, ] ); $element->end_controls_section(); } public function register_controls($widget, $args) { $widget->add_control( 'ep_image_hover_effects_on', [ 'label' => esc_html__('Hover Effects?', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, ] ); $widget->add_control( 'hover_effects', [ 'label' => esc_html__('Choose Effect', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => [ '1' => 'Effect 01', '2' => 'Effect 02', '3' => 'Effect 03', '4' => 'Effect 04', '5' => 'Effect 05', '6' => 'Effect 06', ], 'default' => '1', 'prefix_class' => 'bdt-image-hover-effect-wrap bdt-image-hover-effect-', 'condition' => [ 'ep_image_hover_effects_on' => 'yes', ], ] ); $widget->add_control( 'effects_color', [ 'label' => esc_html__('Effects Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => 'rgba(0, 0, 0, .1)', 'condition' => [ 'ep_image_hover_effects_on' => 'yes', ], 'selectors' => [ '{{WRAPPER}}.bdt-image-hover-effect-wrap::before, {{WRAPPER}}.bdt-image-hover-effect-wrap::after' => 'background: {{VALUE}};border-color: {{VALUE}};', ], ] ); $widget->add_control( 'effets_width', [ 'label' => esc_html__('Effects Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px', '%'], 'condition' => [ 'ep_image_hover_effects_on' => 'yes', 'hover_effects' => ['3','5','6'], ], 'selectors' => [ '{{WRAPPER}}.bdt-image-hover-effect-wrap::before, {{WRAPPER}}.bdt-image-hover-effect-wrap::after' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $widget->add_control( 'effects_duration', [ 'label' => esc_html__('Effects Duration(ms)', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['ms'], 'range' => [ 'ms' => [ 'min' => 100, 'max' => 5000, 'step' => 100, ], ], 'default' => [ 'unit' => 'ms', ], 'condition' => [ 'ep_image_hover_effects_on' => 'yes', ], 'selectors' => [ '{{WRAPPER}}.bdt-image-hover-effect-wrap::before, {{WRAPPER}}.bdt-image-hover-effect-wrap::after' => 'transition-duration: {{SIZE}}{{UNIT}};', ], ] ); } public function should_script_enqueue($widget) { if ('yes' === $widget->get_settings_for_display('ep_image_hover_effects_on')) { wp_enqueue_style('ep-image-hover-effects'); } } protected function add_actions() { add_action('elementor/element/image/section_style_image/after_section_end', [$this, 'register_section']); add_action('elementor/element/image/ep_image_hover_effects_controls/before_section_end', [$this, 'register_controls'], 10, 2); // render scripts add_action('elementor/frontend/widget/before_render', [$this, 'should_script_enqueue'], 10, 1); } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Image Hover Effects', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, 'has_style' => true, ]; <?php namespace ElementPack\Modules\ContentProtector; use Elementor\Controls_Manager; use Elementor\Plugin; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function __construct() { parent::__construct(); $this->add_actions(); } public function get_name() { return 'bdt-content-protector'; } public function register_controls( $section ) { $section->start_controls_section( 'element_pack_content_protector_section', [ 'tab' => Controls_Manager::TAB_SETTINGS, 'label' => BDTEP_CP . esc_html__( 'Content Protector', 'bdthemes-element-pack' ), ] ); $section->add_control( 'ep_content_protector_enable', [ 'label' => esc_html__( 'Content Protector?', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'render_type' => 'template', ] ); $section->end_controls_section(); } public function footer_script_render() { if ( Plugin::instance()->editor->is_edit_mode() || Plugin::instance()->preview->is_preview_mode() ) { return; } $document = Plugin::instance()->documents->get( get_the_ID() ); if ( ! $document ) { return; } $custom_js = $document->get_settings( 'ep_content_protector_enable' ); if ( empty( $custom_js ) ) { return; } ?> <script src="<?php echo esc_url( BDTEP_ASSETS_URL ); ?>vendor/js/content-protector.min.js"></script> <?php } protected function add_actions() { add_action( 'elementor/documents/register_controls', [ $this, 'register_controls' ], 1, 1 ); add_action( 'wp_footer', [ $this, 'footer_script_render' ], 999 ); } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Content Protector', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\EddProfileEditor\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Typography; if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } class EDD_Profile_Editor extends Module_Base { public function get_name() { return 'bdt-easy-digital-profile-editor'; } public function get_title() { return BDTEP . esc_html__('EDD Profile Editor', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-edd-profile-editor'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['easy', 'digital', 'downloads', 'software', 'eshop', 'estore', 'profile', 'editor']; } public function get_custom_help_url() { return 'https://youtu.be/z6MSJtvbxPQ'; } protected function register_controls() { $this->start_controls_section( 'section_style_fieldset', [ 'label' => esc_html__('Fieldset', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'fieldset_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} fieldset' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'fieldset_border_style', [ 'label' => __('Border Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'none' => __('None', 'bdthemes-element-pack'), 'solid' => __('Solid', 'bdthemes-element-pack'), 'double' => __('Double', 'bdthemes-element-pack'), 'dotted' => __('Dotted', 'bdthemes-element-pack'), 'dashed' => __('Dashed', 'bdthemes-element-pack'), 'groove' => __('Groove', 'bdthemes-element-pack'), ], 'selectors' => [ '{{WRAPPER}} fieldset' => 'border-style: {{VALUE}};', ], ] ); $this->add_responsive_control( 'fieldset_border_width', [ 'label' => esc_html__('Border Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 10, ], ], 'px' => [ 'size' => 2, ], 'selectors' => [ '{{WRAPPER}} fieldset' => 'border-width: {{SIZE}}px;', ], ] ); // $this->add_responsive_control( // 'fieldset_border_radius', // [ // 'label' => esc_html__('Border Radius', 'bdthemes-element-pack') . BDTEP_NC, // 'type' => Controls_Manager::DIMENSIONS, // 'size_units' => ['px', '%'], // 'selectors' => [ // '{{WRAPPER}} fieldset' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', // ], // ] // ); $this->add_responsive_control( 'fieldset_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} fieldset' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'fieldset_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} fieldset' => 'border-color: {{VALUE}};', ], ] ); $this->add_control( 'caption_color', [ 'label' => esc_html__('Caption Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} legend' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'caption_border', 'label' => esc_html__('Caption Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} legend', 'separator' => 'before', ] ); $this->add_responsive_control( 'caption_radius', [ 'label' => esc_html__('Caption Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} legend' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'caption_padding', [ 'label' => esc_html__('Caption Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} legend' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'caption_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} legend', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_label', [ 'label' => esc_html__('Label', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'label_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form label' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'label_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} #edd_profile_editor_form label', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_input', [ 'label' => esc_html__('Input', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'input_placeholder_color', [ 'label' => esc_html__('Placeholder Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input::placeholder' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_profile_editor_form textarea::placeholder' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'input_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_profile_editor_form .wpcf7-textarea' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'others_type_input_text_color', [ 'label' => esc_html__('Others Type Input Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#666666', 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form.select-state' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_profile_editor_form.select-gender' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_profile_editor_form.accept-this-1' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'input_text_background', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input' => 'background-color: {{VALUE}};', '{{WRAPPER}} #edd_profile_editor_form .wpcf7-textarea' => 'background-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'textarea_height', [ 'label' => esc_html__('Textarea Height', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 125, ], 'range' => [ 'px' => [ 'min' => 30, 'max' => 500, ], ], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form .wpcf7-textarea' => 'height: {{SIZE}}{{UNIT}}; display: block;', ], 'separator' => 'before', ] ); $this->add_control( 'input_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input, {{WRAPPER}} #edd_profile_editor_form .wpcf7-textarea, {{WRAPPER}} #edd_profile_editor_form .select.edd-select' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'input_space', [ 'label' => esc_html__('Element Space', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 25, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form-control' => 'margin-top: {{SIZE}}{{UNIT}};', '{{WRAPPER}} #edd_profile_editor_form' => 'margin-top: -{{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'input_border_show', [ 'label' => esc_html__('Border Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'input_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #edd_profile_editor_form input, {{WRAPPER}} #edd_profile_editor_form textarea, {{WRAPPER}} #edd_profile_editor_form .select.edd-select', 'condition' => [ 'input_border_show' => 'yes', ], ] ); $this->add_control( 'input_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', '{{WRAPPER}} #edd_profile_editor_form textarea' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', '{{WRAPPER}} #edd_profile_editor_form .select.edd-select' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_submit_button', [ 'label' => esc_html__('Submit Button', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_button_style'); $this->start_controls_tab( 'tab_button_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'background_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit', 'separator' => 'before', ] ); $this->add_control( 'border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_shadow', 'selector' => '{{WRAPPER}} #edd_profile_editor_submit', ] ); $this->add_control( 'text_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'button_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit', 'separator' => 'before', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_button_hover', [ 'label' => esc_html__('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'hover_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit:hover' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'button_background_hover_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'button_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit:hover' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_additional_option', [ 'label' => esc_html__('Additional Option', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'fullwidth_input', [ 'label' => esc_html__('Fullwidth Input', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => esc_html__('On', 'bdthemes-element-pack'), 'label_off' => esc_html__('Off', 'bdthemes-element-pack'), 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input[type*="text"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="email"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="url"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="number"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="tel"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="date"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="password"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form .select.edd-select' => 'width: 100%;', ], ] ); $this->add_control( 'fullwidth_textarea', [ 'label' => esc_html__('Fullwidth Texarea', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => esc_html__('On', 'bdthemes-element-pack'), 'label_off' => esc_html__('Off', 'bdthemes-element-pack'), 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form textarea' => 'width: 100%;', ], ] ); $this->add_control( 'fullwidth_button', [ 'label' => esc_html__('Fullwidth Button', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => esc_html__('On', 'bdthemes-element-pack'), 'label_off' => esc_html__('Off', 'bdthemes-element-pack'), 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit' => 'width: 100%;', ], ] ); $this->end_controls_section(); } protected function render() { echo do_shortcode('[edd_profile_editor]'); } } <?php namespace ElementPack\Modules\EddProfileEditor; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'easy-digital-profile-editor'; } public function get_widgets() { $widgets = [ 'EDD_Profile_Editor', ]; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('EDD Profile Editor', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, ]; <?php namespace ElementPack\Modules\BbpressSingleView\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Background; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Bbpress_Single_View extends Module_Base { public function get_name() { return 'bdt-bbpress-single-view'; } public function get_title() { return BDTEP . esc_html__('bbPress Single View', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-bbpress-single-view'; } public function get_categories() { return ['element-pack-bbpress']; } public function get_keywords() { return ['bbpress', 'forum', 'community', 'discussion', 'support']; } public function get_custom_help_url() { return 'https://youtu.be/7vkAHZ778c4'; } public function register_controls() { $this->start_controls_section( 'section_bbpress_content', [ 'label' => __('Content', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'bbpress_specific_view', [ 'label' => __('Single View', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => [ 'popular' => __('Popular', 'bdthemes-element-pack'), 'no-replies' => __('No Replies', 'bdthemes-element-pack'), ], 'default' => 'popular', 'dynamic' => ['active' => true], ] ); $this->add_control( 'show_breadcrumb', [ 'label' => __('Show Breadcrumb', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before' ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'section_style_bbpress_breadcrumb', [ 'label' => esc_html__('Breadcrumb', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'breadcrumb_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-breadcrumb *' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'breadcrumb_hover_color', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-breadcrumb a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'breadcrumb_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bbp-breadcrumb' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'breadcrumb_typography', 'selector' => '{{WRAPPER}} .bbp-breadcrumb', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_search', [ 'label' => esc_html__('Search', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_bbpress_search_style'); $this->start_controls_tab( 'tab_bbpress_search_input', [ 'label' => __('Input', 'bdthemes-element-pack'), ] ); $this->add_control( 'input_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} input[type="text"], {{WRAPPER}} input[type="date"], {{WRAPPER}} input[type="email"], {{WRAPPER}} input[type="number"], {{WRAPPER}} input[type="password"], {{WRAPPER}} input[type="search"], {{WRAPPER}} input[type="tel"], {{WRAPPER}} input[type="url"], {{WRAPPER}} select, {{WRAPPER}} textarea' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'input_text_background', 'selector' => '{{WRAPPER}} input[type="text"], {{WRAPPER}} input[type="date"], {{WRAPPER}} input[type="email"], {{WRAPPER}} input[type="number"], {{WRAPPER}} input[type="password"], {{WRAPPER}} input[type="search"], {{WRAPPER}} input[type="tel"], {{WRAPPER}} input[type="url"], {{WRAPPER}} select, {{WRAPPER}} textarea', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'input_border', 'label' => esc_html__('Border', 'elementor-addons'), 'fields_options' => [ 'border' => [ 'default' => 'solid', ], 'width' => [ 'default' => [ 'top' => '1', 'right' => '1', 'bottom' => '1', 'left' => '1', 'unit' => 'px', 'isLinked' => false, ], ], 'color' => [ 'default' => '#c0c0c0', ], ], 'selector' => '{{WRAPPER}} input[type="text"], {{WRAPPER}} input[type="date"], {{WRAPPER}} input[type="email"], {{WRAPPER}} input[type="number"], {{WRAPPER}} input[type="password"], {{WRAPPER}} input[type="search"], {{WRAPPER}} input[type="tel"], {{WRAPPER}} input[type="url"], {{WRAPPER}} select, {{WRAPPER}} textarea', ] ); $this->add_responsive_control( 'input_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} input[type="text"], {{WRAPPER}} input[type="date"], {{WRAPPER}} input[type="email"], {{WRAPPER}} input[type="number"], {{WRAPPER}} input[type="password"], {{WRAPPER}} input[type="search"], {{WRAPPER}} input[type="tel"], {{WRAPPER}} input[type="url"], {{WRAPPER}} select, {{WRAPPER}} textarea' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'input_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} input[type="text"], {{WRAPPER}} input[type="date"], {{WRAPPER}} input[type="email"], {{WRAPPER}} input[type="number"], {{WRAPPER}} input[type="password"], {{WRAPPER}} input[type="search"], {{WRAPPER}} input[type="tel"], {{WRAPPER}} input[type="url"], {{WRAPPER}} select, {{WRAPPER}} textarea' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'input_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} input[type="text"], {{WRAPPER}} input[type="date"], {{WRAPPER}} input[type="email"], {{WRAPPER}} input[type="number"], {{WRAPPER}} input[type="password"], {{WRAPPER}} input[type="search"], {{WRAPPER}} input[type="tel"], {{WRAPPER}} input[type="url"], {{WRAPPER}} select, {{WRAPPER}} textarea', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_bbpress_search_submit', [ 'label' => __('Submit', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_normal_heading', [ 'label' => esc_html__('NORMAL', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, ] ); $this->add_control( 'button_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .bbp-search-form .button' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'button_background_color', 'selector' => '{{WRAPPER}} .bbp-search-form .button', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'button_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bbp-search-form .button', 'separator' => 'before', ] ); $this->add_responsive_control( 'button_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bbp-search-form .button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bbp-search-form .button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bbp-search-form' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'button_typography', 'selector' => '{{WRAPPER}} .bbp-search-form .button', ] ); $this->add_control( 'button_hover_heading', [ 'label' => esc_html__('HOVER', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] ); $this->add_control( 'button_hover_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-search-form .button:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'button_background_color_hover', 'selector' => '{{WRAPPER}} .bbp-search-form .button:hover', ] ); $this->add_control( 'button_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'button_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bbp-search-form .button:hover' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_header', [ 'label' => esc_html__('Header', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'header_title_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums li.bbp-header li' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'header_background', 'selector' => '{{WRAPPER}} #bbpress-forums li.bbp-header', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'header_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #bbpress-forums li.bbp-header', 'separator' => 'before', ] ); $this->add_responsive_control( 'header_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums li.bbp-header' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'header_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums li.bbp-header' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'header_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums li.bbp-header' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'header_typography', 'selector' => '{{WRAPPER}} #bbpress-forums li.bbp-header li', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_body', [ 'label' => esc_html__('Body', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'forum_body_odd_color', [ 'label' => esc_html__('Odd Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums div.odd, {{WRAPPER}} #bbpress-forums ul.odd' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'forum_body_even_color', [ 'label' => esc_html__('Even Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums div.even, {{WRAPPER}} #bbpress-forums ul.even' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'forum_body_list_border_color', [ 'label' => esc_html__('Odd/Even Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums li.bbp-body ul.forum' => 'border-top-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'odd_even_forum_body_padding', [ 'label' => esc_html__( 'Odd/Even Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} #bbpress-forums div.even, {{WRAPPER}} #bbpress-forums ul.even, {{WRAPPER}} #bbpress-forums div.odd, {{WRAPPER}} #bbpress-forums ul.odd' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'forum_body_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-topics', 'separator' => 'before', ] ); $this->add_responsive_control( 'forum_body_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topics' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'forum_body_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topics' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'forum_body_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topics' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_forum_title', [ 'label' => esc_html__('Forum Title', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'forum_title_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-permalink' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'forum_title_color_hover', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-permalink:hover' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'forum_title_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-permalink' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'forum_title_typography', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-topic-permalink', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_forum_count', [ 'label' => esc_html__('Voices/Posts Count', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'forum_count_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-topic-voice-count, {{WRAPPER}} .bbp-topic-reply-count' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'forum_count_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bbp-topic-voice-count, {{WRAPPER}} .bbp-topic-reply-count' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'forum_count_typography', 'selector' => '{{WRAPPER}} .bbp-topic-voice-count, {{WRAPPER}} .bbp-topic-reply-count', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_forum_meta', [ 'label' => esc_html__('Forum Meta', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'forum_meta_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-freshness a, {{WRAPPER}} #bbpress-forums .bbp-topic-meta *' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'forum_meta_color_hover', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-freshness a:hover, {{WRAPPER}} #bbpress-forums .bbp-topic-meta a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'forum_meta_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-freshness a, {{WRAPPER}} #bbpress-forums .bbp-topic-meta *' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'forum_meta_typography', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-topic-freshness a, {{WRAPPER}} #bbpress-forums .bbp-topic-meta *', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_pagination', [ 'label' => esc_html__('Pagination', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'pagination_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-pagination-count' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'pagination_text_typography', 'label' => esc_html__('Text Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bbp-pagination-count', ] ); $this->start_controls_tabs('tabs_button_style'); $this->start_controls_tab( 'tab_button_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'pagination_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'pagination_background_color', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'pagination_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current', 'separator' => 'before', ] ); $this->add_responsive_control( 'pagination_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'pagination_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'pagination_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'pagination_typography', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'pagination_box_shadow', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_pagination_hover', [ 'label' => esc_html__('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'pagination_hover_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'pagination_background_color_hover', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a:hover:hover', ] ); $this->add_control( 'pagination_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'pagination_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a:hover:hover' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_pagination_active', [ 'label' => esc_html__('Active', 'bdthemes-element-pack'), ] ); $this->add_control( 'pagination_active_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'pagination_background_color_active', 'selector' => '#bbpress-forums .bbp-pagination-links span.current', ] ); $this->add_control( 'pagination_active_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'pagination_border_border!' => '', ], 'selectors' => [ '#bbpress-forums .bbp-pagination-links span.current' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } protected function render_pagination_topics() { do_action('bbp_template_before_pagination_loop'); ?> <div class="bbp-pagination"> <div class="bbp-pagination-count"><?php bbp_forum_pagination_count(); ?></div> <div class="bbp-pagination-links"><?php bbp_forum_pagination_links(); ?></div> </div> <?php do_action('bbp_template_after_pagination_loop'); } protected function render_feedback_no_topics() { ?> <div class="bbp-template-notice"> <ul> <li><?php esc_html_e('Oh, bother! No topics were found here.', 'bbpress'); ?></li> </ul> </div> <?php } protected function render_loop_single_topic() { ?> <ul id="bbp-topic-<?php bbp_topic_id(); ?>" <?php bbp_topic_class(); ?>> <li class="bbp-topic-title"> <?php if (bbp_is_user_home()) : ?> <?php if (bbp_is_favorites()) : ?> <span class="bbp-row-actions"> <?php do_action('bbp_theme_before_topic_favorites_action'); ?> <?php bbp_topic_favorite_link(array('before' => '', 'favorite' => '+', 'favorited' => '×')); ?> <?php do_action('bbp_theme_after_topic_favorites_action'); ?> </span> <?php elseif (bbp_is_subscriptions()) : ?> <span class="bbp-row-actions"> <?php do_action('bbp_theme_before_topic_subscription_action'); ?> <?php bbp_topic_subscription_link(array('before' => '', 'subscribe' => '+', 'unsubscribe' => '×')); ?> <?php do_action('bbp_theme_after_topic_subscription_action'); ?> </span> <?php endif; ?> <?php endif; ?> <?php do_action('bbp_theme_before_topic_title'); ?> <a class="bbp-topic-permalink" href="<?php bbp_topic_permalink(); ?>"><?php bbp_topic_title(); ?></a> <?php do_action('bbp_theme_after_topic_title'); ?> <?php bbp_topic_pagination(); ?> <?php do_action('bbp_theme_before_topic_meta'); ?> <p class="bbp-topic-meta"> <?php do_action('bbp_theme_before_topic_started_by'); ?> <span class="bbp-topic-started-by"><?php printf(esc_html__('Started by: %1$s', 'bbpress'), bbp_get_topic_author_link(array('size' => '14'))); ?></span> <?php do_action('bbp_theme_after_topic_started_by'); ?> <?php if (!bbp_is_single_forum() || (bbp_get_topic_forum_id() !== bbp_get_forum_id())) : ?> <?php do_action('bbp_theme_before_topic_started_in'); ?> <span class="bbp-topic-started-in"><?php printf(esc_html__('in: %1$s', 'bbpress'), '<a href="' . bbp_get_forum_permalink(bbp_get_topic_forum_id()) . '">' . bbp_get_forum_title(bbp_get_topic_forum_id()) . '</a>'); ?></span> <?php do_action('bbp_theme_after_topic_started_in'); ?> <?php endif; ?> </p> <?php do_action('bbp_theme_after_topic_meta'); ?> <?php bbp_topic_row_actions(); ?> </li> <li class="bbp-topic-voice-count"><?php bbp_topic_voice_count(); ?></li> <li class="bbp-topic-reply-count"><?php bbp_show_lead_topic() ? bbp_topic_reply_count() : bbp_topic_post_count(); ?></li> <li class="bbp-topic-freshness"> <?php do_action('bbp_theme_before_topic_freshness_link'); ?> <?php bbp_topic_freshness_link(); ?> <?php do_action('bbp_theme_after_topic_freshness_link'); ?> <p class="bbp-topic-meta"> <?php do_action('bbp_theme_before_topic_freshness_author'); ?> <span class="bbp-topic-freshness-author"><?php bbp_author_link(array('post_id' => bbp_get_topic_last_active_id(), 'size' => 14)); ?></span> <?php do_action('bbp_theme_after_topic_freshness_author'); ?> </p> </li> </ul><!-- #bbp-topic-<?php bbp_topic_id(); ?> --> <?php } protected function render_loop_topics() { do_action('bbp_template_before_topics_loop'); ?> <ul id="bbp-forum-<?php bbp_forum_id(); ?>" class="bbp-topics"> <li class="bbp-header"> <ul class="forum-titles"> <li class="bbp-topic-title"><?php esc_html_e('Topic', 'bbpress'); ?></li> <li class="bbp-topic-voice-count"><?php esc_html_e('Voices', 'bbpress'); ?></li> <li class="bbp-topic-reply-count"><?php bbp_show_lead_topic() ? esc_html_e('Replies', 'bbpress') : esc_html_e('Posts', 'bbpress'); ?></li> <li class="bbp-topic-freshness"><?php esc_html_e('Last Post', 'bbpress'); ?></li> </ul> </li> <li class="bbp-body"> <?php while (bbp_topics()) : bbp_the_topic(); ?> <?php $this->render_loop_single_topic(); ?> <?php endwhile; ?> </li> <li class="bbp-footer"> <div class="tr"> <p> <span class="td colspan<?php echo (bbp_is_user_home() && (bbp_is_favorites() || bbp_is_subscriptions())) ? '5' : '4'; ?>"> </span> </p> </div> </li> </ul> <?php do_action('bbp_template_after_topics_loop'); } protected function render_content_single_view() { $settings = $this->get_settings_for_display(); ?> <div id="bbpress-forums" class="bbpress-wrapper"> <?php if ($settings['show_breadcrumb']) : ?> <?php bbp_breadcrumb(); ?> <?php endif; ?> <?php bbp_set_query_name(bbp_get_view_rewrite_id()); ?> <?php if (bbp_view_query()) : ?> <?php $this->render_pagination_topics(); ?> <?php $this->render_loop_topics(); ?> <?php $this->render_pagination_topics(); ?> <?php else : ?> <?php $this->render_feedback_no_topics(); ?> <?php endif; ?> <?php //bbp_reset_query_name(); ?> </div> <?php } public function render() { $settings = $this->get_settings_for_display(); // Sanity check required info // if (empty($settings['bbpress_topic_id'])) { // return element_pack_alert('Ops, Your topic ID is Missing, Please enter your specific topic ID'); // } // Set passed attribute to $view_id for clarity $view_id = $settings['bbpress_specific_view']; // Start output buffer bbp_set_query_name('bbp_single_view'); // Set the current view ID bbpress()->current_view_id = $view_id; // Load the view bbp_view_query($view_id); // Output template $this->render_content_single_view(); // reset query wp_reset_postdata(); } } <?php namespace ElementPack\Modules\BbpressSingleView; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'bbpress-single-view'; } public function get_widgets() { $widgets = [ 'Bbpress_Single_View', ]; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('bbPress Single View', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, ]; <?php if ( $_REQUEST['show_lightbox'] ) { $link_url = esc_url( $insta_feeds[ $i ]['image']['large'] ); } else { $link_url = esc_url( $insta_feeds[ $i ]['link'] ); } ?> <div class="bdt-instagram-item-wrapper feed-type-<?php echo esc_attr( $insta_feeds[ $i ]['post_type'] ); ?>"> <div class="bdt-instagram-item bdt-position-relative bdt-scrollspy-inview bdt-animation-fade"> <div class="bdt-instagram-thumbnail"> <img src="<?php echo esc_attr( $insta_feeds[ $i ]['image']['medium'] ); ?>" alt="<?php esc_html_e( 'Image by:', 'bdthemes-element-pack' ); ?> <?php echo esc_attr( $insta_feeds[ $i ]['user']['full_name'] ); ?> " loading="lazy"> </div> <?php if ( $_REQUEST['show_lightbox'] or $_REQUEST['show_link'] ) : ?> <a class="bdt-position-center bdt-lightbox-icon bdt-icon" href="<?php echo esc_url( $link_url ); ?>" data-elementor-open-lightbox="no"> <svg xmlns="http://www.w3.org/2000/svg" height="32" width="32" x="0px" y="0px" viewBox="0 0 42 42" xml:space="preserve"> <polygon points="42,19 23,19 23,0 19,0 19,19 0,19 0,23 19,23 19,42 23,42 23,23 42,23 " /> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> </svg> </a> <?php endif; ?> <div class='bdt-instagram-like-comment'> <?php if ( $_REQUEST['show_like'] ) : ?> <span class="bdt-icons"><span class='far fa-heart'></span> <b> <?php echo esc_attr( $insta_feeds[ $i ]['like'] ); ?> </b></span> <?php endif; ?> <?php if ( $_REQUEST['show_comment'] ) : ?> <span class="bdt-icons"><span class='far fa-comment'></span> <b> <?php echo esc_attr( $insta_feeds[ $i ]['comment']['count'] ); ?> </b></span> <?php endif; ?> </div> </div> </div><?php namespace ElementPack\Modules\Instagram\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Core\Schemes; use Elementor\Group_Control_Border; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Css_Filter; use ElementPack\Modules\Instagram\Skins; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Instagram extends Module_Base { public function get_name() { return 'bdt-instagram'; } public function get_title() { return BDTEP . esc_html__( 'Instagram', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-instagram-feed'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'instagram', 'gallery', 'photos', 'images' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-styles' ]; } else { return [ 'ep-font', 'ep-instagram' ]; } } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-scripts' ]; } else { return [ 'ep-instagram' ]; } } public function get_custom_help_url() { return 'https://youtu.be/uj9WpuFIZb8'; } public function register_skins() { $this->add_skin( new Skins\Skin_Carousel( $this ) ); } protected function register_controls() { $this->start_controls_section( 'section_content_layout', [ 'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ), ] ); $this->add_control( 'instagram_user_token', [ 'label' => esc_html__( 'Instagram Token (Optional)', 'bdthemes-element-pack' ), 'description' => esc_html__( 'Enter instagram User Token if you want to show separated user\'s photos', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'label_block' => true, ] ); $this->add_control( 'masonry', [ 'label' => esc_html__( 'Masonry', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ '_skin' => '', ], ] ); $this->add_control( 'item_ratio', [ 'label' => esc_html__( 'Image Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 250, ], 'range' => [ 'px' => [ 'min' => 50, 'max' => 500, 'step' => 5, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram-thumbnail *' => 'height: {{SIZE}}px', ], 'condition' => [ 'masonry!' => 'yes', ], ] ); $this->add_responsive_control( 'columns', [ 'label' => esc_html__( 'Columns', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => '4', 'tablet_default' => '3', 'mobile_default' => '2', 'options' => [ '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', ], ] ); $this->add_control( 'items', [ 'label' => esc_html__( 'Item Limit', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 1, 'max' => 100, ], ], 'default' => [ 'size' => 12, ], ] ); $this->add_control( 'column_gap', [ 'label' => esc_html__( 'Column Gap', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'small', 'options' => [ 'small' => esc_html__( 'Small', 'bdthemes-element-pack' ), 'medium' => esc_html__( 'Medium', 'bdthemes-element-pack' ), 'large' => esc_html__( 'Large', 'bdthemes-element-pack' ), 'collapse' => esc_html__( 'Collapse', 'bdthemes-element-pack' ), ], ] ); // $this->add_control( // 'show_profile', // [ // 'label' => esc_html__( 'Profile', 'bdthemes-element-pack' ), // 'type' => Controls_Manager::SWITCHER, // 'condition' => [ // 'layout!' => 'carousel', // ], // ] // ); // $this->add_control( // 'show_loadmore', // [ // 'label' => esc_html__('Show Load More', 'bdthemes-element-pack'), // 'type' => Controls_Manager::SWITCHER, // ] // ); $this->add_control( 'show_follow_me', [ 'label' => esc_html__( 'Follow Me', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ '_skin' => 'bdt-instagram-carousel', ], ] ); $this->add_control( 'follow_me_text', [ 'label' => esc_html__( 'Follow Me Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'placeholder' => esc_html__( 'follow me @', 'bdthemes-element-pack' ), 'default' => esc_html__( 'follow me @', 'bdthemes-element-pack' ), 'condition' => [ '_skin' => 'bdt-instagram-carousel', 'show_follow_me' => 'yes', ], ] ); $this->add_control( 'show_lightbox', [ 'label' => esc_html__( 'Lightbox', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before', ] ); $this->add_control( 'lightbox_animation', [ 'label' => esc_html__( 'Lightbox Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'slide', 'options' => [ 'slide' => esc_html__( 'Slide', 'bdthemes-element-pack' ), 'fade' => esc_html__( 'Fade', 'bdthemes-element-pack' ), 'scale' => esc_html__( 'Scale', 'bdthemes-element-pack' ), ], 'condition' => [ 'show_lightbox' => 'yes', ], //'separator' => 'before', ] ); $this->add_control( 'lightbox_autoplay', [ 'label' => __( 'Lightbox Autoplay', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'show_lightbox' => 'yes', ] ] ); $this->add_control( 'lightbox_pause', [ 'label' => __( 'Lightbox Pause on Hover', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'show_lightbox' => 'yes', ], ] ); $this->add_control( 'show_link', [ 'label' => esc_html__( 'Link Image to Post', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'show_lightbox!' => 'yes', ], ] ); $this->add_control( 'target_blank', [ 'label' => esc_html__( 'Open in new window', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'show_lightbox!' => 'yes', 'show_link' => 'yes', ], ] ); $this->add_control( 'show_overlay', [ 'label' => esc_html__( 'Show Overlay', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'conditions' => [ 'relation' => 'or', 'terms' => [ [ 'name' => 'show_lightbox', 'value' => 'yes', ], [ 'name' => 'show_link', 'value' => 'yes', ], ], ], ] ); $this->add_control( 'show_link_icon', [ 'label' => esc_html__( 'Link Icon', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'show_overlay' => 'yes' ], ] ); $this->add_control( 'alignment', [ 'label' => __( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], ], 'default' => 'center', 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-instagram-profile' => 'text-align: {{VALUE}}', ], 'condition' => [ 'show_profile' => 'yes', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_additional', [ 'label' => esc_html__( 'Additional Settings', 'bdthemes-element-pack' ), ] ); $this->add_control( 'cache_gallery', [ 'label' => esc_html__( 'Cache the Gallery', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'cache_time', [ 'label' => esc_html__( 'Cache Time', 'bdthemes-element-pack' ), 'description' => esc_html__( 'How much hour(s) you want to cache.', 'bdthemes-element-pack' ), 'type' => Controls_Manager::NUMBER, 'default' => 12, 'condition' => [ 'cache_gallery' => 'yes' ] ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_item', [ 'label' => __( 'Item', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs( 'tabs_item_style' ); $this->start_controls_tab( 'tab_item_normal', [ 'label' => __( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'item_background', [ 'label' => __( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-instagram-item' => 'background-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'item_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-instagram-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'item_border', 'label' => __( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-instagram .bdt-instagram-item', 'separator' => 'before', ] ); $this->add_control( 'item_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-instagram-item, {{WRAPPER}} .bdt-instagram .bdt-overlay.bdt-overlay-default, {{WRAPPER}} .bdt-instagram .swiper-carousel' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_control( 'image_section_layout', [ 'label' => __( 'Image/Video', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'image_border', 'label' => __( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-instagram .bdt-instagram-thumbnail *', ] ); $this->add_control( 'image_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-instagram-thumbnail *' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters', 'selector' => '{{WRAPPER}} .bdt-instagram .bdt-instagram-item img, {{WRAPPER}} .bdt-instagram .bdt-instagram-item video', ] ); $this->add_control( 'item_opacity', [ 'label' => __( 'Opacity (%)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-instagram-item img, {{WRAPPER}} .bdt-instagram .bdt-instagram-item video' => 'opacity: {{SIZE}};', ], ] ); $this->add_control( 'shadow_mode', [ 'label' => esc_html__( 'Shadow Mode', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'prefix_class' => 'bdt-ep-shadow-mode-', 'condition' => [ '_skin' => 'bdt-instagram-carousel', ], ] ); $this->add_control( 'shadow_color', [ 'label' => esc_html__( 'Shadow Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .elementor-widget-container:before' => 'background: linear-gradient(to right, {{VALUE}} 0%,rgba(255,255,255,0) 100%);', '{{WRAPPER}} .elementor-widget-container:after' => 'background: linear-gradient(to right, rgba(255,255,255,0) 0%, {{VALUE}} 100%);', ], 'conditions' => [ 'terms' => [ [ 'name' => '_skin', 'value' => 'bdt-instagram-carousel', ], [ 'name' => 'shadow_mode', 'value' => 'yes', ], ], ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_item_hover', [ 'label' => __( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'item_hover_border_color', [ 'label' => __( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'item_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-instagram-item:hover' => 'border-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters_hover', 'selector' => '{{WRAPPER}} .bdt-instagram .bdt-instagram-item:hover img, {{WRAPPER}} .bdt-instagram .bdt-instagram-item:hover video', ] ); $this->add_control( 'item_hover_opacity', [ 'label' => __( 'Opacity (%)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-instagram-item:hover img, {{WRAPPER}} .bdt-instagram .bdt-instagram-item:hover video' => 'opacity: {{SIZE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_overlay', [ 'label' => esc_html__( 'Overlay', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'conditions' => [ 'relation' => 'or', 'terms' => [ [ 'name' => 'show_lightbox', 'value' => 'yes', ], [ 'name' => 'show_link', 'value' => 'yes', ], ], ], ] ); $this->add_responsive_control( 'overlay_gap', [ 'label' => __( 'Overlay Gap', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 15 ], 'selectors' => [ '{{WRAPPER}} .bdt-overlay' => 'margin: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'overlay_icon_size', [ 'label' => __( 'Overlay Icon Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 40 ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-instagram-item .bdt-overlay span' => 'font-size: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'overlay_background', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-overlay.bdt-overlay-default' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'overlay_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-instagram-item.bdt-transition-toggle *' => 'color: {{VALUE}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_follow_me', [ 'label' => __( 'Follow Me', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'conditions' => [ 'terms' => [ [ 'name' => '_skin', 'value' => 'bdt-instagram-carousel', ], [ 'name' => 'show_follow_me', 'value' => 'yes', ], ], ], ] ); $this->add_control( 'follow_me_background', [ 'label' => __( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-instagram-follow-me a' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'follow_me_text_color', [ 'label' => __( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-instagram-follow-me a' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'follow_me_text_hover_color', [ 'label' => __( 'Text Hover Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-instagram-follow-me a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'follow_me_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram-follow-me a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'follow_me_border', 'label' => __( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-instagram-follow-me a', 'separator' => 'before', ] ); $this->add_control( 'follow_me_radius', [ 'label' => __( 'Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram-follow-me a' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'follow_me_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} .bdt-instagram-follow-me a', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_navigation', [ 'label' => __( 'Navigation', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => 'bdt-instagram-carousel', ], ] ); $this->add_control( 'arrows_size', [ 'label' => __( 'Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 12, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-slidenav-previous svg, {{WRAPPER}} .bdt-instagram .bdt-slidenav-next svg' => 'height: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}}', ], ] ); $this->add_control( 'arrows_color', [ 'label' => __( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-slidenav-previous svg, {{WRAPPER}} .bdt-instagram .bdt-slidenav-next svg' => 'color: {{VALUE}}', ], ] ); $this->add_control( 'arrows_hover_color', [ 'label' => __( 'Hover Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-slidenav-previous:hover svg, {{WRAPPER}} .bdt-instagram .bdt-slidenav-next:hover svg' => 'color: {{VALUE}}', ], ] ); $this->add_control( 'arrows_background', [ 'label' => __( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-slidenav-previous, {{WRAPPER}} .bdt-instagram .bdt-slidenav-next' => 'background-color: {{VALUE}}', ], ] ); $this->add_control( 'arrows_hover_background', [ 'label' => __( 'Hover Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-slidenav-previous:hover, {{WRAPPER}} .bdt-instagram .bdt-slidenav-next:hover' => 'background-color: {{VALUE}}', ], ] ); $this->add_responsive_control( 'arrows_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-slidenav-previous' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', '{{WRAPPER}} .bdt-instagram .bdt-slidenav-next' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'arrows_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-instagram .bdt-slidenav-previous, {{WRAPPER}} .bdt-instagram .bdt-slidenav-next' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'arrows_position', [ 'label' => __( 'Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 150, 'step' => 5, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-slidenav-previous' => 'transform: translateY(-50%) translateY(0px) translateX(-{{SIZE}}px);', '{{WRAPPER}} .bdt-slidenav-next' => 'transform: translateY(-50%) translateY(0px) translateX({{SIZE}}px);', ], ] ); $this->end_controls_section(); } protected function filter_response( $response ) { if ( is_wp_error( $response ) ) { $response = array( 'status' => 422, 'message' => $response->get_error_message() ); } else { $response = array( 'status' => wp_remote_retrieve_response_code( $response ), 'message' => wp_remote_retrieve_response_message( $response ), 'body' => json_decode( wp_remote_retrieve_body( $response ) ), ); } return (object) $response; } protected function remote_get( $url ) { $response = wp_remote_get( $url, array( 'timeout' => 100, 'user-agent' => $_SERVER['HTTP_USER_AGENT'], ) ); return $this->filter_response( $response ); } protected $graph_url = 'https://graph.instagram.com'; protected function get_access_token_transit_key( $user_token ) { $widget_id = strtolower( $this->get_id() ); $user_token = md5( $user_token ); $user_token = strtolower( $user_token ); return 'ep_instagram_long_lived_access_token_' . $widget_id . '_' . $user_token; } protected function get_instagram_account_transit_key( $user_token ) { $widget_id = strtolower( $this->get_id() ); $user_token = md5( $user_token ); $user_token = strtolower( $user_token ); return 'ep_instagram_account_id_' . $widget_id . '_' . $user_token; } protected function get_instagram_media_data_transit_key( $user_token ) { $widget_id = strtolower( $this->get_id() ); $user_token = md5( $user_token ); $user_token = strtolower( $user_token ); return 'ep_instagram_media_data_' . $widget_id . '_' . $user_token; } protected function refresh_token( $token, $app_secret ) { $url = $this->graph_url . "/refresh_access_token?grant_type=ig_refresh_token&access_token=$token"; $result = $this->remote_get( $url ); if ( $result->status == 200 ) { return $result->body->access_token; } return $result; } protected function get_access_token( $user_token, $app_secret ) { $cache_key = $this->get_access_token_transit_key( $user_token ); $accessTokenData = get_transient( $cache_key ); if ( ! $accessTokenData ) { $url = $this->graph_url . "/refresh_access_token?grant_type=ig_refresh_token&&access_token=$user_token"; $result = $this->remote_get( $url ); if ( $result->status == 200 ) { $accessTokenData = $result->body->access_token; $accessTokenData = $accessTokenData . '_bdthemes_' . time(); set_transient( $cache_key, $accessTokenData ); } else { return $result; } } $accessTokenArr = explode( '_bdthemes_', $accessTokenData ); if ( count( $accessTokenArr ) == 2 ) { $access_token = $accessTokenArr[0]; $generatedTime = $accessTokenArr[1]; $now = time(); // or your date as well $datediff = $now - $generatedTime; $totalDays = round( $datediff / ( 60 * 60 * 24 ) ); if ( $totalDays > 40 ) { $access_token = $this->refresh_token( $access_token, $app_secret ); if ( is_string( $access_token ) ) { $accessTokenData = $access_token . '_bdthemes_' . time(); set_transient( $cache_key, $accessTokenData ); } } return $access_token; } } private function get_instagram_account_id( $access_token ) { $cache_key = $this->get_instagram_account_transit_key( $access_token ); $account_id = get_transient( $cache_key ); if ( ! $account_id ) { $url = $this->graph_url . "/me?fields=id&access_token=$access_token"; $result = $this->remote_get( $url ); if ( $result->status == 200 ) { $account_id = $result->body->id; set_transient( $cache_key, $account_id, DAY_IN_SECONDS * 100 ); } else { return $result; } } return $account_id; } public function get_media( $access_token, $account_id ) { $cache_key = $this->get_instagram_media_data_transit_key( $access_token ); $settings = $this->get_settings_for_display(); $data = ''; $isCacheEnabled = isset( $settings['cache_gallery'] ) && $settings['cache_gallery'] == 'yes'; if ( $isCacheEnabled ) { $data = get_transient( $cache_key ); } else { delete_transient( $cache_key ); } if ( ! $data ) { $url = $this->graph_url . "/$account_id/media?fields=id,media_type,media_url,permalink,username,timestamp&access_token=$access_token&limit=100"; $result = $this->remote_get( $url ); if ( $result->status == 200 ) { $data = $result->body; if ( $isCacheEnabled ) { $cache_time = isset( $settings['cache_time'] ) ? intval( $settings['cache_time'] ) : 1; if ( $cache_time < 1 ) { $cache_time = 1; } set_transient( $cache_key, $data, ( HOUR_IN_SECONDS * $cache_time ) ); } } else { return $result; } } return $data; } public function get_collect_data( $app_secret ) { $settings = $this->get_settings_for_display(); $options = get_option( 'element_pack_api_settings' ); if ( $settings['instagram_user_token'] ) { $instagram_user_token = $settings['instagram_user_token']; } elseif ( ! empty( $options['instagram_access_token'] ) ) { $instagram_user_token = $options['instagram_access_token']; } else { element_pack_alert( 'Ops! You did not set Instagram User Token!' ); return false; } // $access_token = $this->get_access_token($instagram_user_token, $app_secret); $access_token = $instagram_user_token; if ( is_string( $access_token ) && strlen( $access_token ) > 20 ) { $account_id = $this->get_instagram_account_id( $access_token ); if ( is_string( $account_id ) && strlen( $account_id ) > 5 ) { return $this->get_media( $access_token, $account_id ); } else { return $account_id; } } return $access_token; } public function get_instagram_data( $app_secret ) { $data = $this->get_collect_data( $app_secret ); if ( isset( $data->data ) ) { return $data->data; } else { if ( isset( $data->status ) && $data->status == 422 ) { element_pack_alert( $data->message ); } } return []; } public function render() { $settings = $this->get_settings_for_display(); $options = get_option( 'element_pack_api_settings' ); $instagram_app_secret = ( ! empty( $options['instagram_app_secret'] ) ) ? $options['instagram_app_secret'] : ''; if ( ! $instagram_app_secret ) { element_pack_alert( 'Ops! You did not set Instagram App Secret in element pack settings!' ); return; } /** * This is the data */ $data = $this->get_instagram_data( $instagram_app_secret ); $this->add_render_attribute( 'instagram-wrapper', 'class', 'bdt-instagram' ); $this->add_render_attribute( 'instagram', 'class', 'bdt-grid' ); $this->add_render_attribute( 'instagram', 'class', 'bdt-grid-' . esc_attr( $settings["column_gap"] ) ); $columns_mobile = isset( $settings['columns_mobile'] ) ? $settings['columns_mobile'] : 2; $columns_tablet = isset( $settings['columns_tablet'] ) ? $settings['columns_tablet'] : 3; $columns = isset( $settings['columns'] ) ? $settings['columns'] : 4; $this->add_render_attribute( 'instagram', 'class', 'bdt-child-width-1-' . esc_attr( $columns ) . '@m' ); $this->add_render_attribute( 'instagram', 'class', 'bdt-child-width-1-' . esc_attr( $columns_tablet ) . '@s' ); $this->add_render_attribute( 'instagram', 'class', 'bdt-child-width-1-' . esc_attr( $columns_mobile ) ); $this->add_render_attribute( 'instagram', 'data-bdt-grid', '' ); if ( $settings['masonry'] ) { $this->add_render_attribute( 'instagram', 'data-bdt-grid', 'masonry: true;' ); } $this->add_render_attribute( 'instagram', 'class', 'bdt-instagram-grid' ); if ( 'yes' == $settings['show_lightbox'] ) { $this->add_render_attribute( 'instagram', 'data-bdt-lightbox', 'animation:' . $settings['lightbox_animation'] . ';' ); if ( $settings['lightbox_autoplay'] ) { $this->add_render_attribute( 'instagram', 'data-bdt-lightbox', 'autoplay: 500;' ); if ( $settings['lightbox_pause'] ) { $this->add_render_attribute( 'instagram', 'data-bdt-lightbox', 'pause-on-hover: true;' ); } } } $this->add_render_attribute( [ 'instagram-wrapper' => [ 'data-settings' => [ wp_json_encode( array_filter( [ 'current_page' => 1, ] ) ) ] ] ] ); ?> <div <?php $this->print_render_attribute_string( 'instagram-wrapper' ); ?>> <div <?php $this->print_render_attribute_string( 'instagram' ); ?>> <?php // TODO need to fix load more $limit = 1; foreach ( $data as $item ) { ?> <div class="bdt-instagram-item-wrapper feed-type-video bdt-first-column"> <div class="bdt-instagram-item bdt-transition-toggle bdt-position-relative bdt-scrollspy-inview bdt-animation-fade"> <div class="bdt-instagram-thumbnail"> <?php if ( 'VIDEO' == $item->media_type ) : ?> <video src="<?php echo esc_url( $item->media_url ); ?>" title="Image by: <?php echo esc_html( $item->username ); ?>"> <?php else : ?> <img src="<?php echo esc_url( $item->media_url ); ?>" alt="Image by: <?php echo esc_html( $item->username ); ?>" loading="lazy"> <?php endif; ?> </div> <?php if ( $settings['show_lightbox'] or $settings['show_link'] ) : $target_href = ( isset( $settings['show_link'] ) && ( $settings['show_link'] == 'yes' ) ) ? $item->permalink : $item->media_url; $target_blank = ( isset( $settings['target_blank'] ) && ( 'yes' == $settings['target_blank'] ) ) ? '_blank' : '_self'; ?> <a target="<?php echo esc_attr( $target_blank ); ?>" href="<?php echo esc_url( $target_href ); ?>" data-elementor-open-lightbox="no"> <?php if ( $settings['show_overlay'] ) : ?> <div class="bdt-transition-fade bdt-inline-clip bdt-position-cover bdt-overlay bdt-overlay-default "> <?php if ( $settings['show_link_icon'] ) : ?> <?php if ( 'VIDEO' == $item->media_type ) : ?> <span class='bdt-position-center ep-icon-play'></span> <?php else : ?> <span class='bdt-position-center ep-icon-plus'></span> <?php endif; ?> <?php endif; ?> </div> <?php endif; ?> </a> <?php endif; ?> </div> </div> <?php if ( $limit++ == $settings['items']['size'] ) { break; } } ?> </div> </div> <?php } } <?php if ( $_REQUEST['show_lightbox'] ) { $link_url = esc_url( $insta_feeds[$i]['image']['large'] ); } else { $link_url = esc_url( $insta_feeds[ $i ]['link'] ); } ?> <div class="bdt-instagram-item-wrapper feed-type-<?php echo esc_attr( $insta_feeds[ $i ]['post_type'] ); ?>"> <div class="bdt-instagram-item bdt-transition-toggle bdt-position-relative bdt-scrollspy-inview bdt-animation-fade"> <div class="bdt-instagram-thumbnail"> <img src="<?php echo esc_url($insta_feeds[$i]['image']['medium']); ?>" alt="<?php esc_html_e( 'Image by:', 'bdthemes-element-pack' ); ?> <?php echo esc_html($insta_feeds[ $i ]['user']['full_name']); ?> " loading="lazy"> </div> <?php if ( $_REQUEST['show_lightbox'] or $_REQUEST['show_link'] ) : ?> <a href="<?php echo esc_url($link_url); ?>" data-elementor-open-lightbox="no"> <div class='bdt-transition-fade bdt-inline-clip bdt-position-cover bdt-overlay bdt-overlay-default '> <span class='bdt-position-center' bdt-overlay-icon></span> <div class='bdt-instagram-like-comment bdt-flex-center bdt-child-width-auto bdt-grid'> <?php if ( $_REQUEST['show_like'] ) : ?> <span><span class='ep-icon-heart-empty'></span> <b><?php echo esc_attr( $insta_feeds[ $i ]['like'] ); ?></b></span> <?php endif; ?> <?php if ( $_REQUEST['show_comment'] ) : ?> <span><span class='ep-icon-bubble'></span> <b><?php echo esc_attr( $insta_feeds[ $i ]['comment']['count'] ); ?></b></span> <?php endif; ?> </div> </div> </a> <?php endif; ?> </div> </div> [28-Mar-2026 15:16:00 UTC] PHP Warning: Undefined array key "show_lightbox" in /home/everqlsh/public_html/wp-content/plugins/bdthemes-element-pack/modules/instagram/widgets/template-classic.php on line 3 [28-Mar-2026 15:16:00 UTC] PHP Fatal error: Uncaught Error: Call to undefined function esc_url() in /home/everqlsh/public_html/wp-content/plugins/bdthemes-element-pack/modules/instagram/widgets/template-classic.php:6 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-content/plugins/bdthemes-element-pack/modules/instagram/widgets/template-classic.php on line 6 [28-Mar-2026 15:16:00 UTC] PHP Warning: Undefined array key "show_lightbox" in /home/everqlsh/public_html/wp-content/plugins/bdthemes-element-pack/modules/instagram/widgets/template.php on line 4 [28-Mar-2026 15:16:00 UTC] PHP Fatal error: Uncaught Error: Call to undefined function esc_url() in /home/everqlsh/public_html/wp-content/plugins/bdthemes-element-pack/modules/instagram/widgets/template.php:7 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-content/plugins/bdthemes-element-pack/modules/instagram/widgets/template.php on line 7 <?php namespace ElementPack\Modules\Instagram; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function __construct() { parent::__construct(); $this->add_actions(); } public function get_name() { return 'instagram'; } public function get_widgets() { $widgets = [ 'Instagram' ]; return $widgets; } /** * @param int $item_count * @param bool $cache * @param float|int $cache_time * * @return bool|mixed */ public function element_pack_instagram_feed( $item_count = 100, $cache = true, $cache_time = HOUR_IN_SECONDS * 12 ) { $options = get_option( 'element_pack_api_settings' ); $access_token = ( ! empty( $options['instagram_access_token'] ) ) ? $options['instagram_access_token'] : ''; if ( $access_token ) { $data = ( $cache ) ? get_transient( 'ep_instagram_feed_data' ) : false; if ( false === $data ) { $url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' . $access_token . '&count=' . $item_count; $feeds_json = wp_remote_fopen( $url ); $feeds_obj = json_decode( $feeds_json, true ); $feeds_images_array = []; $instagram_user = []; $ins_counter = 1; if ( 200 == $feeds_obj['meta']['code'] ) { if ( ! empty( $feeds_obj['data'] ) ) { foreach ( $feeds_obj['data'] as $data ) { array_push( $feeds_images_array, array( 'image' => [ 'small' => $data['images']['thumbnail']['url'], // thumbnail image 'medium' => $data['images']['low_resolution']['url'], // medium image 'large' => $data['images']['standard_resolution']['url'], // large image ], 'link' => $data['link'], 'like' => $data['likes']['count'], 'comment' => [ 'count' => $data['comments']['count'] ], //'text' => $data['text'], 'post_type' => $data['type'], 'user' => $data['user'], ) ); if ( 1 == $ins_counter ) { $instagram_user = $data['user']; $ins_counter++; } } set_transient( 'ep_instagram_feed_data', $feeds_images_array, $cache_time ); set_transient( 'ep_instagram_user', $instagram_user, $cache_time ); return get_transient( 'ep_instagram_feed_data' ); } } } return $data; } } /** * Instagram post layout maker with ajax load * @return string instagram images with layout */ public function element_pack_instagram_ajax_load() { $limit = isset( $_REQUEST['item_per_page'] ) ? sanitize_text_field( $_REQUEST['item_per_page'] ) : 12; $current_page = isset( $_REQUEST['current_page'] ) ? sanitize_text_field( $_REQUEST['current_page'] ) : 1; $load_more_per_click = isset( $_REQUEST['load_more_per_click'] ) ? sanitize_text_field( $_REQUEST['load_more_per_click'] ) : ''; $cache = isset( $_REQUEST['cache'] ) ? sanitize_text_field( $_REQUEST['cache'] ) : false; $cache_time = isset( $_REQUEST['cache_time'] ) ? sanitize_text_field( $_REQUEST['cache_time'] ) : 0; $skin = isset( $_REQUEST['skin'] ) ? sanitize_text_field( $_REQUEST['skin'] ) : ''; $insta_feeds = $this->element_pack_instagram_feed( 100, $cache, $cache_time ); if ( $current_page == 1 ) { $start = 0; $end = $limit - 1; } else { $start = $limit + ( ( $current_page - 2 ) * $load_more_per_click ) + 1; $end = $limit + ( $load_more_per_click * ( $current_page - 1 ) ); } ob_start(); for ( $i = $start; $i <= $end; $i++ ) { if ( isset( $insta_feeds[ $i ] ) ) { if ( 'bdt-classic-grid' == $skin ) { include 'widgets/template-classic.php'; } else { include 'widgets/template.php'; } } } $output = ob_get_clean(); echo wp_kses_post( $output ); die(); } public function add_actions() { add_action( 'wp_ajax_nopriv_element_pack_instagram_ajax_load', [ $this, 'element_pack_instagram_ajax_load' ] ); add_action( 'wp_ajax_element_pack_instagram_ajax_load', array( $this, 'element_pack_instagram_ajax_load' ) ); } } <?php namespace ElementPack\Modules\Instagram\Skins; use Elementor\Controls_Manager; use Elementor\Skin_Base; use ElementPack\Base\Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Carousel extends Skin_Base { public function get_id() { return 'bdt-instagram-carousel'; } public function get_title() { return esc_html__( 'Carousel', 'bdthemes-element-pack' ); } public function render() { $settings = $this->parent->get_settings_for_display(); $options = get_option( 'element_pack_api_settings' ); $access_token = ( ! empty( $options['instagram_access_token'] ) ) ? $options['instagram_access_token'] : ''; $instagram_app_secret = ( ! empty( $options['instagram_app_secret'] ) ) ? $options['instagram_app_secret'] : ''; if ( ! $instagram_app_secret ) { element_pack_alert( 'Ops! You did not set Instagram App Secret in element pack settings!' ); return; } $data = $this->parent->get_instagram_data( $instagram_app_secret ); if ( ! $access_token ) { element_pack_alert( 'Ops! You did not set Instagram Access Token in element pack settings!' ); return; } $this->parent->add_render_attribute( 'instagram-wrapper', 'class', 'bdt-instagram bdt-instagram-carousel' ); $this->parent->add_render_attribute( 'instagram-wrapper', 'data-bdt-slider', '' ); $this->parent->add_render_attribute( 'instagram-carousel', 'class', 'bdt-grid bdt-slider-items' ); $this->parent->add_render_attribute( 'instagram-carousel', 'class', 'bdt-grid-' . esc_attr( $settings["column_gap"] ) ); $columns_mobile = isset( $settings['columns_mobile'] ) ? $settings['columns_mobile'] : 2; $columns_tablet = isset( $settings['columns_tablet'] ) ? $settings['columns_tablet'] : 3; $columns = isset( $settings['columns'] ) ? $settings['columns'] : 4; $this->parent->add_render_attribute( 'instagram-carousel', 'class', 'bdt-child-width-1-' . esc_attr( $columns ) . '@m' ); $this->parent->add_render_attribute( 'instagram-carousel', 'class', 'bdt-child-width-1-' . esc_attr( $columns_tablet ) . '@s' ); $this->parent->add_render_attribute( 'instagram-carousel', 'class', 'bdt-child-width-1-' . esc_attr( $columns_mobile ) ); if ( 'yes' == $settings['show_lightbox'] ) { $this->parent->add_render_attribute( 'instagram-carousel', 'data-bdt-lightbox', 'animation:' . $settings['lightbox_animation'] . ';' ); if ( $settings['lightbox_autoplay'] ) { $this->parent->add_render_attribute( 'instagram-carousel', 'data-bdt-lightbox', 'autoplay: 500;' ); if ( $settings['lightbox_pause'] ) { $this->parent->add_render_attribute( 'instagram-carousel', 'data-bdt-lightbox', 'pause-on-hover: true;' ); } } } $this->parent->add_render_attribute( [ 'instagram-wrapper' => [ 'data-settings' => [ wp_json_encode( array_filter( [ 'action' => 'element_pack_instagram_ajax_load', 'show_link' => ( $settings['show_link'] ) ? true : false, 'show_lightbox' => ( $settings['show_lightbox'] ) ? true : false, 'current_page' => 1, 'load_more_per_click' => 4, 'item_per_page' => $settings["items"]["size"], ] ) ) ] ] ] ); ?> <div <?php $this->parent->print_render_attribute_string( 'instagram-wrapper' ); ?>> <?php if ( $settings['show_follow_me'] ) : $insta_user = get_transient( 'ep_instagram_user' ); $username = ( isset( $insta_user ) && ! empty( $insta_user['username'] ) ) ? $insta_user['username'] : ''; ?> <div class='bdt-instagram-follow-me bdt-position-z-index bdt-position-center'> <a href='https://www.instagram.com/<?php echo esc_html( $username ); ?>'> <?php echo esc_html( $settings['follow_me_text'] ); ?> <?php echo esc_html( $username ); ?> </a> </div> <?php endif; ?> <div <?php $this->parent->print_render_attribute_string( 'instagram-carousel' ); ?>> <?php $limit = 1; foreach ( $data as $item ) { ?> <div class="bdt-instagram-item-wrapper feed-type-video bdt-first-column"> <div class="bdt-instagram-item bdt-transition-toggle bdt-position-relative bdt-scrollspy-inview bdt-animation-fade"> <div class="bdt-instagram-thumbnail"> <?php if ( 'VIDEO' == $item->media_type ) : ?> <video src="<?php echo esc_url( $item->media_url ); ?>" title="Image by: <?php echo esc_attr( $item->username ); ?>"> <?php else : ?> <img src="<?php echo esc_url( $item->media_url ); ?>" alt="Image by: <?php echo esc_attr( $item->username ); ?>" loading="lazy"> <?php endif; ?> </div> <?php if ( $settings['show_lightbox'] or $settings['show_link'] ) : $target_href = ( isset( $settings['show_link'] ) && ( $settings['show_link'] == 'yes' ) ) ? $item->permalink : $item->media_url; $target_blank = ( isset( $settings['target_blank'] ) && ( 'yes' == $settings['target_blank'] ) ) ? '_blank' : '_self'; ?> <a target="<?php echo esc_attr( $target_blank ); ?>" href="<?php echo esc_url( $target_href ); ?>" data-elementor-open-lightbox="no"> <?php if ( $settings['show_overlay'] ) : ?> <div class="bdt-transition-fade bdt-inline-clip bdt-position-cover bdt-overlay bdt-overlay-default "> <?php if ( $settings['show_link_icon'] ) : ?> <?php if ( 'VIDEO' == $item->media_type ) : ?> <span class='bdt-position-center ep-icon-play'></span> <?php else : ?> <span class='bdt-position-center ep-icon-plus'></span> <?php endif; ?> <?php endif; ?> </div> <?php endif; ?> </a> <?php endif; ?> </div> </div> <?php if ( $limit++ == $settings['items']['size'] ) { break; } } ?> </div> <a class='bdt-position-center-left bdt-position-small bdt-hidden-hover bdt-visible@m' href='#' data-bdt-slidenav-previous data-bdt-slider-item='previous'></a> <a class='bdt-position-center-right bdt-position-small bdt-hidden-hover bdt-visible@m' href='#' data-bdt-slidenav-next data-bdt-slider-item='next'></a> </div> <?php } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Instagram', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => false, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\LottieIconBox\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Background; use Elementor\Group_Control_Css_Filter; use Elementor\Icons_Manager; use ElementPack\Utils; use ElementPack\Element_Pack_Loader; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Lottie_Icon_Box extends Module_Base { public function get_name() { return 'bdt-lottie-icon-box'; } public function get_title() { return BDTEP . esc_html( 'Lottie Icon Box', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-lottie-icon-box'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'advanced', 'icon', 'features', 'lottie', 'box', 'animation', 'bodymovin', 'transition', 'image', 'svg' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-styles' ]; } else { return [ 'ep-lottie-icon-box' ]; } } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'lottie', 'ep-scripts' ]; } else { return [ 'lottie', 'ep-lottie-icon-box' ]; } } public function get_custom_help_url() { return 'https://youtu.be/1jKFSglW6qE'; } protected function register_controls() { $this->start_controls_section( 'section_content_layout', [ 'label' => esc_html( 'Layout', 'bdthemes-element-pack' ), ] ); $this->add_control( 'lottie_json_source', [ 'label' => esc_html( 'Select JSON Source', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'url', 'options' => [ 'url' => esc_html( 'Load From URL', 'bdthemes-element-pack' ), 'local' => esc_html( 'Self Hosted', 'bdthemes-element-pack' ), 'custom' => esc_html( 'Custom JSON Code', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'lottie_json_path', [ 'label' => esc_html( 'Lottie JSON URL', 'bdthemes-element-pack' ), 'description' => sprintf( esc_html( 'Enter your lottie josn file, if you don\'t understand lottie json file so please %1s look here %2s', 'bdthemes-element-pack' ), '<a href="https://lottiefiles.com/featured" target="_blank">', '</a>' ), 'type' => Controls_Manager::TEXT, 'autocomplete' => false, 'show_external' => false, 'label_block' => true, 'show_label' => false, 'default' => BDTEP_ASSETS_URL . 'others/rocket-space.json', 'placeholder' => esc_html( 'Enter your json URL', 'bdthemes-element-pack' ), 'condition' => [ 'lottie_json_source' => 'url', ], 'dynamic' => [ 'active' => true, ], ] ); $this->add_control( 'upload_json_file', [ 'label' => esc_html( 'Select JSON File', 'bdthemes-element-pack' ), 'type' => 'json-upload', 'label_block' => true, 'show_label' => true, //'callback_selector'=>'lottie_json_path', 'condition' => [ 'lottie_json_source' => 'local', ], 'dynamic' => [ 'active' => true, ], ] ); $this->add_control( 'lottie_json_code', [ 'label' => esc_html( 'Paste JSON Code', 'bdthemes-element-pack' ), 'description' => sprintf( esc_html( 'Enter your lottie josn text, if you don\'t understand lottie json file so please %1s look here %2s', 'bdthemes-element-pack' ), '<a href="https://lottiefiles.com/featured" target="_blank">', '</a>' ), 'type' => Controls_Manager::TEXTAREA, 'label_block' => true, 'show_label' => true, 'dynamic' => [ 'active' => true, ], 'placeholder' => esc_html( 'Enter your json TEXT', 'bdthemes-element-pack' ), 'condition' => [ 'lottie_json_source' => 'custom', ], ] ); $this->add_control( 'play_action', [ 'label' => esc_html( 'Play Action', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'autoplay', 'options' => [ '' => esc_html( 'None', 'bdthemes-element-pack' ), 'autoplay' => esc_html( 'Auto Play', 'bdthemes-element-pack' ), 'click' => esc_html( 'Play on Click', 'bdthemes-element-pack' ), 'column' => esc_html( 'Play on Hover', 'bdthemes-element-pack' ), 'section' => esc_html( 'Play on Hover Section', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'view_type', [ 'label' => esc_html( 'Start When', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'options' => [ 'pageload' => esc_html( 'Page Loaded', 'bdthemes-element-pack' ), 'scroll' => esc_html( 'When Scroll', 'bdthemes-element-pack' ), ], 'default' => 'pageload', 'separator' => 'before', ] ); $this->add_control( 'loop', [ 'label' => esc_html( 'Loop', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( //* 'lottie_number_of_times', [ 'label' => esc_html( 'Times', 'bdthemes-element-pack' ), 'type' => Controls_Manager::NUMBER, 'render_type' => 'content', // 'conditions' => [ // 'relation' => 'and', // 'terms' => [ // [ // 'name' => 'lottie_trigger', // 'operator' => '!==', // 'value' => 'bind_to_scroll', // ], // [ // 'name' => 'loop', // 'operator' => '===', // 'value' => 'yes', // ], // ], // ], 'min' => 0, 'step' => 1, 'frontend_available' => true, 'condition' => [ 'loop' => [ 'yes' ], ] ] ); $this->add_control( 'speed', [ 'label' => esc_html( 'Play Speed', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0.1, 'max' => 1, 'step' => 0.1, ], ], ] ); $this->add_control( 'lottie_start_point', [ 'label' => esc_html( 'Start Point', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'frontend_available' => true, 'render_type' => 'content', 'default' => [ 'size' => '0', 'unit' => '%', ], 'size_units' => [ '%' ], ] ); $this->add_control( 'lottie_end_point', [ 'label' => esc_html( 'End Point', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'frontend_available' => true, 'render_type' => 'content', 'default' => [ 'size' => '100', 'unit' => '%', ], 'size_units' => [ '%' ], ] ); $this->add_control( 'lottie_renderer', [ 'label' => esc_html( 'Renderer', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'svg', 'options' => [ 'svg' => esc_html( 'SVG', 'bdthemes-element-pack' ), 'canvas' => esc_html( 'Canvas', 'bdthemes-element-pack' ), ], 'separator' => 'before', ] ); $this->add_control( 'title_text', [ 'label' => esc_html( 'Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true, ], 'default' => esc_html( 'Icon Box Heading', 'bdthemes-element-pack' ), 'placeholder' => esc_html( 'Enter your title', 'bdthemes-element-pack' ), 'label_block' => true, 'separator' => 'before', ] ); $this->add_control( 'title_link', [ 'label' => esc_html( 'Title Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'prefix_class' => 'bdt-title-link-' ] ); $this->add_control( 'title_link_url', [ 'label' => esc_html( 'Title Link URL', 'bdthemes-element-pack' ), 'type' => Controls_Manager::URL, 'dynamic' => [ 'active' => true ], 'placeholder' => 'http://your-link.com', 'condition' => [ 'title_link' => 'yes' ] ] ); $this->add_control( 'show_sub_title', [ 'label' => esc_html( 'Show Sub Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'separator' => 'before', ] ); $this->add_control( 'sub_title_text', [ 'label' => esc_html( 'Sub Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true, ], 'default' => esc_html( 'Icon Box Sub Heading', 'bdthemes-element-pack' ), 'placeholder' => esc_html( 'Enter your sub title', 'bdthemes-element-pack' ), 'label_block' => true, 'condition' => [ 'show_sub_title' => 'yes', ], ] ); $this->add_control( 'show_separator', [ 'label' => esc_html( 'Title Separator', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'separator' => 'before', ] ); $this->add_control( 'description_text', [ 'label' => esc_html( 'Description', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXTAREA, 'dynamic' => [ 'active' => true, ], 'default' => esc_html( 'Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'bdthemes-element-pack' ), 'placeholder' => esc_html( 'Enter your description', 'bdthemes-element-pack' ), 'rows' => 10, 'separator' => 'before', ] ); $this->add_control( 'position', [ 'label' => esc_html( 'Icon Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'separator' => 'before', 'default' => 'top', 'options' => [ 'left' => [ 'title' => esc_html( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-left', ], 'top' => [ 'title' => esc_html( 'Top', 'bdthemes-element-pack' ), 'icon' => 'eicon-v-align-top', ], 'right' => [ 'title' => esc_html( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-right', ], ], 'prefix_class' => 'elementor-position-', 'toggle' => false, 'render_type' => 'template', ] ); $this->add_control( 'icon_inline', [ 'label' => esc_html( 'Icon Inline', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'position' => [ 'left', 'right' ] ], ] ); $this->add_control( 'icon_vertical_alignment', [ 'label' => esc_html( 'Icon Vertical Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'top' => [ 'title' => esc_html( 'Top', 'bdthemes-element-pack' ), 'icon' => 'eicon-v-align-top', ], 'middle' => [ 'title' => esc_html( 'Middle', 'bdthemes-element-pack' ), 'icon' => 'eicon-v-align-middle', ], 'bottom' => [ 'title' => esc_html( 'Bottom', 'bdthemes-element-pack' ), 'icon' => 'eicon-v-align-bottom', ], ], 'default' => 'top', 'toggle' => false, 'prefix_class' => 'elementor-vertical-align-', 'condition' => [ 'position' => [ 'left', 'right' ], 'icon_inline' => '', ], ] ); $this->add_responsive_control( 'text_align', [ 'label' => esc_html( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => esc_html( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => esc_html( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box' => 'text-align: {{VALUE}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_readmore', [ 'label' => esc_html( 'Read More', 'bdthemes-element-pack' ), 'condition' => [ 'readmore' => 'yes', ], ] ); $this->add_control( 'readmore_text', [ 'label' => esc_html( 'Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true ], 'default' => esc_html( 'Read More', 'bdthemes-element-pack' ), 'placeholder' => esc_html( 'Read More', 'bdthemes-element-pack' ), ] ); $this->add_control( 'readmore_link', [ 'label' => esc_html( 'Link to', 'bdthemes-element-pack' ), 'type' => Controls_Manager::URL, 'dynamic' => [ 'active' => true, ], 'placeholder' => esc_html( 'https://your-link.com', 'bdthemes-element-pack' ), 'default' => [ 'url' => '#', ], 'condition' => [ 'readmore' => 'yes', //'readmore_text!' => '', ] ] ); $this->add_control( 'onclick', [ 'label' => esc_html( 'OnClick', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'readmore' => 'yes', //'readmore_text!' => '', ] ] ); $this->add_control( 'onclick_event', [ 'label' => esc_html( 'OnClick Event', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'placeholder' => 'myFunction()', 'description' => sprintf( esc_html( 'For details please look <a href="%s" target="_blank">here</a>' ), 'https://www.w3schools.com/jsref/event_onclick.asp' ), 'condition' => [ 'readmore' => 'yes', //'readmore_text!' => '', 'onclick' => 'yes' ] ] ); $this->add_control( 'advanced_readmore_icon', [ 'label' => esc_html( 'Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::ICONS, 'condition' => [ 'readmore' => 'yes' ], 'label_block' => false, 'skin' => 'inline' ] ); $this->add_control( 'readmore_icon_align', [ 'label' => esc_html( 'Icon Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'right', 'options' => [ 'left' => esc_html( 'Left', 'bdthemes-element-pack' ), 'right' => esc_html( 'Right', 'bdthemes-element-pack' ), ], 'condition' => [ 'advanced_readmore_icon[value]!' => '', ], ] ); $this->add_control( 'readmore_icon_indent', [ 'label' => esc_html( 'Icon Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 100, ], ], 'default' => [ 'size' => 8, ], 'condition' => [ 'advanced_readmore_icon[value]!' => '', 'readmore_text!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-readmore .bdt-button-icon-align-right' => is_rtl() ? 'margin-right: {{SIZE}}{{UNIT}};' : 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-lottie-icon-box-readmore .bdt-button-icon-align-left' => is_rtl() ? 'margin-left: {{SIZE}}{{UNIT}};' : 'margin-right: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'readmore_on_hover', [ 'label' => esc_html( 'Show on Hover', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'prefix_class' => 'bdt-readmore-on-hover-', ] ); $this->add_responsive_control( 'readmore_horizontal_offset', [ 'label' => esc_html( 'Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => -50, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -200, 'max' => 200, ], ], 'condition' => [ 'readmore_on_hover' => 'yes', ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-readmore-h-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'readmore_vertical_offset', [ 'label' => esc_html( 'Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'devices' => [ 'desktop', 'tablet', 'mobile' ], 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -200, 'max' => 200, ], ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-readmore-v-offset: {{SIZE}}px;' ], 'condition' => [ 'readmore_on_hover' => 'yes', ], ] ); $this->add_control( 'button_css_id', [ 'label' => esc_html( 'Button ID', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true, ], 'default' => '', 'title' => esc_html( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'bdthemes-element-pack' ), 'description' => esc_html( 'Please make sure the ID is unique and not used elsewhere on the page this form is displayed. This field allows <code>A-z 0-9</code> & underscore chars without spaces.', 'bdthemes-element-pack' ), 'separator' => 'before', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_indicator', [ 'label' => esc_html( 'Indicator', 'bdthemes-element-pack' ), 'condition' => [ 'indicator' => 'yes', ], ] ); $this->add_responsive_control( 'indicator_width', [ 'label' => esc_html( 'Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 10, 'step' => 2, 'max' => 300, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-indicator-svg' => 'width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'indicator_horizontal_offset', [ 'label' => esc_html( 'Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -300, 'step' => 2, 'max' => 300, ], ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-indicator-h-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'indicator_vertical_offset', [ 'label' => esc_html( 'Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -300, 'step' => 2, 'max' => 300, ], ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-indicator-v-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'indicator_rotate', [ 'label' => esc_html( 'Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'devices' => [ 'desktop', 'tablet', 'mobile' ], 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -360, 'max' => 360, 'step' => 5, ], ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-indicator-rotate: {{SIZE}}deg;' ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_badge', [ 'label' => esc_html( 'Badge', 'bdthemes-element-pack' ), 'condition' => [ 'badge' => 'yes', ], ] ); $this->add_control( 'badge_text', [ 'label' => esc_html( 'Badge Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => 'POPULAR', 'placeholder' => 'Type Badge Title', 'dynamic' => [ 'active' => true, ], ] ); $this->add_control( 'badge_position', [ 'label' => esc_html( 'Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'top-right', 'options' => element_pack_position(), ] ); $this->add_responsive_control( 'badge_horizontal_offset', [ 'label' => esc_html( 'Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -300, 'step' => 2, 'max' => 300, ], ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-badge-h-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'badge_vertical_offset', [ 'label' => esc_html( 'Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -300, 'step' => 2, 'max' => 300, ], ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-badge-v-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'badge_rotate', [ 'label' => esc_html( 'Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'devices' => [ 'desktop', 'tablet', 'mobile' ], 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -360, 'max' => 360, 'step' => 5, ], ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-badge-rotate: {{SIZE}}deg;' ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_additional', [ 'label' => esc_html( 'Additional Options', 'bdthemes-element-pack' ), ] ); $this->add_responsive_control( 'top_icon_vertical_offset', [ 'label' => esc_html( 'Icon Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 200, ], ], 'condition' => [ 'position' => 'top', ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-icon-top-v-offset: -{{SIZE}}px;' ], ] ); $this->add_responsive_control( 'top_icon_horizontal_offset', [ 'label' => esc_html( 'Icon Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -200, 'max' => 200, ], ], 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'condition' => [ 'position' => 'top', ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-icon-top-h-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'left_right_icon_horizontal_offset', [ 'label' => esc_html( 'Icon Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -200, 'max' => 200, ], ], 'condition' => [ 'position' => [ 'left', 'right' ], ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-icon-left-h-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'left_right_icon_vertical_offset', [ 'label' => esc_html( 'Icon Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -200, 'max' => 200, ], ], 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'condition' => [ 'position' => [ 'left', 'right' ], ], 'selectors' => [ '{{WRAPPER}}' => '--ep-lottie-icon-box-icon-left-v-offset: {{SIZE}}px;' ], ] ); $this->add_control( 'title_size', [ 'label' => esc_html( 'Title HTML Tag', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'h3', 'options' => element_pack_title_tags(), ] ); $this->add_control( 'readmore', [ 'label' => esc_html( 'Read More Button', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'separator' => 'before', 'default' => 'yes', ] ); $this->add_control( 'indicator', [ 'label' => esc_html( 'Indicator', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'badge', [ 'label' => esc_html( 'Badge', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'global_link', [ 'label' => esc_html( 'Global Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'prefix_class' => 'bdt-global-link-', 'description' => esc_html( 'Be aware! When Global Link activated then title link and read more link will not work', 'bdthemes-element-pack' ), ] ); $this->add_control( 'global_link_url', [ 'label' => esc_html( 'Global Link URL', 'bdthemes-element-pack' ), 'type' => Controls_Manager::URL, 'dynamic' => [ 'active' => true ], 'placeholder' => 'http://your-link.com', 'condition' => [ 'global_link' => 'yes' ] ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'section_style_image', [ 'label' => esc_html( 'Lottie Icon', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs( 'image_effects' ); $this->start_controls_tab( 'normal', [ 'label' => esc_html( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'icon_fill_color', [ 'label' => esc_html( 'Icon Fill Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap svg *' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'icon_stroke_color', [ 'label' => esc_html( 'Icon Stroke Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap svg *' => 'stroke: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'icon_background', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap', ] ); $this->add_responsive_control( 'icon_padding', [ 'label' => esc_html( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'separator' => 'before', 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'icon_border', 'placeholder' => '1px', 'separator' => 'before', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap' ] ); $this->add_control( 'icon_radius', [ 'label' => esc_html( 'Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'separator' => 'after', 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], 'condition' => [ 'icon_radius_advanced_show!' => 'yes', ], ] ); $this->add_control( 'icon_radius_advanced_show', [ 'label' => esc_html( 'Advanced Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'icon_radius_advanced', [ 'label' => esc_html( 'Radius', 'bdthemes-element-pack' ), 'description' => sprintf( esc_html( 'For example: <b>%1s</b> or Go <a href="%2s" target="_blank">this link</a> and copy and paste the radius value.', 'bdthemes-element-pack' ), '75% 25% 43% 57% / 46% 29% 71% 54%', 'https://9elements.github.io/fancy-border-radius/' ), 'type' => Controls_Manager::TEXT, 'size_units' => [ 'px', '%' ], 'separator' => 'after', 'default' => '75% 25% 43% 57% / 46% 29% 71% 54%', 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap' => 'border-radius: {{VALUE}}; overflow: hidden;', '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap img' => 'border-radius: {{VALUE}}; overflow: hidden;' ], 'condition' => [ 'icon_radius_advanced_show' => 'yes', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'icon_shadow', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap' ] ); $this->add_responsive_control( 'icon_space', [ 'label' => esc_html( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'separator' => 'before', 'default' => [ 'size' => 15, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}}.elementor-position-right .bdt-lottie-icon-box-icon' => 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}}.elementor-position-left .bdt-lottie-icon-box-icon' => 'margin-right: {{SIZE}}{{UNIT}};', '{{WRAPPER}}.elementor-position-top .bdt-lottie-icon-box-icon' => 'margin-bottom: {{SIZE}}{{UNIT}};', '(mobile){{WRAPPER}} .bdt-lottie-icon-box-icon' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'icon_size', [ 'label' => esc_html( 'Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px', 'em', 'vh', 'vw' ], 'range' => [ 'px' => [ 'min' => 6, 'max' => 300, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap' => 'font-size: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'rotate', [ 'label' => esc_html( 'Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, 'unit' => 'deg', ], 'range' => [ 'deg' => [ 'max' => 360, 'min' => -360, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap .bdt-lottie-container' => 'transform: rotate({{SIZE}}{{UNIT}});', ], ] ); $this->add_control( 'icon_background_rotate', [ 'label' => esc_html( 'Background Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, 'unit' => 'deg', ], 'range' => [ 'deg' => [ 'max' => 360, 'min' => -360, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap' => 'transform: rotate({{SIZE}}{{UNIT}});', ], ] ); $this->add_control( 'background_hover_transition_image', [ 'label' => esc_html( 'Transition Duration', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0.3, ], 'range' => [ 'px' => [ 'max' => 3, 'step' => 0.1, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-icon-wrap' => 'transition-duration: {{SIZE}}s', ], ] ); $this->add_control( 'opacity', [ 'label' => esc_html( 'Opacity', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-image svg' => 'opacity: {{SIZE}};', ], ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters', 'selector' => '{{WRAPPER}} .bdt-lottie-image svg', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'hover', [ 'label' => esc_html( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'icon_fill_hover_color', [ 'label' => esc_html( 'Icon Fill Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-icon-wrap svg *' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'icon_stroke_hover_color', [ 'label' => esc_html( 'Icon Stroke Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-icon-wrap svg *' => 'stroke: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'icon_hover_background', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-icon-wrap:after', ] ); $this->add_control( 'icon_effect', [ 'label' => esc_html( 'Effect', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'prefix_class' => 'bdt-icon-effect-', 'default' => 'none', 'options' => [ 'none' => esc_html( 'None', 'bdthemes-element-pack' ), 'a' => esc_html( 'Effect A', 'bdthemes-element-pack' ), 'b' => esc_html( 'Effect B', 'bdthemes-element-pack' ), 'c' => esc_html( 'Effect C', 'bdthemes-element-pack' ), 'd' => esc_html( 'Effect D', 'bdthemes-element-pack' ), 'e' => esc_html( 'Effect E', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'icon_hover_border_color', [ 'label' => esc_html( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'separator' => 'before', 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-icon-wrap' => 'border-color: {{VALUE}};', ], 'condition' => [ 'icon_border_border!' => '', ], ] ); $this->add_control( 'icon_hover_radius', [ 'label' => esc_html( 'Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'separator' => 'after', 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-icon-wrap' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ] ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'icon_hover_shadow', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-icon-wrap' ] ); $this->add_control( 'icon_hover_rotate', [ 'label' => esc_html( 'Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => 'deg', ], 'range' => [ 'deg' => [ 'max' => 360, 'min' => -360, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-icon-wrap .bdt-lottie-container' => 'transform: rotate({{SIZE}}{{UNIT}});', ], ] ); $this->add_control( 'icon_hover_background_rotate', [ 'label' => esc_html( 'Background Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => 'deg', ], 'range' => [ 'deg' => [ 'max' => 360, 'min' => -360, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-icon-wrap' => 'transform: rotate({{SIZE}}{{UNIT}});', ], ] ); $this->add_control( 'opacity_hover', [ 'label' => esc_html( 'Opacity', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-image svg' => 'opacity: {{SIZE}};', ], ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters_hover', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-image svg', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_title', [ 'label' => esc_html( 'Title', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs( 'tabs_title_style' ); $this->start_controls_tab( 'tab_title_style_normal', [ 'label' => esc_html( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_responsive_control( 'title_bottom_space', [ 'label' => esc_html( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-title' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'title_color', [ 'label' => esc_html( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-content .bdt-lottie-icon-box-title' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-content .bdt-lottie-icon-box-title', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_title_style_hover', [ 'label' => esc_html( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'title_color_hover', [ 'label' => esc_html( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-content .bdt-lottie-icon-box-title' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography_hover', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-content .bdt-lottie-icon-box-title', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_sub_title', [ 'label' => esc_html( 'Sub Title', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_sub_title' => 'yes', ], ] ); $this->start_controls_tabs( 'tabs_sub_title_style' ); $this->start_controls_tab( 'tab_sub_title_style_normal', [ 'label' => esc_html( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_responsive_control( 'sub_title_bottom_space', [ 'label' => esc_html( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-sub-title' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'sub_title_color', [ 'label' => esc_html( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-content .bdt-lottie-icon-box-sub-title' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'sub_title_typography', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-content .bdt-lottie-icon-box-sub-title', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_sub_title_style_hover', [ 'label' => esc_html( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'sub_title_color_hover', [ 'label' => esc_html( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-content .bdt-lottie-icon-box-sub-title' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'sub_title_typography_hover', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-content .bdt-lottie-icon-box-sub-title', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_description', [ 'label' => esc_html( 'Description', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs( 'tabs_description_style' ); $this->start_controls_tab( 'tab_description_style_normal', [ 'label' => esc_html( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_responsive_control( 'description_bottom_space', [ 'label' => esc_html( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-content .bdt-lottie-icon-box-description' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'description_color', [ 'label' => esc_html( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-content .bdt-lottie-icon-box-description' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'description_typography', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-content .bdt-lottie-icon-box-description', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_description_style_hover', [ 'label' => esc_html( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'description_color_hover', [ 'label' => esc_html( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-content .bdt-lottie-icon-box-description' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'description_typography_hover', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box:hover .bdt-lottie-icon-box-content .bdt-lottie-icon-box-description', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_content_title_separator', [ 'label' => esc_html( 'Title Separator', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_separator' => 'yes', ], ] ); $this->add_control( 'title_separator_type', [ 'label' => esc_html( 'Separator Type', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'line', 'options' => [ 'line' => esc_html( 'Line', 'bdthemes-element-pack' ), 'bloomstar' => esc_html( 'Bloomstar', 'bdthemes-element-pack' ), 'bobbleaf' => esc_html( 'Bobbleaf', 'bdthemes-element-pack' ), 'demaxa' => esc_html( 'Demaxa', 'bdthemes-element-pack' ), 'fill-circle' => esc_html( 'Fill Circle', 'bdthemes-element-pack' ), 'finalio' => esc_html( 'Finalio', 'bdthemes-element-pack' ), //'fitical' => esc_html( 'Fitical', 'bdthemes-element-pack' ), 'jemik' => esc_html( 'Jemik', 'bdthemes-element-pack' ), //'genizen' => esc_html( 'Genizen', 'bdthemes-element-pack' ), 'leaf-line' => esc_html( 'Leaf Line', 'bdthemes-element-pack' ), //'lendine' => esc_html( 'Lendine', 'bdthemes-element-pack' ), 'multinus' => esc_html( 'Multinus', 'bdthemes-element-pack' ), //'oradox' => esc_html( 'Oradox', 'bdthemes-element-pack' ), 'rotate-box' => esc_html( 'Rotate Box', 'bdthemes-element-pack' ), 'sarator' => esc_html( 'Sarator', 'bdthemes-element-pack' ), 'separk' => esc_html( 'Separk', 'bdthemes-element-pack' ), 'slash-line' => esc_html( 'Slash Line', 'bdthemes-element-pack' ), //'subtrexo' => esc_html( 'Subtrexo', 'bdthemes-element-pack' ), 'tripline' => esc_html( 'Tripline', 'bdthemes-element-pack' ), 'vague' => esc_html( 'Vague', 'bdthemes-element-pack' ), 'zigzag-dot' => esc_html( 'Zigzag Dot', 'bdthemes-element-pack' ), 'zozobe' => esc_html( 'Zozobe', 'bdthemes-element-pack' ), ], //'render_type' => 'none', ] ); $this->add_control( 'title_separator_border_style', [ 'label' => esc_html( 'Separator Style', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'solid' => esc_html( 'Solid', 'bdthemes-element-pack' ), 'dotted' => esc_html( 'Dotted', 'bdthemes-element-pack' ), 'dashed' => esc_html( 'Dashed', 'bdthemes-element-pack' ), 'groove' => esc_html( 'Groove', 'bdthemes-element-pack' ), ], 'condition' => [ 'title_separator_type' => 'line' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-separator' => 'border-top-style: {{VALUE}};', ], ] ); $this->add_control( 'title_separator_line_color', [ 'label' => esc_html( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'title_separator_type' => 'line' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-separator' => 'border-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'title_separator_height', [ 'label' => esc_html( 'Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 1, 'max' => 15, ] ], 'condition' => [ 'title_separator_type' => 'line' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-separator' => 'border-top-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'title_separator_width', [ 'label' => esc_html( 'Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px', '%' ], 'range' => [ '%' => [ 'min' => 1, 'max' => 100, ], 'px' => [ 'min' => 1, 'max' => 300, ] ], 'condition' => [ 'title_separator_type' => 'line' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-separator' => 'width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'title_separator_svg_fill_color', [ 'label' => esc_html( 'Fill Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'title_separator_type!' => 'line' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-separator-wrap svg *' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'title_separator_svg_stroke_color', [ 'label' => esc_html( 'Stroke Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'title_separator_type!' => 'line' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-separator-wrap svg *' => 'stroke: {{VALUE}};', ], ] ); $this->add_responsive_control( 'title_separator_svg_width', [ 'label' => esc_html( 'Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px', '%' ], 'range' => [ '%' => [ 'min' => 1, 'max' => 100, ], 'px' => [ 'min' => 1, 'max' => 300, ] ], 'condition' => [ 'title_separator_type!' => 'line' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-separator-wrap > *' => 'width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'title_separator_spacing', [ 'label' => esc_html( 'Separator Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-separator-wrap' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_readmore', [ 'label' => esc_html( 'Read More', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'readmore' => 'yes', ], ] ); $this->add_control( 'readmore_attention', [ 'label' => esc_html( 'Attention', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->start_controls_tabs( 'tabs_readmore_style' ); $this->start_controls_tab( 'tab_readmore_normal', [ 'label' => esc_html( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'readmore_text_color', [ 'label' => esc_html( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-readmore' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-lottie-icon-box-readmore svg' => 'fill: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'readmore_background', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-readmore', 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'readmore_border', 'placeholder' => '1px', 'separator' => 'before', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-readmore' ] ); $this->add_responsive_control( 'readmore_radius', [ 'label' => esc_html( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'separator' => 'after', 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-readmore' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'readmore_shadow', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-readmore', ] ); $this->add_responsive_control( 'readmore_padding', [ 'label' => esc_html( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-readmore' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'readmore_typography', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-readmore', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_readmore_hover', [ 'label' => esc_html( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'readmore_hover_text_color', [ 'label' => esc_html( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-readmore:hover' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-lottie-icon-box-readmore:hover svg' => 'fill: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'readmore_hover_background', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-readmore:hover', 'separator' => 'before', ] ); $this->add_control( 'readmore_hover_border_color', [ 'label' => esc_html( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-readmore:hover' => 'border-color: {{VALUE}};', ], 'condition' => [ 'readmore_border_border!' => '' ] ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'readmore_hover_shadow', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-readmore:hover', ] ); $this->add_control( 'readmore_hover_animation', [ 'label' => esc_html( 'Hover Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HOVER_ANIMATION, ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_indicator', [ 'label' => esc_html( 'Indicator', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'indicator' => 'yes', ], ] ); $this->add_control( 'indicator_style', [ 'label' => esc_html( 'Indicator Style', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => '1', 'options' => [ '1' => esc_html( 'Style 1', 'bdthemes-element-pack' ), '2' => esc_html( 'Style 2', 'bdthemes-element-pack' ), '3' => esc_html( 'Style 3', 'bdthemes-element-pack' ), '4' => esc_html( 'Style 4', 'bdthemes-element-pack' ), '5' => esc_html( 'Style 5', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'indicator_fill_color', [ 'label' => esc_html( 'Fill Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-indicator-svg svg' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'indicator_stroke_color', [ 'label' => esc_html( 'Stroke Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-indicator-svg svg' => 'stroke: {{VALUE}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_badge', [ 'label' => esc_html( 'Badge', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'badge' => 'yes', ], ] ); $this->add_control( 'badge_text_color', [ 'label' => esc_html( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-badge span' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'badge_background', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-badge span', 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'badge_border', 'placeholder' => '1px', 'separator' => 'before', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-badge span' ] ); $this->add_responsive_control( 'badge_radius', [ 'label' => esc_html( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'separator' => 'after', 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-badge span' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'badge_shadow', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-badge span', ] ); $this->add_responsive_control( 'badge_padding', [ 'label' => esc_html( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box-badge span' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'badge_typography', 'selector' => '{{WRAPPER}} .bdt-lottie-icon-box-badge span', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_additional', [ 'label' => esc_html( 'Additional', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'content_padding', [ 'label' => esc_html( 'Content Inner Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-lottie-icon-box-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] ); $this->add_control( 'icon_inline_spacing', [ 'label' => esc_html( 'Icon Inline Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 100, ], ], 'condition' => [ 'position' => [ 'left', 'right' ], 'icon_inline' => 'yes', ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-icon-box .bdt-icon-heading' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); } protected function render_lottie_icon() { $settings = $this->get_settings_for_display(); $json_code = ''; $json_path = ''; $is_json_url = true; if ( $settings['lottie_json_source'] == 'url' ) { $json_path = $settings['lottie_json_path']; } elseif ( $settings['lottie_json_source'] == 'local' ) { $json_path = $settings['upload_json_file']; } elseif ( $settings['lottie_json_source'] == 'custom' ) { $json_code = $settings['lottie_json_code']; $is_json_url = false; } $this->add_render_attribute( 'wrapper', 'class', 'bdt-lottie-image bdt-lottie-icon-box-icon-wrap' ); if ( ! empty( $settings['shape'] ) ) { $this->add_render_attribute( 'wrapper', 'class', 'elementor-image-shape-' . $settings['shape'] ); } $lottie_start_point = ( ! empty( $settings['lottie_start_point']['size'] ) ? $settings['lottie_start_point']['size'] : 0 ); $lottie_end_point = ( isset( $settings['lottie_end_point']['size'] ) ) ? $settings['lottie_end_point']['size'] : 0; $lottie_end_point = ( strlen( $lottie_end_point ) > 0 ) ? $lottie_end_point : 100; $loopSet = ''; if ( isset( $settings['loop'] ) ) { $loopSet = ( $settings['loop'] ) ? true : false; } if ( ! empty( $settings['lottie_number_of_times'] ) && strlen( $settings['lottie_number_of_times'] ) > 0 ) { $loopSet = ( $settings['lottie_number_of_times'] ) - 1; } $this->add_render_attribute( [ 'lottie' => [ 'id' => 'bdt-lottie-' . $this->get_id(), 'class' => 'bdt-lottie-container', 'data-settings' => [ wp_json_encode( [ 'loop' => $loopSet, 'is_json_url' => $is_json_url, 'json_path' => $json_path, 'json_code' => $json_code, 'view_type' => $settings['view_type'], 'speed' => ( $settings['speed']['size'] ) ? $settings['speed']['size'] : 1, 'play_action' => $settings['play_action'], 'start_point' => $lottie_start_point, 'end_point' => $lottie_end_point, 'lottie_renderer' => $settings['lottie_renderer'], ] ) ] ] ] ); ?> <div class="bdt-lottie-icon-box-icon"> <div <?php $this->print_render_attribute_string( 'wrapper' ); ?>> <div <?php $this->print_render_attribute_string( 'lottie' ); ?>></div> </div> </div> <?php } protected function render() { $settings = $this->get_settings_for_display(); $this->add_render_attribute( 'advanced-icon-box-title', 'class', 'bdt-lottie-icon-box-title' ); $this->add_render_attribute( 'advanced-icon-box-sub-title', 'class', 'bdt-lottie-icon-box-sub-title' ); if ( 'yes' == $settings['title_link'] and $settings['title_link_url']['url'] ) { $target = $settings['title_link_url']['is_external'] ? '_blank' : '_self'; $this->add_render_attribute( 'advanced-icon-box-title', 'onclick', "window.open('" . esc_url($settings['title_link_url']['url']) . "', '$target')" ); } $this->add_render_attribute( 'description_text', 'class', 'bdt-lottie-icon-box-description' ); $this->add_inline_editing_attributes( 'title_text', 'none' ); $this->add_inline_editing_attributes( 'description_text' ); $this->add_render_attribute( 'readmore', 'class', [ 'bdt-lottie-icon-box-readmore', 'bdt-display-inline-block' ] ); if ( ! empty( $settings['readmore_link']['url'] ) ) { $this->add_link_attributes( 'readmore', $settings['readmore_link'] ); } if ( $settings['readmore_attention'] ) { $this->add_render_attribute( 'readmore', 'class', 'bdt-ep-attention-button' ); } if ( $settings['readmore_hover_animation'] ) { $this->add_render_attribute( 'readmore', 'class', 'elementor-animation-' . $settings['readmore_hover_animation'] ); } if ( $settings['onclick'] ) { $this->add_render_attribute( 'readmore', 'onclick', $settings['onclick_event'] ); } if ( ! empty( $settings['button_css_id'] ) ) { $this->add_render_attribute( 'readmore', 'id', $settings['button_css_id'] ); } $this->add_render_attribute( 'advanced-icon-box', 'class', 'bdt-lottie-icon-box' ); if ( 'yes' == $settings['global_link'] and $settings['global_link_url']['url'] ) { $target = $settings['global_link_url']['is_external'] ? '_blank' : '_self'; $this->add_render_attribute( 'advanced-icon-box', 'onclick', "window.open('" . esc_url($settings['global_link_url']['url']) . "', '$target')" ); } if ( 'yes' == $settings['icon_inline'] && 'top' != $settings['position'] ) { $this->add_render_attribute( 'advanced-icon-box-icon-heading', 'class', 'bdt-icon-heading bdt-flex bdt-flex-middle' ); if ( 'right' == $settings['position'] ) { $this->add_render_attribute( 'advanced-icon-box-icon-heading', 'class', 'bdt-flex-row-reverse' ); } } ?> <div <?php $this->print_render_attribute_string( 'advanced-icon-box' ); ?>> <?php if ( '' == $settings['icon_inline'] ) : ?> <?php $this->render_lottie_icon(); ?> <?php endif; ?> <div class="bdt-lottie-icon-box-content"> <div <?php $this->print_render_attribute_string( 'advanced-icon-box-icon-heading' ); ?>> <?php if ( 'yes' == $settings['icon_inline'] ) : ?> <?php $this->render_lottie_icon(); ?> <?php endif; ?> <div class="bdt-icon-box-title-wrapper"> <?php if ( $settings['title_text'] ) : ?> <<?php echo esc_url( Utils::get_valid_html_tag( $settings['title_size'] ) ); ?> <?php $this->print_render_attribute_string( 'advanced-icon-box-title' ); ?>> <span <?php $this->print_render_attribute_string( 'title_text' ); ?>> <?php echo wp_kses( $settings['title_text'], element_pack_allow_tags( 'title' ) ); ?> </span> </<?php echo esc_url( Utils::get_valid_html_tag( $settings['title_size'] ) ); ?>> <?php endif; ?> <?php if ( 'yes' == $settings['show_sub_title'] ) : ?> <div <?php $this->print_render_attribute_string( 'advanced-icon-box-sub-title' ); ?>> <?php echo wp_kses( $settings['sub_title_text'], element_pack_allow_tags( 'title' ) ); ?> </div> <?php endif; ?> </div> </div> <?php if ( $settings['show_separator'] ) : ?> <?php if ( 'line' == $settings['title_separator_type'] ) : ?> <div class="bdt-lottie-icon-box-separator-wrap"> <div class="bdt-lottie-icon-box-separator"></div> </div> <?php elseif ( 'line' != $settings['title_separator_type'] ) : ?> <div class="bdt-lottie-icon-box-separator-wrap"> <?php $svg_image = BDTEP_ASSETS_PATH . 'images/separator/' . $settings['title_separator_type'] . '.svg'; if ( file_exists( $svg_image ) ) { ob_start(); include( $svg_image ); $svg_image = ob_get_clean(); echo wp_kses( $svg_image, element_pack_allow_tags( 'svg' ) ); } ?> </div> <?php endif; ?> <?php endif; ?> <?php if ( $settings['description_text'] ) : ?> <div <?php $this->print_render_attribute_string( 'description_text' ); ?>> <?php echo wp_kses( $settings['description_text'], element_pack_allow_tags( 'text' ) ); ?> </div> <?php endif; ?> <?php if ( $settings['readmore'] ) : ?> <a <?php $this->print_render_attribute_string( 'readmore' ); ?>> <?php echo esc_html( $settings['readmore_text'] ); ?> <?php if ( $settings['advanced_readmore_icon']['value'] ) : ?> <span class="bdt-button-icon-align-<?php echo esc_attr( $settings['readmore_icon_align'] ); ?>"> <?php Icons_Manager::render_icon( $settings['advanced_readmore_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); ?> </span> <?php endif; ?> </a> <?php endif ?> </div> </div> <?php if ( $settings['indicator'] ) : ?> <div class="bdt-indicator-svg bdt-svg-style-<?php echo esc_attr( $settings['indicator_style'] ); ?>"> <?php echo element_pack_svg_icon( 'arrow-' . $settings['indicator_style'] ); ?> </div> <?php endif; ?> <?php if ( $settings['badge'] and '' != $settings['badge_text'] ) : ?> <div class="bdt-lottie-icon-box-badge bdt-position-<?php echo esc_attr( $settings['badge_position'] ); ?>"> <span class="bdt-badge bdt-padding-small"> <?php echo esc_html( $settings['badge_text'] ); ?> </span> </div> <?php endif; ?> <?php } } <?php namespace ElementPack\Modules\LottieIconBox; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'lottie-icon-box'; } public function get_widgets() { $widgets = [ 'Lottie_Icon_Box', ]; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Lottie Icon Box', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => false, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\EddCheckout\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Background; use Elementor\Icons_Manager; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class EDD_Checkout extends Module_Base { public function get_name() { return 'bdt-edd-checkout'; } public function get_title() { return BDTEP . esc_html__('EDD Checkout', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-edd-checkout bdt-new'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['edd', 'easy', 'digital', 'downlaod', 'checkout', 'purchase']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-edd-checkout']; } } protected function register_controls() { $this->register_form_controls_layout(); $this->register_checkout_table_header(); $this->register_checkout_table_body(); $this->register_checkout_table_subtotal(); $this->register_form_controls_label(); $this->register_form_controls_fields(); $this->register_checkout_purchase_total(); $this->register_form_submit_button(); } protected function register_checkout_table_header() { $this->start_controls_section( 'section_checkout_table_header_style', [ 'label' => __('Table Header', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_checkout_table_header_style'); $this->start_controls_tab( 'tab_checkout_table_header_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'checkout_header_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart .edd_cart_header_row th' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'checkout_header_background', 'label' => __('Background', 'bdthemes-element-pack'), 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} #edd_checkout_cart .edd_cart_header_row th', ] ); $this->add_responsive_control( 'header_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart .edd_cart_header_row th' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'header_typography', 'selector' => '{{WRAPPER}} #edd_checkout_cart .edd_cart_header_row th', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_checkout_header_hover', [ 'label' => __('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'checkout_header_hover_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart .edd_cart_header_row th:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'checkout_header_hover_background', 'label' => __('Background', 'bdthemes-element-pack'), 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} #edd_checkout_cart .edd_cart_header_row th:hover', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } protected function register_checkout_table_body() { $this->start_controls_section( 'section_style_body', [ 'label' => __('Table Body', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'checkout_cell_border_style', 'label' => __('Border', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_checkout_cart .edd_cart_item td', ] ); $this->add_responsive_control( 'cell_padding', [ 'label' => __('Cell Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'default' => [ 'top' => 0.5, 'bottom' => 0.5, 'left' => 1, 'right' => 1, 'unit' => 'em' ], 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart .edd_cart_item td' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->start_controls_tabs('tabs_body_style'); $this->start_controls_tab( 'tab_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'normal_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart .edd_cart_item td' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'normal_action_btn_color', [ 'label' => __('Action Button Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart .edd_cart_item td a' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'normal_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart .edd_cart_item td' => 'background-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_hover', [ 'label' => __('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'row_hover_text_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart .edd_cart_item td:hover' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'row_action_btn_hover_color', [ 'label' => __('Action Button Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart .edd_cart_item td a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'row_hover_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart .edd_cart_item td:hover' => 'background-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } protected function register_checkout_table_subtotal() { $this->start_controls_section( 'checkout_section_total', [ 'label' => __('Table Footer', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'checkout_total_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart th.edd_cart_total' => 'color: {{VALUE}}', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'checkout_total_background', 'label' => __('Background', 'bdthemes-element-pack'), 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} #edd_checkout_cart th.edd_cart_total', ] ); $this->add_responsive_control( 'checkout_total_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} #edd_checkout_cart th.edd_cart_total' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'checkout_total_border', 'label' => __('Border', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_checkout_cart th.edd_cart_total', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'checkout_total_typography', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_checkout_cart th.edd_cart_total', ] ); $this->end_controls_section(); } protected function register_checkout_purchase_total() { $this->start_controls_section( 'checkout_section_purchase_total', [ 'label' => __('Purchase Total', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'checkout_purchase_total_label_color', [ 'label' => __('Label Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap #edd_final_total_wrap strong' => 'color: {{VALUE}}', ], ] ); $this->add_control( 'checkout_purchase_total_color', [ 'label' => __('Price Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap #edd_final_total_wrap' => 'color: {{VALUE}}', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'checkout_purchase_total_background', 'label' => __('Background', 'bdthemes-element-pack'), 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap #edd_final_total_wrap', ] ); $this->add_responsive_control( 'checkout_purchase_total_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap #edd_final_total_wrap' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'checkout_purchase_total_border', 'label' => __('Border', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap #edd_final_total_wrap', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'checkout_purchase_total_typography', 'label' => __('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap #edd_final_total_wrap > *', ] ); $this->end_controls_section(); } protected function register_form_controls_layout() { $this->start_controls_section( 'section_layout', [ 'label' => __('Layout', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'checkout_action_button_type', [ 'label' => __('Action Button Type', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => [ 'icon' => __('Icon', 'bdthemes-element-pack'), 'text' => __('Text', 'bdthemes-element-pack'), ], 'default' => 'icon', 'dynamic' => ['active' => true], ] ); $this->add_control( 'checkout_action_button_text', [ 'label' => __('Button Text', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'default' => __('Remove', 'bdthemes-element-pack'), 'condition' => [ 'checkout_action_button_type' => 'text' ] ] ); $this->add_control( 'checkout_action_button_icon', [ 'label' => __('Select Icon', 'bdthemes-element-pack'), 'type' => Controls_Manager::ICONS, 'default' => [ 'value' => 'eicon-close', 'library' => 'solid', ], 'condition' => [ 'checkout_action_button_type' => 'icon' ] ] ); $this->add_control( 'edd_register_form_input_fullwidth', [ 'label' => esc_html__('Fullwidth Input', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => esc_html__('On', 'bdthemes-element-pack'), 'label_off' => esc_html__('Off', 'bdthemes-element-pack'), 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap input[type*="text"]' => 'width: 100%;', '{{WRAPPER}} #edd_checkout_form_wrap input[type*="email"]' => 'width: 100%;', '{{WRAPPER}} #edd_checkout_form_wrap input[type*="url"]' => 'width: 100%;', '{{WRAPPER}} #edd_checkout_form_wrap input[type*="number"]' => 'width: 100%;', '{{WRAPPER}} #edd_checkout_form_wrap input[type*="tel"]' => 'width: 100%;', '{{WRAPPER}} #edd_checkout_form_wrap input[type*="date"]' => 'width: 100%;', '{{WRAPPER}} #edd_checkout_form_wrap input[type*="password"]' => 'width: 100%;', '{{WRAPPER}} #edd_checkout_form_wrap .select.edd-select' => 'width: 100%;', ], 'separator' => 'before' ] ); $this->add_control( 'edd_register_form_button_fullwidth', [ 'label' => esc_html__('Fullwidth Button', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => esc_html__('On', 'bdthemes-element-pack'), 'label_off' => esc_html__('Off', 'bdthemes-element-pack'), 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap .edd-submit' => 'width: 100%;', ], ] ); $this->end_controls_section(); } protected function register_form_controls_label() { $this->start_controls_section( 'section_style_labels', [ 'label' => esc_html__('Form Label', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'heading_checkout_profile_form_title_label', [ 'label' => __('P R O F I L E I N F O', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, ] ); $this->add_control( 'profile_label_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-edd-checkout #edd_checkout_form_wrap legend' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'profile_label_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-edd-checkout #edd_checkout_form_wrap fieldset' => 'border-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'profile_label_border_width', [ 'label' => esc_html__('Border Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-edd-checkout #edd_checkout_form_wrap fieldset' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'profile_label_typography', 'selector' => '{{WRAPPER}} .bdt-edd-checkout #edd_checkout_form_wrap legend', ] ); $this->add_control( 'heading_checkout_profile_form_label', [ 'label' => __('L A B E L', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, ] ); $this->add_control( 'label_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap label' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'label_spacing', [ 'label' => esc_html__('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap label' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'label_typography', 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap label', ] ); $this->add_control( 'heading_checkout_profile_form_sub_label', [ 'label' => __('S U B L A B E L', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, ] ); $this->add_control( 'sub_label_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap span.edd-description' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'sub_label_spacing', [ 'label' => esc_html__('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap span.edd-description' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'sub_label_typography', 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap span.edd-description', ] ); $this->end_controls_section(); } protected function register_form_controls_fields() { $this->start_controls_section( 'section_field_style', [ 'label' => esc_html__('Form Fields', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_field_style'); $this->start_controls_tab( 'tab_field_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'field_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_placeholder_color', [ 'label' => esc_html__('Placeholder Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input::placeholder' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input::-moz-placeholder' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_background_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'field_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '#edd_checkout_form_wrap input.edd-input', 'separator' => 'before', ] ); $this->add_responsive_control( 'field_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'field_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; height: auto;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'field_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'field_box_shadow', 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_field_hover', [ 'label' => esc_html__('Focus', 'bdthemes-element-pack'), ] ); $this->add_control( 'field_text_color_focus', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input:focus' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_placeholder_color_focus', [ 'label' => esc_html__('Placeholder Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input:focus::placeholder' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input:focus::-moz-placeholder' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'field_background_color_focus', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input:focus' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'field_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'field_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap input.edd-input:focus' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } protected function register_form_submit_button() { $this->start_controls_section( 'section_submit_button_style', [ 'label' => esc_html__('Form Submit Button', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_button_style'); $this->start_controls_tab( 'tab_button_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap #edd-purchase-button' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'button_background_color', 'label' => __('Background Color', 'bdthemes-element-pack'), 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap #edd-purchase-button', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'button_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap #edd-purchase-button', 'separator' => 'before', ] ); $this->add_responsive_control( 'button_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap #edd-purchase-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_text_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap #edd-purchase-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_text_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap #edd-purchase-button' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'button_typography', 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap #edd-purchase-button', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_box_shadow', 'label' => esc_html__('Box Shadow', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_checkout_form_wrap #edd-purchase-button', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_button_hover', [ 'label' => esc_html__('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_hover_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_checkout_form_wrap #edd-purchase-button:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'button_background_hover_color', 'label' => __('Background Color', 'bdthemes-element-pack'), 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} #edd_register_form #edd-purchase-button:hover', ] ); $this->add_control( 'button_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_register_form #edd-purchase-button:hover' => 'border-color: {{VALUE}};', ], 'condition' => [ 'button_border_border!' => '', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_hover_box_shadow', 'label' => esc_html__('Box Shadow', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #edd_register_form #edd-purchase-button:hover', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } public function render() { $settings = $this->get_settings_for_display(); $payment_mode = edd_get_chosen_gateway(); $form_action = esc_url(edd_get_checkout_uri('payment-mode=' . $payment_mode)); if (!edd_is_ajax_disabled()) { $this->add_render_attribute('edd_checkout_cart', 'class', ['ajaxed'], true); } global $post; ?> <div class="bdt-edd-checkout"> <table id="edd_checkout_cart" <?php $this->print_render_attribute_string('edd_checkout_cart'); ?>> <thead> <tr class="edd_cart_header_row"> <?php do_action('edd_checkout_table_header_first'); ?> <th class="edd_cart_item_name"><?php esc_html_e('Item Name', 'bdthemes-element-pack'); ?></th> <th class="edd_cart_item_price"><?php esc_html_e('Item Price', 'bdthemes-element-pack'); ?></th> <th class="edd_cart_actions"><?php esc_html_e('Actions', 'bdthemes-element-pack'); ?></th> <?php do_action('edd_checkout_table_header_last'); ?> </tr> </thead> <tbody> <?php $cart_items = edd_get_cart_contents(); ?> <?php do_action('edd_cart_items_before'); ?> <?php if ($cart_items) : ?> <?php foreach ($cart_items as $key => $item) : ?> <tr class="edd_cart_item" id="edd_cart_item_<?php echo esc_attr($key) . '_' . esc_attr($item['id']); ?>" data-download-id="<?php echo esc_attr($item['id']); ?>"> <?php do_action('edd_checkout_table_body_first', $item); ?> <td class="edd_cart_item_name"> <?php if (current_theme_supports('post-thumbnails') && has_post_thumbnail($item['id'])) { echo '<div class="edd_cart_item_image">'; echo get_the_post_thumbnail($item['id'], apply_filters('edd_checkout_image_size', array(25, 25))); echo '</div>'; } $item_title = edd_get_cart_item_name($item); echo '<span class="edd_checkout_cart_item_title">' . esc_html($item_title) . '</span>'; /** * Runs after the item in cart's title is echoed * @since 2.6 * * @param array $item Cart Item * @param int $key Cart key */ do_action('edd_checkout_cart_item_title_after', $item, $key); ?> </td> <td class="edd_cart_item_price"> <?php echo esc_html(edd_cart_item_price($item['id'], $item['options'])); do_action('edd_checkout_cart_item_price_after', $item); ?> </td> <td class="edd_cart_actions"> <?php if (edd_item_quantities_enabled() && !edd_download_quantities_disabled($item['id'])) : ?> <input type="number" min="1" step="1" name="edd-cart-download-<?php echo esc_attr($key); ?>-quantity" data-key="<?php echo esc_attr($key); ?>" class="edd-input edd-item-quantity" value="<?php echo esc_attr(edd_get_cart_item_quantity($item['id']), $item['options']); ?>" /> <input type="hidden" name="edd-cart-downloads[]" value="<?php echo esc_attr($item['id']); ?>" /> <input type="hidden" name="edd-cart-download-<?php echo esc_attr($key); ?>-options" value="<?php echo esc_attr(json_encode($item['options'])); ?>" /> <?php endif; ?> <?php do_action('edd_cart_actions', $item, $key); ?> <a class="edd_cart_remove_item_btn" href="<?php echo esc_url(wp_nonce_url(edd_remove_item_url($key), 'edd-remove-from-cart-' . $key, 'edd_remove_from_cart_nonce')); ?>"> <?php if (($settings['checkout_action_button_type'] === 'text')) { echo '<span class="edd-action-btn-remove-text">' . esc_html($settings['checkout_action_button_text']) . '</span>'; } else { ?> <span class="edd-action-btn-remove-icon"> <?php Icons_Manager::render_icon($settings['checkout_action_button_icon'], ['aria-hidden' => 'true', 'class' => 'fa-fw']); ?> </span> <?php } ?> </a> </td> <?php do_action('edd_checkout_table_body_last', $item); ?> </tr> <?php endforeach; ?> <?php endif; ?> <?php do_action('edd_cart_items_middle'); ?> <!-- Show any cart fees, both positive and negative fees --> <?php if (edd_cart_has_fees()) : ?> <?php foreach (edd_get_cart_fees() as $fee_id => $fee) : ?> <tr class="edd_cart_fee" id="edd_cart_fee_<?php echo esc_attr($fee_id); ?>"> <?php do_action('edd_cart_fee_rows_before', $fee_id, $fee); ?> <td class="edd_cart_fee_label"><?php echo esc_html($fee['label']); ?></td> <td class="edd_cart_fee_amount"><?php echo esc_html(edd_currency_filter(edd_format_amount($fee['amount']))); ?></td> <td> <?php if (!empty($fee['type']) && 'item' == $fee['type']) : ?> <a href="<?php echo esc_url(edd_remove_cart_fee_url($fee_id)); ?>"> <?php esc_html_e('Remove', 'bdthemes-element-pack'); ?> </a> <?php endif; ?> </td> <?php do_action('edd_cart_fee_rows_after', $fee_id, $fee); ?> </tr> <?php endforeach; ?> <?php endif; ?> <?php do_action('edd_cart_items_after'); ?> </tbody> <tfoot> <?php if (has_action('edd_cart_footer_buttons')) : ?> <tr class="edd_cart_footer_row<?php if (edd_is_cart_saving_disabled()) { echo ' edd-no-js'; } ?>"> <th colspan="<?php echo esc_attr(edd_checkout_cart_columns()); ?>"> <?php do_action('edd_cart_footer_buttons'); ?> </th> </tr> <?php endif; ?> <?php if (edd_use_taxes() && !edd_prices_include_tax()) : ?> <tr class="edd_cart_footer_row edd_cart_subtotal_row" <?php if (!edd_is_cart_taxed()) echo ' style="display:none;"'; ?>> <?php do_action('edd_checkout_table_subtotal_first'); ?> <th colspan="<?php echo esc_attr(edd_checkout_cart_columns()); ?>" class="edd_cart_subtotal"> <?php esc_html_e('Subtotal', 'bdthemes-element-pack'); ?>: <span class="edd_cart_subtotal_amount"><?php echo esc_html(edd_cart_subtotal()); ?></span> </th> <?php do_action('edd_checkout_table_subtotal_last'); ?> </tr> <?php endif; ?> <tr class="edd_cart_footer_row edd_cart_discount_row" <?php if (!edd_cart_has_discounts()) echo ' style="display:none;"'; ?>> <?php do_action('edd_checkout_table_discount_first'); ?> <th colspan="<?php echo esc_attr(edd_checkout_cart_columns()); ?>" class="edd_cart_discount"> <?php edd_cart_discounts_html(); ?> </th> <?php do_action('edd_checkout_table_discount_last'); ?> </tr> <?php if (edd_use_taxes()) : ?> <tr class="edd_cart_footer_row edd_cart_tax_row" <?php if (!edd_is_cart_taxed()) echo ' style="display:none;"'; ?>> <?php do_action('edd_checkout_table_tax_first'); ?> <th colspan="<?php echo esc_attr(edd_checkout_cart_columns()); ?>" class="edd_cart_tax"> <?php esc_html_e('Tax', 'bdthemes-element-pack'); ?>: <span class="edd_cart_tax_amount" data-tax="<?php echo esc_attr(edd_get_cart_tax(false)); ?>"><?php echo esc_html(edd_cart_tax()); ?></span> </th> <?php do_action('edd_checkout_table_tax_last'); ?> </tr> <?php endif; ?> <tr class="edd_cart_footer_row"> <?php do_action('edd_checkout_table_footer_first'); ?> <th colspan="<?php echo esc_attr(edd_checkout_cart_columns()); ?>" class="edd_cart_total"><?php esc_html_e('Total', 'bdthemes-element-pack'); ?>: <span class="edd_cart_amount" data-subtotal="<?php echo esc_attr(edd_get_cart_subtotal()); ?>" data-total="<?php echo esc_attr(edd_get_cart_total()); ?>"><?php edd_cart_total(); ?></span></th> <?php do_action('edd_checkout_table_footer_last'); ?> </tr> </tfoot> </table> <div id="edd_checkout_form_wrap" class="edd_clearfix"> <?php do_action('edd_before_purchase_form'); ?> <form id="edd_purchase_form" class="edd_form" action="<?php echo esc_html($form_action); ?>" method="POST"> <?php /** * Hooks in at the top of the checkout form * * @since 1.0 */ do_action('edd_checkout_form_top'); if (edd_is_ajax_disabled() && !empty($_REQUEST['payment-mode'])) { do_action('edd_purchase_form'); } elseif (edd_show_gateways()) { do_action('edd_payment_mode_select'); } else { do_action('edd_purchase_form'); } /** * Hooks in at the bottom of the checkout form * * @since 1.0 */ do_action('edd_checkout_form_bottom') ?> </form> <?php do_action('edd_after_purchase_form'); ?> </div> </div> <!--end #edd_checkout_form_wrap--> <?php } } <?php namespace ElementPack\Modules\EddCheckout; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'bdt-edd-checkout'; } public function get_widgets() { $widgets = [ 'EDD_Checkout', ]; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('EDD Checkout', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, 'has_style' => true, ]; <?php namespace ElementPack\Modules\GiveGoal\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Typography; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Give_Goal extends Module_Base { public function get_name() { return 'bdt-give-goal'; } public function get_title() { return BDTEP . __('Give Goal', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-give-goal'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['give', 'charity', 'donation', 'donor', 'history', 'wall', 'form', 'goal']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-give-goal']; } } public function get_custom_help_url() { return 'https://youtu.be/WdRBJL7fOvk'; } protected function register_controls() { $this->start_controls_section( 'give_login_settings', [ 'label' => __('Give Goal', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'form_id', [ 'label' => __('Form ID', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => element_pack_give_forms_options(), 'default' => 0 ] ); $this->add_control( 'show_text', [ 'label' => __('Show Amount', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] ); $this->add_control( 'show_bar', [ 'label' => __('Show Progress Bar', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'section_income_style', [ 'label' => esc_html__('Income Amount', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_text' => 'yes' ] ] ); $this->add_control( 'income_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-goal span.income' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'income_spacing', [ 'label' => __('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-give-goal span.income' => 'margin-right: {{SIZE}}{{UNIT}} !important;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'income_typography', 'selector' => '{{WRAPPER}} .bdt-give-goal span.income', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_goal_amount_style', [ 'label' => esc_html__('Goal Amount', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_text' => 'yes' ] ] ); $this->add_control( 'goal_amount_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-goal .raised' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'goal_amount_typography', 'selector' => '{{WRAPPER}} .bdt-give-goal .raised', ] ); $this->end_controls_section(); $this->start_controls_section( 'sectn_style', [ 'label' => esc_html__('Progress Bar', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_bar' => 'yes' ] ] ); $this->add_control( 'progress_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-goal .give-progress-bar>span' => 'background-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'progress_bg_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-goal .give-progress-bar' => 'background-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'progress_border_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-give-goal .give-progress-bar, {{WRAPPER}} .bdt-give-goal .give-progress-bar>span' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'progress_height', [ 'label' => __('Height', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-give-goal .give-progress-bar' => 'height: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'progress_spacing', [ 'label' => __('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-give-goal .give-progress-bar' => 'margin-top: {{SIZE}}{{UNIT}} !important;', ], ] ); $this->end_controls_section(); } private function get_shortcode() { $settings = $this->get_settings_for_display(); if (!$settings['form_id']) { return '<div class="bdt-alert bdt-alert-warning">' . __('Please select a Give Forms From Setting!', 'bdthemes-element-pack') . '</div>'; } $attributes = [ 'id' => $settings['form_id'], 'show_text' => $settings['show_text'], 'show_bar' => $settings['show_bar'] ]; $this->add_render_attribute('shortcode', $attributes); $shortcode = []; $shortcode[] = sprintf('[give_goal %s]', $this->get_render_attribute_string('shortcode')); return implode("", $shortcode); } public function render() { $this->add_render_attribute('give_wrapper', 'class', 'bdt-give-goal'); ?> <div <?php $this->print_render_attribute_string('give_wrapper'); ?>> <?php echo do_shortcode($this->get_shortcode()); ?> </div> <?php } public function render_plain_content() { echo wp_kses_post($this->get_shortcode()); } } <?php namespace ElementPack\Modules\GiveGoal; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'give-goal'; } public function get_widgets() { $widgets = ['Give_Goal']; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Give Goal', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, 'has_style' => true, ]; <?php namespace ElementPack\Modules\BrandCarousel\Widgets; use ElementPack\Base\Module_Base; use Elementor\Group_Control_Css_Filter; use Elementor\Repeater; use Elementor\Controls_Manager; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Image_Size; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Text_Shadow; use Elementor\Group_Control_Background; use Elementor\Group_Control_Border; use ElementPack\Utils; use ElementPack\Traits\Global_Swiper_Controls; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class Brand_Carousel extends Module_Base { use Global_Swiper_Controls; public function get_name() { return 'bdt-brand-carousel'; } public function get_title() { return BDTEP . esc_html__('Brand Carousel', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-brand-carousel'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['brand', 'carousel', 'client', 'logo', 'showcase']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-font', 'ep-brand-carousel']; } } public function get_script_depends() { if ($this->ep_is_edit_mode()) { return ['ep-scripts']; } else { return ['ep-brand-carousel']; } } public function get_custom_help_url() { return 'https://youtu.be/LdCxFzpYuO0'; } protected function register_controls() { $this->start_controls_section( 'ep_section_brand', [ 'label' => esc_html__('Brand Items', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $repeater = new Repeater(); $repeater->add_control( 'image', [ 'label' => esc_html__('Brand Image', 'bdthemes-element-pack'), 'type' => Controls_Manager::MEDIA, 'default' => [ 'url' => Utils::get_placeholder_image_src(), ], ] ); $repeater->add_control( 'brand_name', [ 'label' => esc_html__('Brand Name', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Brand Name', 'bdthemes-element-pack'), 'label_block' => true, 'dynamic' => ['active' => true], ] ); $repeater->add_control( 'link', [ 'label' => esc_html__('Website Url', 'bdthemes-element-pack'), 'type' => Controls_Manager::URL, 'placeholder' => esc_html__('https://your-link.com', 'plugin-domain'), 'show_external' => true, 'default' => [ 'url' => '#', 'is_external' => true, 'nofollow' => true, ], 'label_block' => true, 'dynamic' => ['active' => true], ] ); $repeater->add_control( 'website_link_text', [ 'label' => esc_html__('Website Url Text', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('www.example.com', 'bdthemes-element-pack'), 'placeholder' => esc_html__('Paste URL Text or Type', 'bdthemes-element-pack'), 'label_block' => true, 'dynamic' => ['active' => true], ] ); $this->add_control( 'brand_items', [ 'show_label' => false, 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(), 'title_field' => '{{{ name }}}', 'default' => [ ['image' => ['url' => Utils::get_placeholder_image_src()]], ['image' => ['url' => Utils::get_placeholder_image_src()]], ['image' => ['url' => Utils::get_placeholder_image_src()]], ['image' => ['url' => Utils::get_placeholder_image_src()]], ['image' => ['url' => Utils::get_placeholder_image_src()]], ['image' => ['url' => Utils::get_placeholder_image_src()]], ] ] ); $this->add_group_control( Group_Control_Image_Size::get_type(), [ 'name' => 'thumbnail', 'default' => 'medium', 'separator' => 'before', 'exclude' => ['custom'] ] ); $this->end_controls_section(); $this->start_controls_section( 'section_additional_settings', [ 'label' => esc_html__('Additional Settings', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_responsive_control( 'columns', [ 'label' => esc_html__('Columns', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 3, 'tablet_default' => 2, 'mobile_default' => 1, 'options' => [ 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', ], ] ); $this->add_control( 'item_gap', [ 'label' => esc_html__('Item Gap', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 20, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], ] ); $this->add_control( 'item_match_height', [ 'label' => esc_html__('Item Match Height', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'prefix_class' => 'bdt-item-match-height--', 'render_type' => 'template' ] ); $this->add_control( 'show_brand_name', [ 'label' => esc_html__('Show Brand Name', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before' ] ); $this->add_control( 'brand_html_tag', [ 'label' => esc_html__('Name HTML Tag', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'h3', 'options' => element_pack_title_tags(), 'condition' => [ 'show_brand_name' => 'yes' ] ] ); $this->add_control( 'show_website_link', [ 'label' => esc_html__('Show Link Text', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before' ] ); $this->add_control( 'brand_event', [ 'label' => esc_html__('Select Event ', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'hover-icon', 'options' => [ 'click' => esc_html__('Click', 'bdthemes-element-pack'), 'hover-icon' => esc_html__('Icon Hover', 'bdthemes-element-pack'), 'hover-item' => esc_html__('Item Hover', 'bdthemes-element-pack'), ], 'separator' => 'before' ] ); $this->add_control( 'icon_position', [ 'label' => esc_html__('Icon Position', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'bottom-left', 'options' => [ 'top-left' => esc_html__('Top Left', 'bdthemes-element-pack'), 'top-right' => esc_html__('Top Right', 'bdthemes-element-pack'), 'bottom-left' => esc_html__('Bottom Left', 'bdthemes-element-pack'), 'bottom-right' => esc_html__('Bottom Right', 'bdthemes-element-pack'), 'center-center' => esc_html__('Center Center', 'bdthemes-element-pack'), ], 'prefix_class' => 'bdt-ep-icon--', ] ); $this->end_controls_section(); //Navigation Controls $this->start_controls_section( 'section_content_navigation', [ 'label' => esc_html__('Navigation', 'bdthemes-element-pack'), ] ); //Global Navigation Controls $this->register_navigation_controls(); $this->end_controls_section(); //Global Carousel Settings Controls $this->register_carousel_settings_controls(); //Style $this->start_controls_section( 'section_style_items', [ 'label' => esc_html__('Items', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_item_style'); $this->start_controls_tab( 'tab_item_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'item_background', 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-item', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'item_border', 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-item', 'separator' => 'before', ] ); $this->add_responsive_control( 'item_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'item_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_box_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-item', ] ); $this->add_responsive_control( 'item_shadow_padding', [ 'label' => esc_html__('Match Padding', 'bdthemes-element-pack'), 'description' => esc_html__('You have to add padding for matching overlaping normal/hover box shadow when you used Box Shadow option.', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'step' => 1, 'max' => 50, ] ], 'selectors' => [ '{{WRAPPER}} .swiper-carousel' => 'padding: {{SIZE}}{{UNIT}}; margin: 0 -{{SIZE}}{{UNIT}};' ], ] ); $this->add_control( 'image_heading', [ 'label' => esc_html__('Image', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] ); $this->add_responsive_control( 'brand_image_size', [ 'label' => esc_html__('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 10, 'max' => 500, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-image img' => 'height: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}}; object-fit: cover;', ], ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters', 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-image img', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_item_hover', [ 'label' => esc_html__('Hover', 'bdthemes-element-pack'), ] ); // $this->add_group_control( // Group_Control_Background::get_type(), // [ // 'name' => 'item_hover_background', // 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-item:hover', // ] // ); $this->add_control( 'item_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'item_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-item:hover' => 'border-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_hover_box_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-item:hover', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_icon', [ 'label' => esc_html__('Icon', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'icon_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-icon' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'icon_background', 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-checkbox, {{WRAPPER}} .bdt-ep-brand-carousel-content', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'icon_border', 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-checkbox, {{WRAPPER}} .bdt-ep-brand-carousel-content' ] ); $this->add_control( 'iamge_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-checkbox, {{WRAPPER}} .bdt-ep-brand-carousel-content' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'iamge_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-checkbox, {{WRAPPER}} .bdt-ep-brand-carousel-content' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'icon_size', [ 'label' => esc_html__('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 10, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-checkbox, {{WRAPPER}} .bdt-ep-brand-carousel-content' => 'height: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'icon_font_size', [ 'label' => esc_html__('Font Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-icon' => 'font-size: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'img_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-checkbox, {{WRAPPER}} .bdt-ep-brand-carousel-content' ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_name', [ 'label' => esc_html__('Name', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_brand_name' => 'yes', ] ] ); $this->add_control( 'name_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-name' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'name_typography', 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-name', ] ); $this->add_group_control( Group_Control_Text_Shadow::get_type(), [ 'name' => 'name_shadow', 'label' => esc_html__('Text Shadow', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-name', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_website_link', [ 'label' => esc_html__('Text', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_website_link' => 'yes', ] ] ); $this->add_control( 'website_link_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-link' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'website_link_hover_color', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-link:hover' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'website_link_top_space', [ 'label' => esc_html__('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-brand-carousel-text' => 'padding-top: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'website_link_typography', 'selector' => '{{WRAPPER}} .bdt-ep-brand-carousel-link', ] ); $this->end_controls_section(); //Navigation Style $this->start_controls_section( 'section_style_navigation', [ 'label' => esc_html__('Navigation', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'conditions' => [ 'relation' => 'or', 'terms' => [ [ 'name' => 'navigation', 'operator' => '!=', 'value' => 'none', ], [ 'name' => 'show_scrollbar', 'value' => 'yes', ], ], ], ] ); //Global Navigation Style Controls $this->register_navigation_style_controls('swiper-carousel'); $this->end_controls_section(); } public function render_header() { $settings = $this->get_settings_for_display(); //Global Function $this->render_swiper_header_attribute('brand-carousel'); $this->add_render_attribute('carousel', 'class', 'bdt-ep-brand-carousel'); ?> <div <?php $this->print_render_attribute_string('carousel'); ?>> <div <?php $this->print_render_attribute_string('swiper'); ?>> <div class="swiper-wrapper"> <?php } protected function render_carosuel_item() { $settings = $this->get_settings_for_display(); if (empty($settings['brand_items'])) { return; } ?> <?php foreach ($settings['brand_items'] as $index => $item) : if ($settings['brand_event'] == 'hover-item') { $this->add_render_attribute('item-wrap', 'class', 'bdt-ep-brand-carousel-item bdt-ep-brand-carousel-item-hover swiper-slide', true); } else { $this->add_render_attribute('item-wrap', 'class', 'bdt-ep-brand-carousel-item swiper-slide', true); } $this->add_render_attribute('name-wrap', 'class', 'bdt-ep-brand-carousel-name', true); $link_key = 'link_' . $index; $this->add_render_attribute($link_key, 'class', 'bdt-ep-brand-carousel-link', true); $this->add_link_attributes($link_key, $item['link']); ?> <div <?php $this->print_render_attribute_string('item-wrap'); ?>> <div class="bdt-ep-brand-carousel-image"> <?php $thumb_url = Group_Control_Image_Size::get_attachment_image_src($item['image']['id'], 'thumbnail', $settings); if (!$thumb_url) { printf('<img src="%1$s" alt="%2$s">', esc_url($item['image']['url']), esc_html($item['brand_name'])); } else { print(wp_get_attachment_image( $item['image']['id'], $settings['thumbnail_size'], false, [ 'alt' => esc_html($item['brand_name']) ] )); } ?> </div> <?php if ($settings['brand_event'] == 'click') : ?> <input class="bdt-ep-brand-carousel-checkbox" type="checkbox"> <?php endif; ?> <div class="bdt-ep-brand-carousel-content"> <div class="bdt-ep-brand-carousel-icon"> <i class="ep-icon-plus-2" aria-hidden="true"></i> </div> <div class="bdt-ep-brand-carousel-inner"> <?php if ($item['brand_name'] && $settings['show_brand_name']) : ?> <<?php echo esc_attr(Utils::get_valid_html_tag($settings['brand_html_tag'])); ?> <?php $this->print_render_attribute_string('name-wrap'); ?>> <?php echo wp_kses($item['brand_name'], element_pack_allow_tags('brand_name')); ?> </<?php echo esc_attr(Utils::get_valid_html_tag($settings['brand_html_tag'])); ?>> <?php endif; ?> <?php if (!empty($item['link']['url']) && $settings['show_website_link']) : ?> <div class="bdt-ep-brand-carousel-text"> <a <?php $this->print_render_attribute_string($link_key); ?>> <?php echo esc_html($item['website_link_text']); ?> </a> </div> <?php endif; ?> </div> </div> </div> <?php endforeach; ?> <?php } public function render() { $this->render_header(); $this->render_carosuel_item(); $this->render_footer(); } } <?php namespace ElementPack\Modules\BrandCarousel; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'brand-carousel'; } public function get_widgets() { $widgets = [ 'Brand_Carousel', ]; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Brand Carousel', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => false, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\ImageMagnifier\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use ElementPack\Modules\ImageMagnifier\Skins; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Image_Magnifier extends Module_Base { public function get_name() { return 'bdt-image-magnifier'; } public function get_title() { return BDTEP . esc_html__( 'Image Magnifier', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-image-magnifier'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'image', 'magnifier', 'magnifying', 'zoom' ]; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return [ 'imagezoom' ]; } } public function get_script_depends() { if ($this->ep_is_edit_mode()) { return ['imagezoom', 'imagesloaded', 'ep-scripts']; } else { return [ 'imagezoom', 'imagesloaded', 'ep-image-magnifier' ]; } } public function get_custom_help_url() { return 'https://youtu.be/GSy3pLihNPY'; } protected function register_controls() { $this->start_controls_section( 'section_content_layout', [ 'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ), ] ); $this->start_controls_tabs('tabs_image_choose'); $this->start_controls_tab( 'image_choose_thumb_image', [ 'label' => __('Thumb Image', 'bdthemes-element-pack') ] ); $this->add_control( 'image', [ 'label' => esc_html__( 'Thumb Image', 'bdthemes-element-pack' ), 'type' => Controls_Manager::MEDIA, 'dynamic' => [ 'active' => true ], 'description' => esc_html__( 'If you want to load magnifying image as large so open right tab', 'bdthemes-element-pack' ), ] ); $this->end_controls_tab(); $this->start_controls_tab( 'image_choose_magnify_image', [ 'label' => __('Magnify Image', 'bdthemes-element-pack') ] ); $this->add_control( 'magnify_img', [ 'label' => esc_html__( 'Magnify Image', 'bdthemes-element-pack' ), 'type' => Controls_Manager::MEDIA, 'dynamic' => [ 'active' => true ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->add_control( 'type', [ 'label' => esc_html__( 'Type', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'inner', 'options' => [ 'inner' => esc_html__( 'Inner', 'bdthemes-element-pack' ), 'standard' => esc_html__( 'Standard', 'bdthemes-element-pack' ), 'follow' => esc_html__( 'Follow', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'smooth_move', [ 'label' => esc_html__( 'Smooth Move', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'preload', [ 'label' => esc_html__( 'Preload', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'zoom_ratio', [ 'label' => esc_html__( 'Zoom Ratio', 'bdthemes-element-pack' ), 'type' => Controls_Manager::IMAGE_DIMENSIONS, 'description' => 'Zoom ratio widht and height, such as 480:300', 'condition' => [ 'type' => ['standard', 'follow'] ] ] ); $this->add_control( 'horizontal_offset', [ 'label' => esc_html__( 'Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => '10', ], 'condition' => [ 'type' => 'standard', ], ] ); $this->add_control( 'vertical_offset', [ 'label' => esc_html__( 'Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => '0', ], 'condition' => [ 'type' => 'standard', ], ] ); $this->add_control( 'position', [ 'label' => esc_html__( 'Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'left', 'options' => [ 'right' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'left' => esc_html__( 'Left', 'bdthemes-element-pack' ), ], 'condition' => [ 'type' => 'standard', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_image', [ 'label' => esc_html__( 'Image', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'image_background', [ 'label' => __( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-image-magnifier' => 'background-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'image_padding', [ 'label' => __( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .bdt-image-magnifier' => 'padding: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'image_border', 'label' => __( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-image-magnifier', 'separator' => 'before', ] ); $this->add_control( 'image_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-image-magnifier' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_control( 'image_opacity', [ 'label' => __( 'Opacity (%)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-image-magnifier img' => 'opacity: {{SIZE}};', ], ] ); $this->end_controls_section(); } public function render() { $settings = $this->get_settings_for_display(); $id = 'bdt-image-magnifier-' . $this->get_id(); $image_url = wp_get_attachment_image_src( $settings['image']['id'], 'full' ); $big_image_src = wp_get_attachment_image_src( $settings['magnify_img']['id'], 'full' ); $big_image_src = ( $big_image_src ) ? : $image_url; $horizontal_offset = (isset($settings['horizontal_offset']['size']) ? $settings['horizontal_offset']['size'] : 0); $vertical_offset = (isset($settings['vertical_offset']['size']) ? $settings['vertical_offset']['size'] : 0); $zoom_ratio_width = ( isset($settings['zoom_ratio']['width']) ? $settings['zoom_ratio']['width'] : 480); $zoom_ratio_height = (isset($settings['zoom_ratio']['height']) ? $settings['zoom_ratio']['height'] : 300); $this->add_render_attribute( [ 'image-magnifier' => [ 'class' => [ 'bdt-image-magnifier-image' ], 'src' => esc_attr($image_url[0]), 'alt' => '', ] ] ); $this->add_render_attribute( [ 'image-magnifier-settings' => [ 'data-settings' => [ wp_json_encode(array_filter([ "type" => $settings["type"], "bigImageSrc" => esc_attr($big_image_src[0]), "smoothMove" => $settings["smooth_move"] ? true : false, "preload" => $settings["preload"] ? true : false, "position" => $settings["position"], "zoomSize" => [ (int) $zoom_ratio_width , (int) $zoom_ratio_height ], "offset" => [ (int) $horizontal_offset , (int) $vertical_offset ], ])) ], 'class' => [ 'bdt-image-magnifier', 'bdt-position-relative', ] ] ] ); // type:The image zoom mode.(inner,standard,follow) Default:inner // bigImageSrc:If Call image zoom on the thumb image and want to zoom with large image set this option. Default:null // smoothMove:Is the zoomviewer's image move smooth. (true/false) Default:true // preload:Is ImageZoom preload the large image. Default:true // zoomSize:The ZoomView Size for standard mode and follow mode. Default:[100,100] // offset:Set the offset of the zoomviewer for standard mode. Default:[10,0] // position:Set left/right to show the zoomviewer. Default:right // alignTo:Set the id of the zoomviewer align to (Standard Mode). Default:null (alignTo the riginal image) // descriptionClass:The coustom description css class. Default:null // showDescription:Is zoomimage auto show the image description. Default:true // zoomViewerClass:The coustom class of the zoom viewer for follow mode and standard mode. Default:null // zoomHandlerClass:The coustom class of the zoom handler area for standard mode. Default:null string // onShow:Event when zoom begin. Default:null // onHide:Event when zoom end. Default:null // if ($settings['position']) { // $this->add_render_attribute( 'image-magnifier-settings', 'position', $settings['position'] ); // } if (isset($image_url[0])) { ?> <div <?php $this->print_render_attribute_string( 'image-magnifier-settings' ); ?>> <img <?php $this->print_render_attribute_string( 'image-magnifier' ); ?>> </div> <?php } else { ?> <div class="bdt-alert-warning bdt-text-center">Opps!! You didn't choose any image for magnifying action</div> <?php } } } <?php namespace ElementPack\Modules\ImageMagnifier; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'image-magnifier'; } public function get_widgets() { $widgets = [ 'Image_Magnifier', ]; return $widgets; } } <?php namespace ElementPack\Modules\ImageMagnifier\Skins; use Elementor\Skin_Base as Elementor_Skin_Base; use Elementor\Group_Control_Image_Size; use Elementor\Control_Media; use Elementor\Plugin; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Thumbnail extends Elementor_Skin_Base { public function get_id() { return 'bdt-thumbnail'; } public function get_title() { return __( 'Thumbnail', 'bdthemes-element-pack' ); } public function render_navigation($settings) { ?> <div class="swiper-button-next"></div> <div class="swiper-button-prev"></div> <?php } public function render_header($settings) { ?> <div class="bdt-image-magnifier-skin-thumbnail"> <?php } public function render_footer($settings) { ?> <?php $this->render_script($settings); ?> </div> <?php } public function render_script($settings) { ?> <script> jQuery(document).ready(function($){ var galleryThumbs = new Swiper('.gallery-thumbs', { spaceBetween: 10, slidesPerView: 4, loop: true, freeMode: true, loopedSlides: 5, //looped slides should be the same watchSlidesVisibility: true, watchSlidesProgress: true, autoHeight: true, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, }); var galleryTop = new Swiper('.gallery-top', { spaceBetween: 10, loop:true, autoHeight: true, loopedSlides: 5, //looped slides should be the same thumbs: { swiper: galleryThumbs, }, }); }); </script> <?php } public function render_image($settings, $item) { $image_url = wp_get_attachment_image_src( $item['id'], 'full' ); ?> <div class="swiper-slide"> <img src="<?php echo esc_url($image_url[0]); ?>" alt="<?php echo esc_html(get_the_title()); ?>"> </div> <?php } public function render_slider_thumbnail($settings) { $swiper_class = Plugin::$instance->experiments->is_feature_active( 'e_swiper_latest' ) ? 'swiper' : 'swiper-container'; $this->add_render_attribute('swiper', 'class', 'swiper-carousel gallery-top ' . $swiper_class); ?> <div <?php $this->print_render_attribute_string('swiper'); ?>> <div class="swiper-wrapper"> <?php foreach ( $settings['image_magnifier_gallery'] as $index => $item ) : ?> <?php $this->render_image($settings, $item); ?> <?php endforeach; ?> </div> </div> <?php } public function render_slidenav_thumbnail($settings) { $swiper_class = Plugin::$instance->experiments->is_feature_active( 'e_swiper_latest' ) ? 'swiper' : 'swiper-container'; $this->add_render_attribute('swiper', 'class', 'swiper-carousel gallery-thumbs ' . $swiper_class); ?> <div <?php $this->print_render_attribute_string('swiper'); ?>> <div class="swiper-wrapper"> <?php foreach ( $settings['image_magnifier_gallery'] as $index => $item ) : ?> <?php $this->render_image($settings, $item); ?> <?php endforeach; ?> </div> <?php $this->render_navigation($settings); ?> </div> <?php } public function render() { $settings = $this->parent->get_settings_for_display(); $id = $this->parent->get_id(); if ( empty( $settings['image_magnifier_gallery'] ) ) { return; } $this->render_header($settings); $this->render_slider_thumbnail($settings); $this->render_slidenav_thumbnail($settings); $this->render_footer($settings); } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Image Magnifier', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\TotalCount\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Background; use Elementor\Group_Control_Css_Filter; use Elementor\Icons_Manager; use ElementPack\Utils; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Total_Count extends Module_Base { public function get_name() { return 'bdt-total-count'; } public function get_title() { return BDTEP . esc_html__( 'Total Count', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-total-count'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'advanced', 'icon', 'features', 'total', 'counter' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-styles' ]; } else { return [ 'ep-total-count' ]; } } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'advanced-counter', 'ep-scripts' ]; } else { return [ 'advanced-counter', 'ep-total-count' ]; } } public function get_custom_help_url() { return 'https://youtu.be/1KgG9vTrY8I'; } protected function register_controls() { $this->start_controls_section( 'section_content_counter_box', [ 'label' => esc_html__( 'Count Layout', 'bdthemes-element-pack' ), ] ); $this->add_control( 'count_type', [ 'label' => __( 'Total Count For', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'comment', 'options' => [ 'comment' => __( 'Comment', 'bdthemes-element-pack' ), 'user' => __( 'User', 'bdthemes-element-pack' ), 'post' => __( 'Post', 'bdthemes-element-pack' ), // 'views' => __( 'Total Views', 'bdthemes-element-pack' ), //TODO ], ] ); $this->add_control( 'comment_count_type', [ 'label' => __( 'Comment Count Type', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'total', 'options' => [ 'total' => __( 'Total Comment', 'bdthemes-element-pack' ), 'moderated' => __( 'Total Moderated', 'bdthemes-element-pack' ), 'approved' => __( 'Total Approved', 'bdthemes-element-pack' ), 'spam' => __( 'Total Spam', 'bdthemes-element-pack' ), 'trash' => __( 'Total trashed', 'bdthemes-element-pack' ), ], 'condition' => [ 'count_type' => 'comment' ] ] ); $this->add_control( 'custom_post_type', [ 'label' => esc_html__( 'Specific Post Type', 'bdthemes-element-pack' ), 'description' => esc_html__( 'Select post type if you need to search only this post type content.', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'post', 'options' => element_pack_get_post_types(), 'condition' => [ 'count_type' => 'post' ] ] ); $this->add_control( 'user_roles', [ 'label' => esc_html__( 'Select User Type', 'bdthemes-element-pack' ), 'description' => esc_html__( 'What type user count you want to show there.', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'bdt-all-users', 'options' => element_pack_user_roles(), 'condition' => [ 'count_type' => 'user' ] ] ); $this->add_control( 'show_icon', [ 'label' => esc_html__( 'Show Icon / Image', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'icon_type', [ 'label' => esc_html__( 'Icon Type', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'toggle' => false, 'default' => 'icon', 'prefix_class' => 'bdt-icon-type-', 'render_type' => 'template', 'options' => [ 'icon' => [ 'title' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'icon' => 'eicon-star', ], 'image' => [ 'title' => esc_html__( 'Image', 'bdthemes-element-pack' ), 'icon' => 'eicon-image', ], ], 'condition' => [ 'show_icon' => 'yes', ] ] ); $this->add_control( 'selected_icon', [ 'label' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::ICONS, 'default' => [ 'value' => 'fas fa-star', 'library' => 'fa-solid', ], 'render_type' => 'template', 'condition' => [ 'icon_type' => 'icon', 'show_icon' => 'yes', ], ] ); $this->add_control( 'image', [ 'label' => esc_html__( 'Image Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::MEDIA, 'render_type' => 'template', 'default' => [ 'url' => Utils::get_placeholder_image_src(), ], 'condition' => [ 'icon_type' => 'image', ], ] ); $this->add_control( 'count_start', [ 'label' => esc_html__( 'Count Start Number', 'bdthemes-element-pack' ), 'type' => Controls_Manager::NUMBER, 'default' => 0, 'placeholder' => esc_html__( 'Enter your count number', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'fake_count', [ 'label' => esc_html__( 'Fake Addition', 'bdthemes-element-pack' ), 'description' => esc_html__( 'If you want to impress user by some fake data so you can set it here.', 'bdthemes-element-pack' ), 'type' => Controls_Manager::NUMBER, 'default' => 0, 'placeholder' => esc_html__( 'Enter fake amount', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'show_separator', [ 'label' => esc_html__( 'Separator', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'content_text', [ 'label' => esc_html__( 'Count Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Total Count', 'bdthemes-element-pack' ), 'placeholder' => esc_html__( 'Enter your content text', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'counter_number_size', [ 'label' => esc_html__( 'Text HTML Tag', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'h4', 'options' => element_pack_title_tags(), ] ); $this->add_control( 'position', [ 'label' => esc_html__( 'Icon Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'default' => 'top', 'options' => [ 'left' => [ 'title' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-left', ], 'top' => [ 'title' => esc_html__( 'Top', 'bdthemes-element-pack' ), 'icon' => 'eicon-v-align-top', ], 'right' => [ 'title' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-right', ], ], 'prefix_class' => 'elementor-position-', 'toggle' => false, 'render_type' => 'template', 'condition' => [ 'show_icon' => 'yes', ], ] ); $this->add_control( 'icon_inline', [ 'label' => esc_html__( 'Icon Inline', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'position' => [ 'left', 'right' ], ], ] ); $this->add_control( 'icon_vertical_alignment', [ 'label' => esc_html__( 'Icon Vertical Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'top' => [ 'title' => esc_html__( 'Top', 'bdthemes-element-pack' ), 'icon' => 'eicon-v-align-top', ], 'middle' => [ 'title' => esc_html__( 'Middle', 'bdthemes-element-pack' ), 'icon' => 'eicon-v-align-middle', ], 'bottom' => [ 'title' => esc_html__( 'Bottom', 'bdthemes-element-pack' ), 'icon' => 'eicon-v-align-bottom', ], ], 'default' => 'top', 'toggle' => false, 'prefix_class' => 'elementor-vertical-align-', 'condition' => [ 'position' => [ 'left', 'right' ], 'icon_inline' => '', ], ] ); $this->add_responsive_control( 'text_align', [ 'label' => esc_html__( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => esc_html__( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count' => 'text-align: {{VALUE}};', ], ] ); $this->add_control( 'icon_offset_toggle', [ 'label' => __( 'Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::POPOVER_TOGGLE, 'label_off' => __( 'None', 'bdthemes-element-pack' ), 'label_on' => __( 'Custom', 'bdthemes-element-pack' ), 'return_value' => 'yes', 'condition' => [ 'show_icon' => 'yes', ], ] ); $this->start_popover(); $this->add_responsive_control( 'top_icon_vertical_offset', [ 'label' => esc_html__( 'Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 200, ], ], 'condition' => [ 'position' => 'top', 'show_icon' => 'yes', 'icon_offset_toggle' => 'yes' ], 'render_type' => 'ui', 'selectors' => [ '{{WRAPPER}}' => '--ep-top-icon-v-offset: -{{SIZE}}px;' ], ] ); $this->add_responsive_control( 'top_icon_horizontal_offset', [ 'label' => esc_html__( 'Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -200, 'max' => 200, ], ], 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'condition' => [ 'position' => 'top', 'show_icon' => 'yes', 'icon_offset_toggle' => 'yes' ], 'render_type' => 'ui', 'selectors' => [ '{{WRAPPER}} .bdt-total-count-icon' => '-webkit-transform: translate({{SIZE}}px, var(--ep-top-icon-v-offset, 0)); transform: translate({{SIZE}}px, var(--ep-top-icon-v-offset, 0));' ], ] ); $this->add_responsive_control( 'left_right_icon_horizontal_offset', [ 'label' => esc_html__( 'Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -200, 'max' => 200, ], ], 'condition' => [ 'position' => [ 'left', 'right' ], 'show_icon' => 'yes', 'icon_offset_toggle' => 'yes' ], 'render_type' => 'ui', 'selectors' => [ '{{WRAPPER}}' => '--ep-left-right-icon-h-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'left_right_icon_vertical_offset', [ 'label' => esc_html__( 'Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -200, 'max' => 200, ], ], 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'condition' => [ 'position' => [ 'left', 'right' ], 'show_icon' => 'yes', 'icon_offset_toggle' => 'yes' ], 'render_type' => 'ui', 'selectors' => [ '{{WRAPPER}} .bdt-total-count-icon' => '-webkit-transform: translate(var(--ep-left-right-icon-h-offset, 0), {{SIZE}}px); transform: translate(var(--ep-left-right-icon-h-offset, 0), {{SIZE}}px);' ], ] ); $this->end_popover(); $this->end_controls_section(); $this->start_controls_section( 'section_content_additional', [ 'label' => esc_html__( 'Additional Options', 'bdthemes-element-pack' ), ] ); $this->add_control( 'language_input', [ 'label' => esc_html__( 'Language', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXTAREA, 'default' => '0,1,2,3,4,5,6,7,8,9', 'placeholder' => esc_html__( 'Enter your language number', 'bdthemes-element-pack' ), 'rows' => 10, 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'decimal_symbol', [ 'label' => esc_html__( 'Decimal Symbol', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => '.', 'placeholder' => esc_html__( 'Enter your Decimal Symbol', 'bdthemes-element-pack' ), ] ); $this->add_control( 'decimal_places', [ 'label' => esc_html__( 'Decimal places', 'bdthemes-element-pack' ), 'type' => Controls_Manager::NUMBER, 'default' => '0', 'placeholder' => esc_html__( 'Enter your Decimal places', 'bdthemes-element-pack' ), ] ); $this->add_control( 'duration', [ 'label' => esc_html__( 'Duration', 'bdthemes-element-pack' ), 'type' => Controls_Manager::NUMBER, 'default' => '2', 'placeholder' => esc_html__( 'Enter your Duration', 'bdthemes-element-pack' ), ] ); $this->add_control( 'use_easing', [ 'label' => esc_html__( 'Use Easing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] ); $this->add_control( 'use_grouping', [ 'label' => esc_html__( 'Use Grouping', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'no' ] ); $this->add_control( 'counter_separator', [ 'label' => esc_html__( 'Separator Symbol', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => ',', 'placeholder' => esc_html__( 'Enter your Decimal places', 'bdthemes-element-pack' ), 'condition' => [ 'use_grouping' => 'yes', ] ] ); $this->add_control( 'counter_prefix', [ 'label' => esc_html__( 'Prefix', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'placeholder' => esc_html__( 'Enter your Prefix', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'counter_suffix', [ 'label' => esc_html__( 'Suffix', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'placeholder' => esc_html__( 'Enter your Suffix', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'section_style_counter_box', [ 'label' => esc_html__( 'Icon/Image', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_icon' => 'yes', ], ] ); $this->start_controls_tabs( 'icon_colors' ); $this->start_controls_tab( 'icon_colors_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'icon_color', [ 'label' => esc_html__( 'Icon Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper svg' => 'fill: {{VALUE}};', ], 'condition' => [ 'icon_type!' => 'image', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'icon_background', 'selector' => '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper', ] ); $this->add_responsive_control( 'icon_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'icon_border', 'selector' => '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper', ] ); $this->add_responsive_control( 'icon_radius', [ 'label' => esc_html__( 'Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], 'condition' => [ 'icon_radius_advanced_show!' => 'yes', ], ] ); $this->add_control( 'icon_radius_advanced_show', [ 'label' => esc_html__( 'Advanced Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'icon_radius_advanced', [ 'label' => esc_html__( 'Radius', 'bdthemes-element-pack' ), 'description' => sprintf( __( 'For example: <b>%1s</b> or Go <a href="%2s" target="_blank">this link</a> and copy and paste the radius value.', 'bdthemes-element-pack' ), '75% 25% 43% 57% / 46% 29% 71% 54%', 'https://9elements.github.io/fancy-border-radius/' ), 'type' => Controls_Manager::TEXT, 'size_units' => [ 'px', '%' ], 'default' => '75% 25% 43% 57% / 46% 29% 71% 54%', 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper' => 'border-radius: {{VALUE}}; overflow: hidden;', '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper img' => 'border-radius: {{VALUE}}; overflow: hidden;', ], 'condition' => [ 'icon_radius_advanced_show' => 'yes', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'icon_shadow', 'selector' => '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'icon_typography', 'selector' => '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper', 'condition' => [ 'icon_type!' => 'image', ], ] ); $this->add_responsive_control( 'icon_space', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}}.elementor-position-right .bdt-total-count-icon' => 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}}.elementor-position-left .bdt-total-count-icon' => 'margin-right: {{SIZE}}{{UNIT}};', '{{WRAPPER}}.elementor-position-top .bdt-total-count-icon' => 'margin-bottom: {{SIZE}}{{UNIT}};', '(mobile){{WRAPPER}} .bdt-total-count-icon' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'image_fullwidth', [ 'label' => esc_html__( 'Image Fullwidth', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper' => 'width: 100%;box-sizing: border-box;', ], 'condition' => [ 'icon_type' => 'image', ], ] ); $this->add_responsive_control( 'icon_size', [ 'label' => esc_html__( 'Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px', 'em', 'vh', 'vw' ], 'range' => [ 'px' => [ 'min' => 6, 'max' => 300, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper' => 'font-size: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}};', ], 'conditions' => [ 'relation' => 'or', 'terms' => [ [ 'name' => 'image_fullwidth', 'operator' => '==', 'value' => '', ], [ 'name' => 'icon_type', 'operator' => '==', 'value' => 'icon', ], ], ], ] ); $this->add_control( 'rotate', [ 'label' => esc_html__( 'Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, 'unit' => 'deg', ], 'range' => [ 'deg' => [ 'max' => 360, 'min' => -360, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper i' => 'transform: rotate({{SIZE}}{{UNIT}});', '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper img' => 'transform: rotate({{SIZE}}{{UNIT}});', ], ] ); $this->add_control( 'icon_background_rotate', [ 'label' => esc_html__( 'Background Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, 'unit' => 'deg', ], 'range' => [ 'deg' => [ 'max' => 360, 'min' => -360, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-total-count-icon-wrapper' => 'transform: rotate({{SIZE}}{{UNIT}});', ], ] ); $this->add_control( 'image_icon_heading', [ 'label' => esc_html__( 'Image Effect', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'separator' => 'before', 'condition' => [ 'icon_type' => 'image', ], ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters', 'selector' => '{{WRAPPER}} .bdt-total-count img', 'condition' => [ 'icon_type' => 'image', ], ] ); $this->add_control( 'image_opacity', [ 'label' => esc_html__( 'Opacity', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count img' => 'opacity: {{SIZE}};', ], 'condition' => [ 'icon_type' => 'image', ], ] ); $this->add_control( 'background_hover_transition', [ 'label' => esc_html__( 'Transition Duration', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0.3, ], 'range' => [ 'px' => [ 'max' => 3, 'step' => 0.1, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count img' => 'transition-duration: {{SIZE}}s', ], 'condition' => [ 'icon_type' => 'image', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'icon_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'icon_hover_color', [ 'label' => esc_html__( 'Icon Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper svg' => 'fill: {{VALUE}};', ], 'condition' => [ 'icon_type!' => 'image', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'icon_hover_background', 'selector' => '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper:after', ] ); $this->add_control( 'icon_effect', [ 'label' => esc_html__( 'Effect', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'prefix_class' => 'bdt-icon-effect-', 'default' => 'none', 'options' => [ 'none' => esc_html__( 'None', 'bdthemes-element-pack' ), 'a' => esc_html__( 'Effect A', 'bdthemes-element-pack' ), 'b' => esc_html__( 'Effect B', 'bdthemes-element-pack' ), 'c' => esc_html__( 'Effect C', 'bdthemes-element-pack' ), 'd' => esc_html__( 'Effect D', 'bdthemes-element-pack' ), 'e' => esc_html__( 'Effect E', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'icon_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper' => 'border-color: {{VALUE}};', ], 'condition' => [ 'icon_border_border!' => '', ], ] ); $this->add_control( 'icon_hover_radius', [ 'label' => esc_html__( 'Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'icon_hover_shadow', 'selector' => '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper', ] ); $this->add_control( 'icon_hover_rotate', [ 'label' => esc_html__( 'Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => 'deg', ], 'range' => [ 'deg' => [ 'max' => 360, 'min' => -360, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper i' => 'transform: rotate({{SIZE}}{{UNIT}});', '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper img' => 'transform: rotate({{SIZE}}{{UNIT}});', ], ] ); $this->add_control( 'icon_hover_background_rotate', [ 'label' => esc_html__( 'Background Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => 'deg', ], 'range' => [ 'deg' => [ 'max' => 360, 'min' => -360, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper' => 'transform: rotate({{SIZE}}{{UNIT}});', ], ] ); $this->add_control( 'image_icon_hover_heading', [ 'label' => esc_html__( 'Image Effect', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'separator' => 'before', 'condition' => [ 'icon_type' => 'image', ], ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters_hover', 'selector' => '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper img', 'condition' => [ 'icon_type' => 'image', ], ] ); $this->add_control( 'image_opacity_hover', [ 'label' => esc_html__( 'Opacity', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-icon-wrapper img' => 'opacity: {{SIZE}};', ], 'condition' => [ 'icon_type' => 'image', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_counter_number', [ 'label' => esc_html__( 'Count Number', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs( 'tabs_counter_number_style' ); $this->start_controls_tab( 'tab_counter_number_style_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_responsive_control( 'counter_number_bottom_space', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count-number' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'counter_number_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-total-count-content .bdt-total-count-number' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'counter_number_typography', 'selector' => '{{WRAPPER}} .bdt-total-count-content .bdt-total-count-number', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_counter_number_style_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'counter_number_color_hover', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-content .bdt-total-count-number' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'counter_number_typography_hover', 'selector' => '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-content .bdt-total-count-number', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_content_text', [ 'label' => esc_html__( 'Count Text', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs( 'tabs_content_text_style' ); $this->start_controls_tab( 'tab_content_text_style_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'content_text_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-total-count-content .bdt-total-count-content-text' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'content_text_typography', 'selector' => '{{WRAPPER}} .bdt-total-count-content .bdt-total-count-content-text', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_content_text_style_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'content_text_color_hover', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-content .bdt-total-count-content-text' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'content_text_typography_hover', 'selector' => '{{WRAPPER}} .bdt-total-count:hover .bdt-total-count-content .bdt-total-count-content-text', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_content_counter_number_separator', [ 'label' => esc_html__( 'Separator', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_separator' => 'yes', ], ] ); $this->add_control( 'counter_number_separator_type', [ 'label' => esc_html__( 'Separator Type', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'line', 'options' => [ 'line' => esc_html__( 'Line', 'bdthemes-element-pack' ), 'bloomstar' => esc_html__( 'Bloomstar', 'bdthemes-element-pack' ), 'bobbleaf' => esc_html__( 'Bobbleaf', 'bdthemes-element-pack' ), 'demaxa' => esc_html__( 'Demaxa', 'bdthemes-element-pack' ), 'fill-circle' => esc_html__( 'Fill Circle', 'bdthemes-element-pack' ), 'finalio' => esc_html__( 'Finalio', 'bdthemes-element-pack' ), 'jemik' => esc_html__( 'Jemik', 'bdthemes-element-pack' ), 'leaf-line' => esc_html__( 'Leaf Line', 'bdthemes-element-pack' ), 'multinus' => esc_html__( 'Multinus', 'bdthemes-element-pack' ), 'rotate-box' => esc_html__( 'Rotate Box', 'bdthemes-element-pack' ), 'sarator' => esc_html__( 'Sarator', 'bdthemes-element-pack' ), 'separk' => esc_html__( 'Separk', 'bdthemes-element-pack' ), 'slash-line' => esc_html__( 'Slash Line', 'bdthemes-element-pack' ), 'tripline' => esc_html__( 'Tripline', 'bdthemes-element-pack' ), 'vague' => esc_html__( 'Vague', 'bdthemes-element-pack' ), 'zigzag-dot' => esc_html__( 'Zigzag Dot', 'bdthemes-element-pack' ), 'zozobe' => esc_html__( 'Zozobe', 'bdthemes-element-pack' ), ], //'render_type' => 'none', ] ); $this->add_control( 'counter_number_separator_border_style', [ 'label' => esc_html__( 'Separator Style', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'solid' => esc_html__( 'Solid', 'bdthemes-element-pack' ), 'dotted' => esc_html__( 'Dotted', 'bdthemes-element-pack' ), 'dashed' => esc_html__( 'Dashed', 'bdthemes-element-pack' ), 'groove' => esc_html__( 'Groove', 'bdthemes-element-pack' ), ], 'condition' => [ 'counter_number_separator_type' => 'line', ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-number-separator' => 'border-top-style: {{VALUE}};', ], ] ); $this->add_control( 'counter_number_separator_line_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'counter_number_separator_type' => 'line', ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-number-separator' => 'border-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'counter_number_separator_height', [ 'label' => esc_html__( 'Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 1, 'max' => 15, ], ], 'condition' => [ 'counter_number_separator_type' => 'line', ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-number-separator' => 'border-top-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'counter_number_separator_width', [ 'label' => esc_html__( 'Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px', '%' ], 'range' => [ '%' => [ 'min' => 1, 'max' => 100, ], 'px' => [ 'min' => 1, 'max' => 300, ], ], 'condition' => [ 'counter_number_separator_type' => 'line', ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-number-separator' => 'width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'counter_number_separator_svg_fill_color', [ 'label' => esc_html__( 'Fill Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'counter_number_separator_type!' => 'line', ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-number-separator-wrapper svg *' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'counter_number_separator_svg_stroke_color', [ 'label' => esc_html__( 'Stroke Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'counter_number_separator_type!' => 'line', ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-number-separator-wrapper svg *' => 'stroke: {{VALUE}};', ], ] ); $this->add_responsive_control( 'counter_number_separator_svg_width', [ 'label' => esc_html__( 'Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px', '%' ], 'range' => [ '%' => [ 'min' => 1, 'max' => 100, ], 'px' => [ 'min' => 1, 'max' => 300, ], ], 'condition' => [ 'counter_number_separator_type!' => 'line', ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-number-separator-wrapper > *' => 'width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'counter_number_separator_spacing', [ 'label' => esc_html__( 'Separator Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-number-separator-wrapper' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_additional', [ 'label' => esc_html__( 'Additional', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'content_padding', [ 'label' => esc_html__( 'Content Inner Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-total-count-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'icon_inline_spacing', [ 'label' => esc_html__( 'Icon Inline Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 100, ], ], 'condition' => [ 'position' => [ 'left', 'right' ], 'icon_inline' => 'yes', ], 'selectors' => [ '{{WRAPPER}} .bdt-total-count .bdt-icon-heading' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); } protected function render_icon() { $settings = $this->get_settings_for_display(); $has_icon = ! empty( $settings['selected_icon'] ); $has_image = ! empty( $settings['image']['url'] ); if ( $has_image and 'image' == $settings['icon_type'] ) { $this->add_render_attribute( 'image-icon', 'src', $settings['image']['url'] ); $this->add_render_attribute( 'image-icon', 'alt', $settings['content_text'] ); } if ( ! $has_icon && ! empty( $settings['selected_icon']['value'] ) ) { $has_icon = true; } ?> <?php if ( 'yes' == $settings['show_icon'] ) : ?> <?php if ( $has_icon or $has_image ) : ?> <div class="bdt-total-count-icon"> <span class="bdt-total-count-icon-wrapper"> <?php if ( $has_icon and 'icon' == $settings['icon_type'] ) { ?> <?php Icons_Manager::render_icon( $settings['selected_icon'], [ 'aria-hidden' => 'true' ] ); ?> <?php } elseif ( $has_image and 'image' == $settings['icon_type'] ) { ?> <img <?php $this->print_render_attribute_string( 'image-icon' ); ?>> <?php } ?> </span> </div> <?php endif; ?> <?php endif; ?> <?php } protected function render_icon_heading() { $settings = $this->get_settings_for_display(); $this->add_render_attribute( 'total-count-content-number', 'class', 'bdt-total-count-number' ); if ( 'yes' == $settings['icon_inline'] ) { $this->add_render_attribute( 'total-count-icon-heading', 'class', 'bdt-icon-heading bdt-flex bdt-flex-middle' ); } if ( 'right' == $settings['position'] ) { $this->add_render_attribute( 'total-count-icon-heading', 'class', 'bdt-flex-row-reverse' ); } ?> <div <?php $this->print_render_attribute_string( 'total-count-icon-heading' ); ?>> <?php $this->render_icon(); ?> <div class="bdt-counter-box-title-wrapper"> <?php //if ( $settings['fake_count'] ): ?> <div <?php $this->print_render_attribute_string( 'total-count-content-number' ); ?>> <span <?php $this->print_render_attribute_string( 'content_number' ); ?>> <?php $this->render_total_count(); ?> </span> </div> <?php //endif; ?> </div> </div> <?php } public function render_total_count() { $settings = $this->get_settings_for_display(); $fake_count = $settings['fake_count']; $output = ''; if ( $settings['count_type'] == 'comment' ) { $output = element_pack_total_comment( $settings['comment_count_type'] ); } elseif ( $settings['count_type'] == 'user' ) { $output = element_pack_total_user( $settings['user_roles'] ); } elseif ( $settings['count_type'] == 'post' ) { $output = element_pack_total_post( $settings['custom_post_type'] ); } return $output + $fake_count; } protected function render_heading() { $settings = $this->get_settings_for_display(); $id = $this->get_id(); $this->add_render_attribute( 'total-count-content-number', 'class', 'bdt-total-count-number' ); ?> <div <?php $this->print_render_attribute_string( 'total-count-content-number' ); ?>> <span class="bdt-count-this" id="bdt-total-count-data-<?php echo esc_attr( $id ); ?>" <?php $this->print_render_attribute_string( 'content_number' ); ?>> <?php $this->render_total_count(); ?> </span> </div> <?php } protected function render() { $settings = $this->get_settings_for_display(); $this->add_render_attribute( 'content_text', 'class', 'bdt-total-count-content-text' ); $this->add_inline_editing_attributes( 'content_number', 'none' ); $this->add_inline_editing_attributes( 'content_text' ); $this->add_render_attribute( 'total-count', 'class', 'bdt-advanced-counter bdt-total-count' ); // echo $countStart; $this->add_render_attribute( [ 'advanced_counter_data' => [ 'data-settings' => [ wp_json_encode( [ "id" => 'bdt-total-count-data-' . $this->get_id(), "countStart" => $settings['count_start'], "countNumber" => $this->render_total_count(), "language" => explode( ',', $settings['language_input'] ), "decimalPlaces" => $settings['decimal_places'], "duration" => $settings['duration'], "useEasing" => $settings['use_easing'], "useGrouping" => $settings['use_grouping'], "counterSeparator" => $settings['counter_separator'], "decimalSymbol" => $settings['decimal_symbol'], "counterPrefix" => $settings['counter_prefix'], "counterSuffix" => $settings['counter_suffix'], ] ), ], ], ] ); // end send unique data ?> <div <?php $this->print_render_attribute_string( 'total-count' ); ?> <?php $this->print_render_attribute_string( 'advanced_counter_data' ); ?>> <?php if ( '' == $settings['icon_inline'] ) : ?> <?php $this->render_icon(); ?> <?php endif; ?> <div class="bdt-total-count-content"> <?php if ( 'yes' == $settings['icon_inline'] ) : ?> <?php $this->render_icon_heading(); ?> <?php else : ?> <?php $this->render_heading(); ?> <?php endif; ?> <?php if ( $settings['show_separator'] ) : ?> <?php if ( 'line' == $settings['counter_number_separator_type'] ) : ?> <div class="bdt-number-separator-wrapper"> <div class="bdt-number-separator"></div> </div> <?php elseif ( 'line' != $settings['counter_number_separator_type'] ) : ?> <div class="bdt-number-separator-wrapper"> <?php $svg_image = BDTEP_ASSETS_PATH . 'images/separator/' . $settings['counter_number_separator_type'] . '.svg'; if ( file_exists( $svg_image ) ) { ob_start(); include $svg_image; $svg_image = ob_get_clean(); echo wp_kses( $svg_image, element_pack_allow_tags( 'svg' ) ); } ?> </div> <?php endif; ?> <?php endif; ?> <?php if ( $settings['content_text'] ) : ?> <<?php echo esc_attr( Utils::get_valid_html_tag( $settings['counter_number_size'] ) ); ?> <?php $this->print_render_attribute_string( 'content_text' ); ?> > <?php echo wp_kses( $settings['content_text'], element_pack_allow_tags( 'text' ) ); ?> </<?php echo esc_attr( Utils::get_valid_html_tag( $settings['counter_number_size'] ) ); ?>> <?php endif; ?> </div> </div> <?php } } <?php namespace ElementPack\Modules\TotalCount; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'total-count'; } public function get_widgets() { $widgets = [ 'Total_Count', ]; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Total Count', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\RemoteFraction\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Text_Shadow; use Elementor\Group_Control_Typography; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class Remote_Fraction extends Module_Base { public function get_name() { return 'bdt-remote-fraction'; } public function get_title() { return BDTEP . esc_html__('Remote Fraction', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-remote-fraction bdt-new'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['remote', 'fraction', 'arrows']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return [ 'ep-styles' ]; } else { return [ 'ep-font', 'ep-remote-fraction' ]; } } public function get_script_depends() { if ($this->ep_is_edit_mode()) { return ['ep-scripts']; } else { return ['ep-remote-fraction']; } } public function get_custom_help_url() { return 'https://youtu.be/UfmwcTjX7L8'; } protected function register_controls() { $this->start_controls_section( 'section_remote_fraction', [ 'label' => esc_html__('Remote Fraction', 'bdthemes-element-pack'), ] ); $this->add_control( 'remote_id', [ 'label' => esc_html__('Remote Carousel ID', 'bdthemes-element-pack'), 'description' => esc_html__('Unique ID of Carousel. Not need to add #', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'dynamic' => ['active' => true], ] ); $this->add_responsive_control( 'vertical_align', [ 'label' => esc_html__('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'top' => [ 'title' => esc_html__('Top', 'bdthemes-element-pack'), 'icon' => 'eicon-v-align-top', ], 'middle' => [ 'title' => esc_html__('Middle', 'bdthemes-element-pack'), 'icon' => 'eicon-v-align-middle', ], 'bottom' => [ 'title' => esc_html__('Bottom', 'bdthemes-element-pack'), 'icon' => 'eicon-v-align-bottom', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-fraction-wrapper' => '{{VALUE}};', ], 'selectors_dictionary' => [ 'top' => 'align-items: flex-start;', 'middle' => 'align-items: center;', 'bottom' => 'align-items: baseline;' ] ] ); $this->add_control( 'digit_pad', [ 'label' => esc_html__('Digit Pad', 'bdthemes-element-pack'), 'type' => Controls_Manager::NUMBER, 'default' => 2, ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_fraction', [ 'label' => esc_html__('Remote Fraction', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'fraction_align', [ 'label' => esc_html__('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'flex-start' => [ 'title' => esc_html__('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-center', ], 'flex-end' => [ 'title' => esc_html__('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-right', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-fraction-wrapper' => 'justify-content: {{VALUE}};', ], ] ); $this->add_responsive_control( 'fraction_spacing', [ 'label' => esc_html__('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px', 'em'], 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], 'em' => [ 'min' => 0, 'max' => 5, 'step' => .1, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-fraction-wrapper' => 'grid-gap: {{SIZE}}{{UNIT}};', ], ] ); $this->start_controls_tabs('tabs_fraction_style'); $this->start_controls_tab( 'fraction_current', [ 'label' => esc_html__('Current', 'bdthemes-element-pack'), ] ); $this->add_control( 'text_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-current' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Text_Shadow::get_type(), [ 'name' => 'text_shadow', 'label' => esc_html__('Text Shadow', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-current', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'fraction_typography', 'selector' => '{{WRAPPER}} .bdt-current', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'fraction_active', [ 'label' => esc_html__('Total', 'bdthemes-element-pack'), ] ); $this->add_control( 'text_color_total', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-total' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Text_Shadow::get_type(), [ 'name' => 'text_shadow_total', 'label' => esc_html__('Text Shadow', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-total', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'fraction_typography_total', 'selector' => '{{WRAPPER}} .bdt-total', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_separator', [ 'label' => esc_html__('Separator', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'separator_total', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-fr-separator' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'separator_size', [ 'label' => __('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 10, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-fr-separator' => 'font-size: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); $id = 'bdt-remote-fraction-' . $this->get_id(); $this->add_render_attribute('remote', [ 'class' => 'bdt-remote-fraction', 'id' => $id, 'data-settings' => [ wp_json_encode(array_filter([ 'id' => '#' . $id, 'remoteId' => !empty($settings['remote_id']) ? '#' . $settings['remote_id'] : false, 'pad' => !empty($settings['digit_pad']) ? $settings['digit_pad'] : 1, ])) ] ]); ?> <div <?php $this->print_render_attribute_string('remote'); ?>> <div class="bdt-fraction-wrapper"> <span class="bdt-current"></span> <span class="bdt-fr-separator">/</span> <span class="bdt-total"></span> </div> </div> <div id="<?php echo esc_attr($id) . '-notice' ?>" class="bdt-alert-danger bdt-hidden" bdt-alert> <a class="bdt-alert-close" bdt-close></a> <p><?php echo esc_html__('Sorry, your ID is maybe not correct. And please make sure that your selected element is developed with Swiper.', 'bdthemes-element-pack'); ?></p> </div> <?php } } <?php namespace ElementPack\Modules\RemoteFraction; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'remote-fraction'; } public function get_widgets() { $widgets = [ 'Remote_Fraction', ]; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Remote Fraction', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => false, 'has_script' => true, 'has_style' => true, ]; <?php namespace ElementPack\Modules\CookieConsent\Widgets; use Elementor\Controls_Manager; use Elementor\Group_Control_Typography; use ElementPack\Base\Module_Base; use ElementPack\Element_Pack_Loader; use ElementPack\Utils; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Cookie_Consent extends Module_Base { public function get_name() { return 'bdt-cookie-consent'; } public function get_title() { return BDTEP . esc_html__( 'Cookie Consent', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-cookie-consent'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'cookie', 'consent' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-styles' ]; } else { return [ 'ep-cookie-consent' ]; } } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'cookieconsent', 'ep-scripts' ]; } else { return [ 'cookieconsent', 'ep-cookie-consent' ]; } } public function get_custom_help_url() { return 'https://youtu.be/BR4t5ngDzqM'; } protected function register_controls() { $this->start_controls_section( 'section_content_layout', [ 'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ), ] ); $this->add_control( 'message', [ 'label' => esc_html__( 'Message', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXTAREA, 'default' => 'This website uses cookies to ensure you get the best experience on our website. ', 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'button_text', [ 'label' => esc_html__( 'Button Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true ], 'default' => 'Got it!', ] ); $this->add_control( 'learn_more_text', [ 'label' => esc_html__( 'Learn More Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'placeholder' => esc_html__( 'Learn more', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], 'default' => 'Learn more', ] ); $this->add_control( 'learn_more_link', [ 'label' => esc_html__( 'Learn More Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::URL, 'show_external' => false, 'placeholder' => esc_html__( 'https://your-link.com', 'bdthemes-element-pack' ), 'default' => [ 'url' => 'http://cookiesandyou.com/', ], 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'position', [ 'label' => esc_html__( 'Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'bottom', 'options' => [ 'bottom' => esc_html__( 'Bottom', 'bdthemes-element-pack' ), 'bottom-left' => esc_html__( 'Bottom Left', 'bdthemes-element-pack' ), 'bottom-right' => esc_html__( 'Bottom Right', 'bdthemes-element-pack' ), 'top' => esc_html__( 'Top', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'pushdown', [ 'label' => esc_html__( 'Show Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'position' => 'top', ], ] ); $this->add_control( 'expiry_days', [ 'label' => esc_html__( 'Expiry Days', 'bdthemes-element-pack' ), 'description' => 'Specify -1 for no expiry', 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 7, ], 'range' => [ 'px' => [ 'min' => -1, 'max' => 365, ], ], 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'custom_attributes', [ 'label' => esc_html__( 'Custom Attributes', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::TEXTAREA, 'default' => 'rel|noopener noreferrer nofollow', 'dynamic' => [ 'active' => true, ], 'placeholder' => esc_html__( 'key|value', 'bdthemes-element-pack' ), 'description' => esc_html__( 'Set custom attributes for the link tag. Each attribute in a separate line.', 'bdthemes-element-pack' ), 'classes' => 'elementor-control-direction-ltr', ] ); $this->add_control( 'google_tag_assistant', [ 'label' => esc_html__('Google Consent Mode', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'separator' => 'before', 'description' => esc_html__('Google Consent Mode for cookie consent. Must make sure, you have installed GTAG on your website. If you disabled any features then they will be sent as denied', 'bdthemes-element-pack' ) . ' <a href="https://developers.google.com/tag-platform/security/guides/consent?sjid=14345995958166516665-AP&consentmode=advanced" target="_blank">' . esc_html__( 'Learn More', 'bdthemes-element-pack' ) . '</a>', ] ); $this->add_control( 'google_tag_ad_user_data', [ 'label' => esc_html__('Ad User Data', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'google_tag_assistant' => 'yes', ], 'default' => 'yes', ] ); $this->add_control( 'google_tag_ad_storage', [ 'label' => esc_html__('Ad Storage', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'google_tag_assistant' => 'yes', ], 'default' => 'yes', ] ); $this->add_control( 'google_tag_ad_personalization', [ 'label' => esc_html__('Ad Personalization', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'google_tag_assistant' => 'yes', ], 'default' => 'yes', ] ); $this->add_control( 'google_tag_analytics_storage', [ 'label' => esc_html__('Analytics Storage', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'google_tag_assistant' => 'yes', ], 'default' => 'yes', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style', [ 'label' => esc_html__( 'Style', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'background', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#3937a3', 'selectors' => [ 'body .cc-window' => 'background-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#ffffff', 'selectors' => [ 'body .cc-window' => 'color: {{VALUE}} !important;', ], ] ); $this->add_control( 'learn_more_color', [ 'label' => esc_html__( 'Learn More Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#4593E3', 'selectors' => [ 'body .cc-window .cc-link' => 'color: {{VALUE}} !important;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'subtitle_typography', 'selector' => 'body .cc-window *', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_dismiss_button', [ 'label' => esc_html__( 'Button', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs( 'tabs_dismiss_button_style' ); $this->start_controls_tab( 'tab_dismiss_button_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'dismiss_button_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#ffffff', 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss' => 'color: {{VALUE}} !important;', ], ] ); $this->add_control( 'dismiss_button_background', [ 'label' => esc_html__( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#41aab9', 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss' => 'background-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'dismiss_button_border_style', [ 'label' => esc_html__( 'Border Style', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'none', 'options' => [ 'none' => esc_html__( 'None', 'bdthemes-element-pack' ), 'solid' => esc_html__( 'Solid', 'bdthemes-element-pack' ), 'double' => esc_html__( 'Double', 'bdthemes-element-pack' ), 'dotted' => esc_html__( 'Dotted', 'bdthemes-element-pack' ), 'dashed' => esc_html__( 'Dashed', 'bdthemes-element-pack' ), 'groove' => esc_html__( 'Groove', 'bdthemes-element-pack' ), ], 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss' => 'border-style: {{VALUE}} !important;', ], ] ); $this->add_control( 'dismiss_button_border_width', [ 'label' => esc_html__( 'Border Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'min' => 0, 'max' => 20, 'size' => 1, ], 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss' => 'border-width: {{SIZE}}{{UNIT}} !important;', ], ] ); $this->add_control( 'dismiss_button_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#ccc', 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss' => 'border-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'dismiss_button_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;', ], ] ); $this->add_responsive_control( 'dismiss_button_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;', ], ] ); $this->add_responsive_control( 'dismiss_button_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'dismiss_button_typography', 'label' => esc_html__( 'Typography', 'bdthemes-element-pack' ), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => 'body .cc-window .cc-btn.cc-dismiss', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_dismiss_button_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'dismiss_button_hover_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss:hover' => 'color: {{VALUE}} !important;', ], ] ); $this->add_control( 'dismiss_button_hover_background', [ 'label' => esc_html__( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss:hover' => 'background-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'dismiss_button_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'dismiss_button_border_style!' => 'none', ], 'selectors' => [ 'body .cc-window .cc-btn.cc-dismiss:hover' => 'border-color: {{VALUE}} !important;', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); $cc_position = $settings['position']; if ( $cc_position == 'bottom-left' ) { $cc_position = 'cc-bottom cc-left cc-floating'; } else if ( $cc_position == 'bottom-right' ) { $cc_position = 'cc-bottom cc-right cc-floating'; } else if ( $cc_position == 'top' ) { $cc_position = 'cc-top cc-banner'; } else if ( $cc_position == 'bottom' ) { $cc_position = 'cc-bottom cc-banner'; } $this->add_render_attribute( 'cookie-consent', 'class', [ 'bdt-cookie-consent', 'bdt-hidden' ] ); if ( ! empty( $settings['custom_attributes'] ) ) { $attributes = explode( "\n", $settings['custom_attributes'] ); $reserved_attr = [ 'role', 'href' ]; foreach ( $attributes as $attribute ) { if ( ! empty( $attribute ) ) { $attr = explode( '|', $attribute, 2 ); if ( ! isset( $attr[1] ) ) { $attr[1] = ''; } if ( ! in_array( strtolower( $attr[0] ), $reserved_attr ) ) { $this->add_render_attribute( 'custom-attr', trim( $attr[0] ), trim( $attr[1] ) ); } } } } $this->add_render_attribute( [ 'cookie-consent' => [ 'data-settings' => [ wp_json_encode( [ 'position' => $settings['position'], 'static' => ( 'top' == $settings['position'] and $settings['pushdown'] ) ? true : false, 'content' => [ 'message' => $settings['message'], 'dismiss' => $settings['button_text'], 'link' => $settings['learn_more_text'], 'href' => esc_url( $settings['learn_more_link']['url'] ), 'custom_attr' => $this->get_render_attribute_string( 'custom-attr' ), ], 'cookie' => [ 'name' => 'element_pack_cookie_widget', 'domain' => Utils::get_site_domain(), 'expiryDays' => $settings['expiry_days']['size'], ], ] ), ], ], ] ); if ( 'yes' == $settings['google_tag_assistant'] ) { $this->add_render_attribute( [ 'cookie-consent' => [ 'data-gtag' => [ wp_json_encode( [ 'gtag_enabled' => true, 'ad_user_data' => ('yes' == $settings['google_tag_ad_user_data']) ? 'granted' : 'denied', 'ad_storage' => ('yes' == $settings['google_tag_ad_storage']) ? 'granted' : 'denied', 'ad_personalization' => ('yes' == $settings['google_tag_ad_personalization']) ? 'granted' : 'denied', 'analytics_storage' => ('yes' == $settings['google_tag_analytics_storage']) ? 'granted' : 'denied', ] ), ], ], ] ); } if ( Element_Pack_Loader::elementor()->editor->is_edit_mode() ) : ?> <div role="dialog" aria-live="polite" aria-label="cookieconsent" aria-describedby="cookieconsent:desc" class="cc-window <?php echo esc_attr( $cc_position ); ?> cc-type-info cc-theme-block cc-color-override--2000495483"> <!--googleoff: all--> <span id="cookieconsent:desc" class="cc-message"> <?php echo esc_html( $settings['message'] ); ?><a aria-label="learn more about cookies" role="button" tabindex="0" class="cc-link" href="<?php echo esc_url( $settings['learn_more_link']['url'] ); ?>" rel="noopener noreferrer nofollow" target="_blank" test> <?php echo esc_html( $settings['learn_more_text'] ); ?> </a> </span> <div class="cc-compliance"> <a aria-label="dismiss cookie message" role="button" tabindex="0" class="cc-btn cc-dismiss"> <?php echo esc_html( $settings['button_text'] ); ?> </a> </div> <!--googleon: all--> </div> <?php else : ?> <div <?php $this->print_render_attribute_string( 'cookie-consent' ); ?>></div> <?php endif; } } <?php namespace ElementPack\Modules\CookieConsent; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'cookie-consent'; } public function get_widgets() { $widgets = [ 'Cookie_Consent', ]; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Cookie Consent', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\Stacker\Widgets; use Elementor\Repeater; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Stacker extends Module_Base { public function get_name() { return 'bdt-stacker'; } public function get_title() { return BDTEP . esc_html__('Stacker', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-stacker'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['stack', 'stacker', 'scroll', 'scroller']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-stacker']; } } public function get_script_depends() { if ($this->ep_is_edit_mode()) { return ['gsap', 'scroll-trigger-js', 'ep-scripts']; } else { return ['gsap', 'scroll-trigger-js', 'ep-stacker']; } } public function get_custom_help_url() { return 'https://youtu.be/fZSTyJc5W7E?si=GkkUhdv9aXPTlVxS'; } protected function register_controls() { $this->start_controls_section( 'section_title', [ 'label' => __('Stacker', 'bdthemes-element-pack'), ] ); $repeater = new Repeater(); $repeater->add_control( 'stacker_section_id', [ 'label' => esc_html__('ID', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'description' => esc_html__( "Just write that's section or Container ID here such 'my-section'. N.B: No need to add '#'.", 'bdthemes-element-pack' ), ] ); $this->add_control( 'stacker_section_list', [ 'label' => __('Section or Container IDs', 'bdthemes-element-pack'), 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(), 'title_field' => '{{{ stacker_section_id }}}', 'frontend_available' => true, 'render_type' => 'none', 'prevent_empty' => false, ] ); $this->add_control( 'stacker_scroller_start', [ 'label' => esc_html__('Scroller Start', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['%'], 'separator' => 'before', 'frontend_available' => true, 'render_type' => 'none', 'default' => [ 'unit' => '%', 'size' => 10, ], ] ); $this->add_control( 'stacker_stacking_space', [ 'label' => esc_html__('Staking Space', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px'], 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'default' => [ 'unit' => 'px', 'size' => 10, ], 'frontend_available' => true, 'render_type' => 'none', ] ); $this->add_control( 'stacker_spacing', [ 'label' => esc_html__('Bottom Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px'], 'range' => [ 'px' => [ 'min' => 0, 'max' => 500, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-stacker .elementor-top-section, {{WRAPPER}} .bdt-ep-stacker .e-con' => 'margin-bottom: {{SIZE}}{{UNIT}}', ], ] ); $this-> add_control( 'stacker_stacking_opacity', [ 'label' => __( 'Transparent on Scroll', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'label_on' => __( 'Yes', 'bdthemes-element-pack' ), 'label_off' => __( 'No', 'bdthemes-element-pack' ), 'separator' => 'before', 'return_value' => 'yes', 'default' => 'no', 'frontend_available' => true, 'render_type' => 'none', ] ); $this->add_control( 'ignore_element_notes', [ 'type' => Controls_Manager::RAW_HTML, 'raw' => esc_html__('Note: This widget won\'t function at the editor mode at all. It will work just fine on frontend perspective.', 'bdthemes-element-pack'), 'content_classes' => 'elementor-panel-alert elementor-panel-alert-info', 'separator' => 'before', ] ); $this->end_controls_section(); } public function render(){?> <div class="bdt-ep-stacker"></div> <?php } } <?php namespace ElementPack\Modules\Stacker; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'stacker'; } public function get_widgets() { $widgets = [ 'Stacker', ]; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Stacker', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\Member\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Image_Size; use Elementor\Repeater; use Elementor\Icons_Manager; use ElementPack\Modules\Member\Skins; use ElementPack\Traits\Global_Mask_Controls; if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } class Member extends Module_Base { use Global_Mask_Controls; public function get_name() { return 'bdt-member'; } public function get_title() { return BDTEP . esc_html__( 'Member', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-member'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'member', 'team', 'experts' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'elementor-icons-fa-solid', 'elementor-icons-fa-brands', 'ep-styles' ]; } else { return [ 'elementor-icons-fa-solid', 'elementor-icons-fa-brands', 'ep-member' ]; } } public function get_custom_help_url() { return 'https://youtu.be/m8_KOHzssPA'; } protected function register_skins() { $this->add_skin( new Skins\Skin_Band( $this ) ); $this->add_skin( new Skins\Skin_Calm( $this ) ); $this->add_skin( new Skins\Skin_Ekip( $this ) ); $this->add_skin( new Skins\Skin_Phaedra( $this ) ); $this->add_skin( new Skins\Skin_Partait( $this ) ); $this->add_skin( new Skins\Skin_Flip( $this ) ); } protected function register_controls() { $this->start_controls_section( 'section_content_layout', [ 'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ), ] ); $this->add_control( 'photo', [ 'label' => esc_html__( 'Choose Photo', 'bdthemes-element-pack' ), 'type' => Controls_Manager::MEDIA, 'dynamic' => [ 'active' => true ], 'default' => [ 'url' => BDTEP_ASSETS_URL . 'images/member.svg', ], ] ); $this->add_control( 'member_alternative_photo', [ 'label' => esc_html__( 'Alternative Photo', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'alternative_photo', [ 'label' => esc_html__( 'Choose Photo', 'bdthemes-element-pack' ), 'type' => Controls_Manager::MEDIA, 'dynamic' => [ 'active' => true ], 'default' => [ 'url' => BDTEP_ASSETS_URL . 'images/member.svg', ], 'condition' => [ 'member_alternative_photo' => 'yes', ], ] ); $this->add_control( 'image_mask_popover', [ 'label' => esc_html__( 'Image Mask', 'bdthemes-element-pack' ), 'type' => Controls_Manager::POPOVER_TOGGLE, 'render_type' => 'template', 'return_value' => 'yes', ] ); //Global Image Mask Controls $this->register_image_mask_controls(); $this->add_control( 'name', [ 'label' => esc_html__( 'Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'John Doe', 'bdthemes-element-pack' ), 'placeholder' => esc_html__( 'Member Name', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'role', [ 'label' => esc_html__( 'Role', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Managing Director', 'bdthemes-element-pack' ), 'placeholder' => esc_html__( 'Member Role', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'description_text', [ 'label' => esc_html__( 'Description', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXTAREA, 'default' => esc_html__( 'Type here some info about this team member, the man very important person of our company.', 'bdthemes-element-pack' ), 'placeholder' => esc_html__( 'Member Description', 'bdthemes-element-pack' ), 'rows' => 10, 'condition' => [ '_skin' => [ '', 'bdt-partait', 'bdt-band', 'bdt-flip' ] ], 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'member_social_icon', [ 'label' => esc_html__( 'Social Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_responsive_control( 'skin_partait_align', [ 'label' => esc_html__( 'Alignment', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => esc_html__( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'selectors' => [ '{{WRAPPER}} .skin-partait .bdt-member-desc-wrapper' => 'text-align: {{VALUE}} !important;', ], 'condition' => [ '_skin' => 'bdt-partait' ] ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_social_link', [ 'label' => esc_html__( 'Social Icon', 'bdthemes-element-pack' ), 'condition' => [ 'member_social_icon' => 'yes' ], ] ); $repeater = new Repeater(); $repeater->add_control( 'social_link_title', [ 'label' => esc_html__( 'Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => 'Facebook', ] ); $repeater->add_control( 'social_link', [ 'label' => esc_html__( 'Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => 'http://www.facebook.com/bdthemes/', ] ); $repeater->add_control( 'social_share_icon', [ 'label' => esc_html__( 'Choose Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::ICONS, 'fa4compatibility' => 'social_icon', ] ); $repeater->add_control( 'icon_background', [ 'label' => esc_html__( 'Icon Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icons {{CURRENT_ITEM}}' => 'background-color: {{VALUE}}', ], 'condition' => [ '_skin!' => 'bdt-band', ], ] ); $repeater->add_control( 'icon_color', [ 'label' => esc_html__( 'Icon Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icons {{CURRENT_ITEM}}' => 'color: {{VALUE}}', '{{WRAPPER}} .bdt-member .bdt-member-icons {{CURRENT_ITEM}} svg' => 'fill: {{VALUE}}', ], 'condition' => [ '_skin!' => 'bdt-band', ], ] ); $this->add_control( 'social_link_list', [ 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(), 'default' => [ [ 'social_link' => 'http://www.facebook.com/bdthemes/', 'social_share_icon' => [ 'value' => 'fab fa-facebook-f', 'library' => 'fa-brands' ], 'social_link_title' => 'Facebook', ], [ 'social_link' => 'http://www.twitter.com/bdthemes/', 'social_share_icon' => [ 'value' => 'fab fa-twitter', 'library' => 'fa-brands' ], 'social_link_title' => 'Twitter', ], [ 'social_link' => 'http://www.linkedin.com/bdthemes/', 'social_share_icon' => [ 'value' => 'fab fa-linkedin-in', 'library' => 'fa-brands' ], 'social_link_title' => 'Linkedin', ], ], 'title_field' => '{{{ social_link_title }}}', ] ); $this->end_controls_section(); //style $this->start_controls_section( 'section_style', [ 'label' => esc_html__( 'Member', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => [ '', 'bdt-band' ], ], ] ); $this->add_control( 'band_item_background_color', [ 'label' => __( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .skin-band .bdt-member-item-wrapper' => 'background: {{VALUE)}};', ], 'condition' => [ '_skin' => [ 'bdt-band' ], ], ] ); $this->add_control( 'band_overlay_color', [ 'label' => __( 'Overlay Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .skin-band .bdt-member-photo:before' => 'background: {{VALUE)}};', ], 'condition' => [ '_skin' => [ 'bdt-band' ], ], ] ); $this->add_responsive_control( 'text_align', [ 'label' => esc_html__( 'Text Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => esc_html__( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-member' => 'text-align: {{VALUE}};', ], ] ); $this->add_control( 'desc_padding', [ 'label' => esc_html__( 'Description Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_flip_style', [ 'label' => __( 'Flip Style', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => 'bdt-flip', ], ] ); $this->add_control( 'back_background_color', [ 'label' => __( 'Back Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-skin-flip-back' => 'background-color: {{VALUE}};', ], 'condition' => [ 'member_alternative_photo' => '', ], ] ); $this->add_responsive_control( 'skin_flip_height', [ 'label' => __( 'Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 100, 'max' => 1000, ], 'vh' => [ 'min' => 10, 'max' => 100, ], ], 'size_units' => [ 'px', 'vh' ], 'selectors' => [ '{{WRAPPER}} .skin-flip' => 'height: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'flip_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px', '%' ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 200, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-skin-flip-layer, {{WRAPPER}} .bdt-skin-flip-layer-overlay' => 'border-radius: {{SIZE}}{{UNIT}}', ], ] ); $this->add_responsive_control( 'flip_desc_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-skin-flip-layer-overlay' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'flip_text_align', [ 'label' => esc_html__( 'Text Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => esc_html__( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'selectors' => [ '{{WRAPPER}} .skin-flip .bdt-member-content' => 'text-align: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Image_Size::get_type(), [ 'name' => 'thumbnail_size', 'label' => esc_html__( 'Image Size', 'bdthemes-element-pack' ), 'exclude' => [ 'custom' ], 'default' => 'full', 'prefix_class' => 'bdt-member--thumbnail-size-', ] ); $this->add_control( 'flip_effect', [ 'label' => __( 'Flip Effect', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'flip', 'options' => [ 'flip' => __( 'Flip', 'bdthemes-element-pack' ), 'slide' => __( 'Slide', 'bdthemes-element-pack' ), 'push' => __( 'Push', 'bdthemes-element-pack' ), ], 'prefix_class' => 'bdt-skin-flip-effect-', ] ); $this->add_control( 'flip_direction', [ 'label' => __( 'Flip Direction', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'left', 'options' => [ 'left' => __( 'Left', 'bdthemes-element-pack' ), 'right' => __( 'Right', 'bdthemes-element-pack' ), 'up' => __( 'Up', 'bdthemes-element-pack' ), 'down' => __( 'Down', 'bdthemes-element-pack' ), ], 'prefix_class' => 'bdt-skin-flip-direction-', ] ); $this->add_control( 'flip_3d', [ 'label' => __( '3D Depth', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'prefix_class' => 'bdt-skin-flip-3d-', 'condition' => [ 'flip_effect' => 'flip', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_photo', [ 'label' => esc_html__( 'Photo', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin!' => 'bdt-flip', ], ] ); $this->start_controls_tabs( 'tabs_photo_style' ); $this->start_controls_tab( 'tab_photo_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'photo_background', [ 'label' => esc_html__( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-photo' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'photo_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-member .bdt-member-photo', 'separator' => 'before', ] ); $this->add_control( 'photo_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-photo' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_control( 'photo_opacity', [ 'label' => esc_html__( 'Opacity (%)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-photo img' => 'opacity: {{SIZE}};', ], ] ); $this->add_control( 'photo_spacing', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-photo' => 'margin-bottom: {{SIZE}}{{UNIT}}', ], 'condition' => [ '_skin!' => [ 'bdt-band' ], ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_photo_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'photo_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-photo:hover' => 'border-color: {{VALUE}};', ], ] ); $this->add_control( 'photo_hover_opacity', [ 'label' => esc_html__( 'Opacity (%)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-photo:hover img' => 'opacity: {{SIZE}};', ], ] ); $this->add_control( 'photo_hover_animation', [ 'label' => esc_html__( 'Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => '', 'options' => [ '' => 'None', 'up' => 'Scale Up', 'down' => 'Scale Down', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_name', [ 'label' => esc_html__( 'Name', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'name_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-name' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'name_typography', 'selector' => '{{WRAPPER}} .bdt-member .bdt-member-name', ] ); $this->add_responsive_control( 'name_bottom_space', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-member-name' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], 'condition' => [ '_skin!' => 'bdt-ekip', ], ] ); $this->add_responsive_control( 'ekip_name_bottom_space', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => '%', ], 'size_units' => [ '%' ], 'range' => [ '%' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .skin-ekip:hover .bdt-member-name' => 'top: {{SIZE}}{{UNIT}};', ], 'condition' => [ '_skin' => 'bdt-ekip', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_role', [ 'label' => esc_html__( 'Role', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'role_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-role' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'role_bottom_space', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-role' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], 'condition' => [ '_skin!' => 'bdt-ekip', ], ] ); $this->add_responsive_control( 'ekip_role_bottom_space', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => '%', ], 'size_units' => [ '%' ], 'range' => [ '%' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .skin-ekip:hover .bdt-member-role' => 'top: {{SIZE}}{{UNIT}};', ], 'condition' => [ '_skin' => 'bdt-ekip', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'role_typography', 'selector' => '{{WRAPPER}} .bdt-member .bdt-member-role', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_text', [ 'label' => esc_html__( 'Text', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => [ '', 'bdt-band', 'bdt-flip', 'bdt-partait' ], ], ] ); $this->add_control( 'text_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-text' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'text_typography', 'selector' => '{{WRAPPER}} .bdt-member .bdt-member-text', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_social_icon', [ 'label' => esc_html__( 'Social Icon', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'member_social_icon' => 'yes' ], ] ); $this->add_control( 'icon_content_background', [ 'label' => esc_html__( 'Icons Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icons' => 'background-color: {{VALUE}}', ], 'condition' => [ '_skin!' => 'bdt-band', ], ] ); $this->add_responsive_control( 'social_icon_content_padding', [ 'label' => esc_html__( 'Icons Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icons' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ '_skin!' => 'bdt-band', ], ] ); $this->start_controls_tabs( 'tabs_social_icon_style' ); $this->start_controls_tab( 'tab_social_icon_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'icon_background', [ 'label' => esc_html__( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icon' => 'background-color: {{VALUE}}', ], 'condition' => [ '_skin!' => 'bdt-band', ], ] ); $this->add_control( 'band_icon_background', [ 'label' => esc_html__( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .skin-band .bdt-member-icons .bdt-member-icon:before' => 'background: {{VALUE}}', ], 'condition' => [ '_skin' => 'bdt-band', ], ] ); $this->add_control( 'icon_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icon i' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-member .bdt-member-icon svg' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'social_icons_top_border_color', [ 'label' => esc_html__( 'Top Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icons' => 'border-top-color: {{VALUE}}', ], 'condition' => [ '_skin!' => 'bdt-band', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'social_icon_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-member .bdt-member-icon', 'condition' => [ '_skin!' => 'bdt-band', ], ] ); $this->add_control( 'social_icon_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icon' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ '_skin!' => 'bdt-band', ], ] ); $this->add_responsive_control( 'social_icon_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icon' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ '_skin!' => 'bdt-band', ], ] ); $this->add_responsive_control( 'social_icon_size', [ 'label' => esc_html__( 'Icon Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icon i' => 'min-width: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-member .bdt-member-icon i:before' => 'font-size: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-member .bdt-member-icon svg' => 'font-size: {{SIZE}}{{UNIT}};', ], // 'condition' => [ // '_skin!' => 'bdt-band', // ], ] ); //icon box size $this->add_responsive_control( 'social_icon_box_size', [ 'label' => esc_html__( 'Icon Background Size', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .skin-band .bdt-member-icon:before, {{WRAPPER}} .skin-band .bdt-member-icon' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};', ], 'condition' => [ '_skin' => 'bdt-band', ], ] ); $this->add_responsive_control( 'social_icon_indent', [ 'label' => esc_html__( 'Icon Space Between', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icon + .bdt-member-icon' => 'margin-left: {{SIZE}}{{UNIT}};', ], ] ); //icon vertical spacing $this->add_responsive_control( 'social_icon_vertical_space', [ 'label' => esc_html__( 'Vertical Spacing', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .skin-band .bdt-member-icons' => 'bottom: {{SIZE}}{{UNIT}};', ], 'condition' => [ '_skin' => 'bdt-band', ], ] ); $this->add_responsive_control( 'ekip_icon_vertical_space', [ 'label' => esc_html__( 'Vertical Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => '%', ], 'size_units' => [ '%' ], 'range' => [ '%' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .skin-ekip:hover .bdt-member-icons' => 'top: {{SIZE}}{{UNIT}};', ], 'condition' => [ '_skin' => 'bdt-ekip', ], ] ); $this->add_control( 'social_icon_tooltip', [ 'label' => esc_html__( 'Tooltip', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_social_icon_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'icon_hover_background', [ 'label' => esc_html__( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icon:hover' => 'background-color: {{VALUE}}', ], 'condition' => [ '_skin!' => 'bdt-band', ], ] ); $this->add_control( 'icon_hover_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icon:hover i' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-member .bdt-member-icon:hover svg' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'icon_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'social_icon_border_border!' => '', '_skin!' => 'bdt-band', ], 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-icon:hover' => 'border-color: {{VALUE}}', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); if ( ! isset( $settings['social_icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $settings['social_icon'] = 'fab fa-facebook-f'; } $image_mask = $settings['image_mask_popover'] == 'yes' ? ' bdt-image-mask' : ''; $this->add_render_attribute( 'image-wrap', 'class', 'bdt-member-photo-wrapper' . $image_mask ); ?> <div class="bdt-member skin-default bdt-transition-toggle"> <?php if ( ! empty( $settings['photo']['url'] ) ) : $photo_hover_animation = ( '' != $settings['photo_hover_animation'] ) ? ' bdt-transition-scale-' . $settings['photo_hover_animation'] : ''; ?> <div <?php $this->print_render_attribute_string( 'image-wrap' ); ?>> <?php if ( ( $settings['member_alternative_photo'] ) and ( ! empty( $settings['alternative_photo']['url'] ) ) ) : ?> <div class="bdt-position-relative bdt-overflow-hidden bdt-position-z-index" data-bdt-toggle="target: > .bdt-member-photo-flip; mode: hover; animation: bdt-animation-fade; queued: true; duration: 300;"> <div class="bdt-member-photo-flip bdt-position-absolute bdt-position-z-index"> <?php $thumb_url = Group_Control_Image_Size::get_attachment_image_src( $settings['alternative_photo']['id'], 'thumbnail_size', $settings ); if ( ! $thumb_url ) { printf( '<img src="%1$s" alt="%2$s">', esc_url( $settings['alternative_photo']['url'] ), esc_html( $settings['name'] ) ); } else { print( wp_get_attachment_image( $settings['alternative_photo']['id'], $settings['thumbnail_size_size'], false, [ 'alt' => esc_html( $settings['name'] ) ] ) ); } ?> </div> <?php endif; ?> <div class="bdt-member-photo"> <div class="<?php echo esc_attr( $photo_hover_animation ); ?>"> <?php $thumb_url = Group_Control_Image_Size::get_attachment_image_src( $settings['photo']['id'], 'thumbnail_size', $settings ); if ( ! $thumb_url ) { printf( '<img src="%1$s" alt="%2$s">', esc_url( $settings['photo']['url'] ), esc_html( $settings['name'] ) ); } else { print( wp_get_attachment_image( $settings['photo']['id'], $settings['thumbnail_size_size'], false, [ 'alt' => esc_html( $settings['name'] ) ] ) ); } ?> </div> </div> <?php if ( ( $settings['member_alternative_photo'] ) and ( ! empty( $settings['alternative_photo']['url'] ) ) ) : ?> </div> <?php endif; ?> </div> <?php endif; ?> <div class="bdt-member-content"> <?php if ( ! empty( $settings['name'] ) ) : ?> <span class="bdt-member-name"> <?php echo wp_kses( $settings['name'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> <?php if ( ! empty( $settings['role'] ) ) : ?> <span class="bdt-member-role"> <?php echo wp_kses( $settings['role'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> <?php if ( ! empty( $settings['description_text'] ) ) : ?> <div class="bdt-member-text bdt-content-wrap"> <?php echo wp_kses( $settings['description_text'], element_pack_allow_tags( 'text' ) ); ?> </div> <?php endif; ?> </div> <?php if ( 'yes' == $settings['member_social_icon'] ) : ?> <div class="bdt-member-icons"> <?php foreach ( $settings['social_link_list'] as $link ) : $tooltip = ( 'yes' == $settings['social_icon_tooltip'] ) ? ' data-bdt-tooltip="' . $link['social_link_title'] . '"' : ''; ?> <?php $migrated = isset( $link['__fa4_migrated']['social_share_icon'] ); $is_new = empty( $link['social_icon'] ) && Icons_Manager::is_migration_allowed(); ?> <a href="<?php echo esc_url( $link['social_link'] ); ?>" class="bdt-member-icon elementor-repeater-item-<?php echo esc_attr( $link['_id'] ); ?>" target="_blank" <?php echo wp_kses_post( $tooltip ); ?>> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $link['social_share_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $link['social_icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </a> <?php endforeach; ?> </div> <?php endif; ?> </div> <?php } } <?php namespace ElementPack\Modules\Member; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'member'; } public function get_widgets() { $widgets = [ 'Member', ]; return $widgets; } } <?php namespace ElementPack\Modules\Member\Skins; use Elementor\Controls_Manager; use Elementor\Group_Control_Image_Size; use Elementor\Icons_Manager; use Elementor\Skin_Base as Elementor_Skin_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Flip extends Elementor_Skin_Base { public function get_id() { return 'bdt-flip'; } public function get_title() { return __( 'Flip', 'bdthemes-element-pack' ); } public function render() { $calm_id = 'flip' . $this->parent->get_id(); $settings = $this->parent->get_settings_for_display(); $alternative_image = ''; $image_mask = $settings['image_mask_popover'] == 'yes' ? ' bdt-image-mask' : ''; $this->parent->add_render_attribute( 'skin-flip', 'class', 'bdt-member skin-flip bdt-transition-toggle bdt-inline' . $image_mask ); if ( ! isset( $settings['social_icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $settings['social_icon'] = 'fab fa-facebook-f'; } $member_image = Group_Control_Image_Size::get_attachment_image_src( $settings['photo']['id'], 'thumbnail_size', $settings); if ( ! $member_image ) { $member_image = $settings['photo']['url']; } if ( 'yes' == $settings['member_alternative_photo'] ) { $alternative_image = Group_Control_Image_Size::get_attachment_image_src( $settings['alternative_photo']['id'], 'thumbnail_size', $settings); if ( ! $alternative_image ) { $alternative_image = $settings['alternative_photo']['url']; } } ?> <div <?php $this->parent->print_render_attribute_string( 'skin-flip' ); ?>> <div class="bdt-skin-flip-layer bdt-skin-flip-front" style="background-image: url('<?php echo esc_url($member_image); ?>');"> <div class="bdt-skin-flip-layer-overlay"> <div class="bdt-skin-flip-layer-inner"> <div class="bdt-member-content bdt-position-bottom-center"> <?php if ( ! empty( $settings['name'] ) ) : ?> <span class="bdt-member-name"><?php echo wp_kses( $settings['name'], element_pack_allow_tags('title') ); ?></span> <?php endif; ?> <?php if ( ! empty( $settings['role'] ) ) : ?> <span class="bdt-member-role"><?php echo wp_kses( $settings['role'], element_pack_allow_tags('title') ); ?></span> <?php endif; ?> </div> </div> </div> </div> <div class="bdt-skin-flip-layer bdt-skin-flip-back" style="background-image: url('<?php echo esc_url($alternative_image); ?>');"> <div class="bdt-skin-flip-layer-overlay"> <div class="bdt-skin-flip-layer-inner"> <?php if ( 'yes' == $settings['member_social_icon'] ) : ?> <div class="bdt-member-icons bdt-position-bottom-center"> <?php foreach ( $settings['social_link_list'] as $link ) : $tooltip = ( 'yes' == $settings['social_icon_tooltip'] ) ? ' title="'.esc_attr( $link['social_link_title'] ).'" data-bdt-tooltip' : ''; ?> <?php $migrated = isset( $link['__fa4_migrated']['social_share_icon'] ); $is_new = empty( $link['social_icon'] ) && Icons_Manager::is_migration_allowed(); ?> <a href="<?php echo esc_url( $link['social_link'] ); ?>" class="bdt-member-icon elementor-repeater-item-<?php echo esc_attr($link['_id']); ?>" target="_blank"<?php echo wp_kses_post($tooltip); ?>> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $link['social_share_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $link['social_icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </a> <?php endforeach; ?> </div> <?php endif; ?> <?php if ( ! empty( $settings['description_text'] ) ) : ?> <div class="bdt-member-text bdt-position-center"><?php echo wp_kses( $settings['description_text'], element_pack_allow_tags('text') ); ?></div> <?php endif; ?> </div> </div> </div> </div> <?php } } <?php namespace ElementPack\Modules\Member\Skins; use Elementor\Controls_Manager; use Elementor\Group_Control_Image_Size; use Elementor\Group_Control_Background; use Elementor\Icons_Manager; use ElementPack\Base\Module_Base; use Elementor\Skin_Base as Elementor_Skin_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Ekip extends Elementor_Skin_Base { protected function _register_controls_actions() { parent::_register_controls_actions(); add_action( 'elementor/element/bdt-member/section_style/before_section_start', [ $this, 'register_ekip_style_controls' ] ); } public function get_id() { return 'bdt-ekip'; } public function get_title() { return __( 'Ekip', 'bdthemes-element-pack' ); } public function register_ekip_style_controls( Module_Base $widget ) { $this->parent = $widget; $this->start_controls_section( 'section_style_phaedra', [ 'label' => __( 'Ekip', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'ekip_overlay_background_color', 'label' => __( 'Background', 'bdthemes-element-pack' ), 'types' => [ 'gradient' ], 'selector' => '{{WRAPPER}} .bdt-member.skin-ekip .ekip-overlay', ] ); $this->add_control( 'ekip_overlay_line_color', [ 'label' => __( 'Overlay Line Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member.skin-ekip .ekip-overlay' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_section(); } public function render() { $ekip_id = 'ekip' . $this->parent->get_id(); $settings = $this->parent->get_settings_for_display(); $image_mask = $settings['image_mask_popover'] == 'yes' ? ' bdt-image-mask' : ''; $this->parent->add_render_attribute( 'skin-ekip', 'class', 'bdt-member skin-ekip bdt-transition-toggle' . $image_mask ); if ( ( $settings['member_alternative_photo'] ) and ( ! empty ( $settings['alternative_photo']['url'] ) ) ) { $this->parent->add_render_attribute( 'skin-ekip', 'class', [ 'bdt-position-relative', 'bdt-overflow-hidden', 'bdt-transition-toggle' ] ); $this->parent->add_render_attribute( 'skin-ekip', 'bdt-toggle', 'target: > div > .bdt-member-photo-flip; mode: hover; animation: bdt-animation-fade; queued: true; duration: 300;' ); } if ( ! isset ( $settings['social_icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $settings['social_icon'] = 'fab fa-facebook-f'; } ?> <div <?php $this->parent->print_render_attribute_string( 'skin-ekip' ); ?>> <?php if ( ! empty ( $settings['photo']['url'] ) ) : $photo_hover_animation = ( '' != $settings['photo_hover_animation'] ) ? ' bdt-transition-scale-' . $settings['photo_hover_animation'] : ''; ?> <div class="bdt-member-photo-wrapper"> <?php if ( ( $settings['member_alternative_photo'] ) and ( ! empty ( $settings['alternative_photo']['url'] ) ) ) : ?> <div class="bdt-member-photo-flip bdt-position-absolute bdt-position-z-index"> <?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'alternative_photo' ) ); ?> </div> <?php endif; ?> <div class="bdt-member-photo"> <div class="<?php echo esc_attr( $photo_hover_animation ); ?>"> <?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'photo' ) ); ?> </div> </div> </div> <?php endif; ?> <div class="ekip-overlay bdt-position-z-index"> <div class="bdt-member-desc"> <div class="bdt-member-content"> <?php if ( ! empty ( $settings['role'] ) ) : ?> <span class="bdt-member-role"> <?php echo wp_kses( $settings['role'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> <?php if ( ! empty ( $settings['name'] ) ) : ?> <span class="bdt-member-name"> <?php echo wp_kses( $settings['name'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> </div> <?php if ( 'yes' == $settings['member_social_icon'] ) : ?> <div class="bdt-member-icons"> <?php foreach ( $settings['social_link_list'] as $link ) : $tooltip = ( 'yes' == $settings['social_icon_tooltip'] ) ? ' title="' . $link['social_link_title'] . '" data-bdt-tooltip' : ''; ?> <?php $migrated = isset ( $link['__fa4_migrated']['social_share_icon'] ); $is_new = empty ( $link['social_icon'] ) && Icons_Manager::is_migration_allowed(); ?> <a href="<?php echo esc_url( $link['social_link'] ); ?>" class="bdt-member-icon elementor-repeater-item-<?php echo esc_attr( $link['_id'] ); ?>" target="_blank" <?php echo wp_kses_post( $tooltip ); ?>> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $link['social_share_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $link['social_icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </a> <?php endforeach; ?> </div> <?php endif; ?> </div> </div> </div> <?php } } <?php namespace ElementPack\Modules\Member\Skins; use Elementor\Group_Control_Image_Size; use Elementor\Icons_Manager; use Elementor\Skin_Base as Elementor_Skin_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Band extends Elementor_Skin_Base { public function get_id() { return 'bdt-band'; } public function get_title() { return __( 'Band', 'bdthemes-element-pack' ); } public function render() { $settings = $this->parent->get_settings_for_display(); if ( ! isset ( $settings['social_icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $settings['social_icon'] = 'fab fa-facebook-f'; } $image_mask = $settings['image_mask_popover'] == 'yes' ? ' bdt-image-mask' : ''; $this->parent->add_render_attribute( 'image-wrap', 'class', 'bdt-member-photo-wrapper' . $image_mask ); ?> <div class="bdt-member skin-band bdt-transition-toggle"> <div class="bdt-member-item-wrapper"> <?php if ( ! empty ( $settings['photo']['url'] ) ) : $photo_hover_animation = ( '' != $settings['photo_hover_animation'] ) ? ' bdt-transition-scale-' . $settings['photo_hover_animation'] : ''; ?> <div <?php $this->parent->print_render_attribute_string( 'image-wrap' ); ?>> <?php if ( ( $settings['member_alternative_photo'] ) and ( ! empty ( $settings['alternative_photo']['url'] ) ) ) : ?> <div class="bdt-position-relative bdt-overflow-hidden" bdt-toggle="target: > .bdt-member-photo-flip; mode: hover; animation: bdt-animation-fade; queued: true; duration: 300;"> <div class="bdt-member-photo-flip bdt-position-absolute bdt-position-z-index"> <?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'alternative_photo' ) ); ?> </div> <?php endif; ?> <div class="bdt-member-photo"> <div class="<?php echo esc_attr( $photo_hover_animation ); ?>"> <?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'photo' ) ); ?> </div> </div> <?php if ( ( $settings['member_alternative_photo'] ) and ( ! empty ( $settings['alternative_photo']['url'] ) ) ) : ?> </div> <?php endif; ?> <?php if ( 'yes' == $settings['member_social_icon'] ) : ?> <div class="bdt-member-icons"> <?php foreach ( $settings['social_link_list'] as $link ) : $tooltip = ( 'yes' == $settings['social_icon_tooltip'] ) ? ' title="' . $link['social_link_title'] . '" data-bdt-tooltip' : ''; $migrated = isset ( $link['__fa4_migrated']['social_share_icon'] ); $is_new = empty ( $link['social_icon'] ) && Icons_Manager::is_migration_allowed(); ?> <a href="<?php echo esc_url( $link['social_link'] ); ?>" class="bdt-member-icon elementor-repeater-item-<?php echo esc_attr( $link['_id'] ); ?>" target="_blank" <?php echo wp_kses_post( $tooltip ); ?>> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $link['social_share_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $link['social_icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </a> <?php endforeach; ?> </div> <?php endif; ?> </div> <?php endif; ?> <div class="bdt-member-content"> <?php if ( ! empty ( $settings['name'] ) ) : ?> <span class="bdt-member-name"> <?php echo wp_kses( $settings['name'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> <?php if ( ! empty ( $settings['role'] ) ) : ?> <span class="bdt-member-role"> <?php echo wp_kses( $settings['role'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> <?php if ( ! empty ( $settings['description_text'] ) ) : ?> <div class="bdt-member-text bdt-content-wrap"> <?php echo wp_kses( $settings['description_text'], element_pack_allow_tags( 'text' ) ); ?> </div> <?php endif; ?> </div> </div> </div> <?php } } <?php namespace ElementPack\Modules\Member\Skins; use Elementor\Controls_Manager; use Elementor\Group_Control_Image_Size; use Elementor\Icons_Manager; use Elementor\Skin_Base as Elementor_Skin_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Partait extends Elementor_Skin_Base { public function get_id() { return 'bdt-partait'; } public function get_title() { return esc_html__( 'Partait', 'bdthemes-element-pack' ); } public function render() { $partait_id = 'partait' . $this->parent->get_id(); $settings = $this->parent->get_settings_for_display(); if ( ! isset ( $settings['social_icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $settings['social_icon'] = 'fab fa-facebook-f'; } $image_mask = $settings['image_mask_popover'] == 'yes' ? ' bdt-image-mask' : ''; $this->parent->add_render_attribute( 'image-wrap', 'class', 'bdt-member-photo-wrapper' . $image_mask ); ?> <div class="bdt-member skin-partait"> <div class="bdt-grid bdt-grid-collapse bdt-child-width-1-2@m" data-bdt-grid> <?php if ( ! empty ( $settings['photo']['url'] ) ) : $photo_hover_animation = ( '' != $settings['photo_hover_animation'] ) ? ' bdt-transition-scale-' . $settings['photo_hover_animation'] : ''; ?> <div <?php $this->parent->print_render_attribute_string( 'image-wrap' ); ?>> <?php if ( ( $settings['member_alternative_photo'] ) and ( ! empty ( $settings['alternative_photo']['url'] ) ) ) : ?> <div class="bdt-position-relative bdt-overflow-hidden" bdt-toggle="target: > .bdt-member-photo-flip; mode: hover; animation: bdt-animation-fade; queued: true; duration: 300;"> <div class="bdt-member-photo-flip bdt-position-absolute bdt-position-z-index"> <?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'alternative_photo' ) ); ?> </div> <?php endif; ?> <div class="bdt-member-photo"> <div class="<?php echo esc_attr( $photo_hover_animation ); ?>"> <?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'photo' ) ); ?> </div> </div> <?php if ( ( $settings['member_alternative_photo'] ) and ( ! empty ( $settings['alternative_photo']['url'] ) ) ) : ?> </div> <?php endif; ?> </div> <?php endif; ?> <div class="bdt-member-desc bdt-position-relative bdt-flex bdt-flex-middle"> <div class="bdt-text-center bdt-member-desc-wrapper"> <div class="bdt-member-content"> <?php if ( ! empty ( $settings['name'] ) ) : ?> <span class="bdt-member-name"> <?php echo wp_kses( $settings['name'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> <?php if ( ! empty ( $settings['role'] ) ) : ?> <span class="bdt-member-role"> <?php echo wp_kses( $settings['role'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> <?php if ( ! empty ( $settings['description_text'] ) ) : ?> <div class="bdt-member-text bdt-content-wrap"> <?php echo wp_kses( $settings['description_text'], element_pack_allow_tags( 'text' ) ); ?> </div> <?php endif; ?> </div> <?php if ( 'yes' == $settings['member_social_icon'] ) : ?> <div class="bdt-member-icons"> <?php foreach ( $settings['social_link_list'] as $link ) : $tooltip = ( 'yes' == $settings['social_icon_tooltip'] ) ? ' title="' . esc_attr( $link['social_link_title'] ) . '" data-bdt-tooltip' : ''; $migrated = isset ( $link['__fa4_migrated']['social_share_icon'] ); $is_new = empty ( $link['social_icon'] ) && Icons_Manager::is_migration_allowed(); ?> <a href="<?php echo esc_url( $link['social_link'] ); ?>" class="bdt-member-icon elementor-repeater-item-<?php echo esc_attr( $link['_id'] ); ?>" target="_blank" <?php echo wp_kses_post( $tooltip ); ?>> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $link['social_share_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $link['social_icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </a> <?php endforeach; ?> </div> <?php endif; ?> </div> </div> </div> </div> <?php } } <?php namespace ElementPack\Modules\Member\Skins; use Elementor\Controls_Manager; use Elementor\Group_Control_Image_Size; use Elementor\Icons_Manager; use ElementPack\Base\Module_Base; use Elementor\Skin_Base as Elementor_Skin_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Calm extends Elementor_Skin_Base { protected function _register_controls_actions() { parent::_register_controls_actions(); add_action( 'elementor/element/bdt-member/section_style/before_section_start', [ $this, 'register_calm_style_controls' ] ); } public function get_id() { return 'bdt-calm'; } public function get_title() { return __( 'Calm', 'bdthemes-element-pack' ); } public function register_calm_style_controls( Module_Base $widget ) { $this->parent = $widget; $this->start_controls_section( 'section_style_calm', [ 'label' => __( 'Calm', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'calm_overlay_color', [ 'label' => __( 'Overlay Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-overlay' => 'background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%,{{VALUE)}} 100%); background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,{{VALUE)}} 100%);', ], ] ); $this->end_controls_section(); } public function render() { $calm_id = 'calm' . $this->parent->get_id(); $settings = $this->parent->get_settings_for_display(); $image_mask = $settings['image_mask_popover'] == 'yes' ? ' bdt-image-mask' : ''; $this->parent->add_render_attribute( 'skin-calm', 'class', 'bdt-member skin-calm bdt-transition-toggle bdt-inline' . $image_mask ); if ( ( $settings['member_alternative_photo'] ) and ( ! empty ( $settings['alternative_photo']['url'] ) ) ) { $this->parent->add_render_attribute( 'skin-calm', 'class', [ 'bdt-position-relative', 'bdt-overflow-hidden', 'bdt-transition-toggle' ] ); $this->parent->add_render_attribute( 'skin-calm', 'bdt-toggle', 'target: > div > .bdt-member-photo-flip; mode: hover; animation: bdt-animation-fade; queued: true; duration: 300;' ); } if ( ! isset ( $settings['social_icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $settings['social_icon'] = 'fab fa-facebook-f'; } ?> <div <?php $this->parent->print_render_attribute_string( 'skin-calm' ); ?>> <?php if ( ! empty ( $settings['photo']['url'] ) ) : $photo_hover_animation = ( '' != $settings['photo_hover_animation'] ) ? ' bdt-transition-scale-' . $settings['photo_hover_animation'] : ''; ?> <div class="bdt-member-photo-wrapper"> <?php if ( ( $settings['member_alternative_photo'] ) and ( ! empty ( $settings['alternative_photo']['url'] ) ) ) : ?> <div class="bdt-member-photo-flip bdt-position-absolute bdt-position-z-index"> <?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'alternative_photo' ) ); ?> </div> <?php endif; ?> <div class="bdt-member-photo"> <div class="<?php echo esc_attr( $photo_hover_animation ); ?>"> <?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'photo' ) ); ?> </div> </div> </div> <?php endif; ?> <div class="bdt-member-overlay bdt-overlay bdt-position-bottom bdt-text-center bdt-position-z-index"> <div class="bdt-member-desc"> <div class="bdt-member-content bdt-transition-slide-bottom-small"> <?php if ( ! empty ( $settings['name'] ) ) : ?> <span class="bdt-member-name"> <?php echo wp_kses( $settings['name'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> <?php if ( ! empty ( $settings['role'] ) ) : ?> <span class="bdt-member-role"> <?php echo wp_kses( $settings['role'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> </div> <?php if ( 'yes' == $settings['member_social_icon'] ) : ?> <div class="bdt-member-icons bdt-transition-slide-bottom"> <?php foreach ( $settings['social_link_list'] as $link ) : $tooltip = ( 'yes' == $settings['social_icon_tooltip'] ) ? ' title="' . esc_attr( $link['social_link_title'] ) . '" data-bdt-tooltip' : ''; ?> <?php $migrated = isset ( $link['__fa4_migrated']['social_share_icon'] ); $is_new = empty ( $link['social_icon'] ) && Icons_Manager::is_migration_allowed(); ?> <a href="<?php echo esc_url( $link['social_link'] ); ?>" class="bdt-member-icon elementor-repeater-item-<?php echo esc_attr( $link['_id'] ); ?>" target="_blank" <?php echo wp_kses_post( $tooltip ); ?>> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $link['social_share_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $link['social_icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </a> <?php endforeach; ?> </div> <?php endif; ?> </div> </div> </div> <?php } } <?php namespace ElementPack\Modules\Member\Skins; use Elementor\Controls_Manager; use Elementor\Group_Control_Image_Size; use Elementor\Icons_Manager; use ElementPack\Base\Module_Base; use Elementor\Skin_Base as Elementor_Skin_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Skin_Phaedra extends Elementor_Skin_Base { protected function _register_controls_actions() { parent::_register_controls_actions(); add_action( 'elementor/element/bdt-member/section_style/before_section_start', [ $this, 'register_phaedra_style_controls' ] ); } public function get_id() { return 'bdt-phaedra'; } public function get_title() { return esc_html__( 'Phaedra', 'bdthemes-element-pack' ); } public function register_phaedra_style_controls( Module_Base $widget ) { $this->parent = $widget; $this->start_controls_section( 'section_style_phaedra', [ 'label' => __( 'Phaedra', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'phaedra_overlay_color', [ 'label' => __( 'Overlay Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-member .bdt-member-overlay' => 'background-color: {{VALUE}};', ], ] ); $this->end_controls_section(); } public function render() { $phaedra_id = 'phaedra' . $this->parent->get_id(); $settings = $this->parent->get_settings_for_display(); $image_mask = $settings['image_mask_popover'] == 'yes' ? ' bdt-image-mask' : ''; $this->parent->add_render_attribute( 'skin-phaedra', 'class', 'bdt-member skin-phaedra bdt-transition-toggle' . $image_mask ); if ( ( $settings['member_alternative_photo'] ) and ( ! empty ( $settings['alternative_photo']['url'] ) ) ) { $this->parent->add_render_attribute( 'skin-phaedra', 'class', [ 'bdt-position-relative', 'bdt-overflow-hidden', 'bdt-transition-toggle' ] ); $this->parent->add_render_attribute( 'skin-phaedra', 'bdt-toggle', 'target: > div > .bdt-member-photo-flip; mode: hover; animation: bdt-animation-fade; queued: true; duration: 300;' ); } if ( ! isset ( $settings['social_icon'] ) && ! Icons_Manager::is_migration_allowed() ) { // add old default $settings['social_icon'] = 'fab fa-facebook-f'; } ?> <div <?php $this->parent->print_render_attribute_string( 'skin-phaedra' ); ?>> <?php if ( ! empty ( $settings['photo']['url'] ) ) : $photo_hover_animation = ( '' != $settings['photo_hover_animation'] ) ? ' bdt-transition-scale-' . $settings['photo_hover_animation'] : ''; ?> <div class="bdt-member-photo-wrapper"> <?php if ( ( $settings['member_alternative_photo'] ) and ( ! empty ( $settings['alternative_photo']['url'] ) ) ) : ?> <div class="bdt-member-photo-flip bdt-position-absolute bdt-position-z-index"> <?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'alternative_photo' ) ); ?> </div> <?php endif; ?> <div class="bdt-member-photo"> <div class="<?php echo esc_attr( $photo_hover_animation ); ?>"> <?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'photo' ) ); ?> </div> </div> </div> <?php endif; ?> <div class="bdt-member-overlay bdt-overlay-default bdt-position-cover bdt-transition-fade bdt-position-z-index"> <div class="bdt-member-desc bdt-position-center bdt-text-center"> <div class="bdt-member-content bdt-transition-slide-top-small"> <?php if ( ! empty ( $settings['name'] ) ) : ?> <span class="bdt-member-name"> <?php echo wp_kses( $settings['name'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> <?php if ( ! empty ( $settings['role'] ) ) : ?> <span class="bdt-member-role"> <?php echo wp_kses( $settings['role'], element_pack_allow_tags( 'title' ) ); ?> </span> <?php endif; ?> </div> <?php if ( 'yes' == $settings['member_social_icon'] ) : ?> <div class="bdt-member-icons bdt-transition-slide-bottom-small"> <?php foreach ( $settings['social_link_list'] as $link ) : $tooltip = ( 'yes' == $settings['social_icon_tooltip'] ) ? ' data-bdt-tooltip="' . esc_attr( $link['social_link_title'] ) . '"' : ''; ?> <?php $migrated = isset ( $link['__fa4_migrated']['social_share_icon'] ); $is_new = empty ( $link['social_icon'] ) && Icons_Manager::is_migration_allowed(); ?> <a href="<?php echo esc_url( $link['social_link'] ); ?>" class="bdt-member-icon elementor-repeater-item-<?php echo esc_attr( $link['_id'] ); ?>" target="_blank" <?php echo wp_kses_post( $tooltip ); ?>> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $link['social_share_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $link['social_icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </a> <?php endforeach; ?> </div> <?php endif; ?> </div> </div> </div> <?php } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Member', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_style' => true, ]; <?php namespace ElementPack\Modules\CreativeButton\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Background; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Box_Shadow; use Elementor\Icons_Manager; if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } class Creative_Button extends Module_Base { public function get_name() { return 'bdt-creative-button'; } public function get_title() { return BDTEP . esc_html__( 'Creative Button', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-creative-button'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'button', 'creative', 'link', 'readmore', 'url', 'animated' ]; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return [ 'ep-font', 'ep-creative-button' ]; } } public function get_custom_help_url() { return 'https://youtu.be/6f2t-79MfnU'; } protected function register_controls() { $this->start_controls_section( 'section_creative_button', [ 'label' => esc_html__( 'Creative Button', 'bdthemes-element-pack' ), ] ); $this->add_control( 'button_style', [ 'label' => esc_html__( 'Style', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'anthe', 'options' => [ 'anthe' => esc_html__( 'Anthe', 'bdthemes-element-pack' ), 'atlas' => esc_html__( 'Atlas', 'bdthemes-element-pack' ), 'bestia' => esc_html__( 'Bestia', 'bdthemes-element-pack' ), 'calypso' => esc_html__( 'Calypso', 'bdthemes-element-pack' ), 'dione' => esc_html__( 'Dione', 'bdthemes-element-pack' ), 'fenrir' => esc_html__( 'Fenrir', 'bdthemes-element-pack' ), 'greip' => esc_html__( 'Greip', 'bdthemes-element-pack' ), 'hati' => esc_html__( 'Hati', 'bdthemes-element-pack' ), 'hyperion' => esc_html__( 'Hyperion', 'bdthemes-element-pack' ), 'helene' => esc_html__( 'Helene', 'bdthemes-element-pack' ), 'janus' => esc_html__( 'Janus', 'bdthemes-element-pack' ), 'kari' => esc_html__( 'Kari', 'bdthemes-element-pack' ), 'mimas' => esc_html__( 'Mimas', 'bdthemes-element-pack' ), 'narvi' => esc_html__( 'Narvi', 'bdthemes-element-pack' ), 'pan' => esc_html__( 'Pan', 'bdthemes-element-pack' ), 'pandora' => esc_html__( 'Pandora', 'bdthemes-element-pack' ), 'pallene' => esc_html__( 'Pallene', 'bdthemes-element-pack' ), 'rhea' => esc_html__( 'Rhea', 'bdthemes-element-pack' ), 'skoll' => esc_html__( 'Skoll', 'bdthemes-element-pack' ), 'surtur' => esc_html__( 'Surtur', 'bdthemes-element-pack' ), 'telesto' => esc_html__( 'Telesto', 'bdthemes-element-pack' ), 'reklo' => esc_html__( 'Reklo', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'text', [ 'label' => esc_html__( 'Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true ], 'default' => esc_html__( 'Read More', 'bdthemes-element-pack' ), 'placeholder' => esc_html__( 'Type Button Text', 'bdthemes-element-pack' ), ] ); $this->add_control( 'link', [ 'label' => esc_html__( 'Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::URL, 'dynamic' => [ 'active' => true ], 'placeholder' => esc_html__( 'https://your-link.com', 'bdthemes-element-pack' ), 'default' => [ 'url' => '#', ], ] ); $this->add_control( 'add_custom_attributes', [ 'label' => __( 'Add Custom Attributes', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'custom_attributes', [ 'label' => __( 'Custom Attributes', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXTAREA, 'dynamic' => [ 'active' => true, ], 'placeholder' => __( 'key|value', 'bdthemes-element-pack' ), 'description' => sprintf( __( 'Set custom attributes for the price table button tag. Each attribute in a separate line. Separate attribute key from the value using %s character.', 'bdthemes-element-pack' ), '<code>|</code>' ), 'classes' => 'elementor-control-direction-ltr', 'condition' => ['add_custom_attributes' => 'yes'] ] ); $this->add_control( 'onclick', [ 'label' => esc_html__( 'OnClick', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'onclick_event', [ 'label' => esc_html__( 'OnClick Event', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'placeholder' => 'myFunction()', 'description' => sprintf( esc_html__('For details please look <a href="%s" target="_blank">here</a>'), 'https://www.w3schools.com/jsref/event_onclick.asp' ), 'condition' => [ 'onclick' => 'yes' ] ] ); $this->add_responsive_control( 'alignment', [ 'label' => esc_html__( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'prefix_class' => 'elementor%s-align-', 'default' => '', 'options' => [ 'left' => [ 'title' => __( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => __( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'selectors' => [ '{{WRAPPER}}.elementor-widget-bdt-creative-button .elementor-widget-container' => 'text-align: {{VALUE}};', ], ] ); $this->add_control( 'button_css_id', [ 'label' => __( 'Button ID', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true, ], 'default' => '', 'title' => __( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'bdthemes-element-pack' ), 'description' => __( 'Please make sure the ID is unique and not used elsewhere on the page this form is displayed. This field allows <code>A-z 0-9</code> & underscore chars without spaces.', 'bdthemes-element-pack' ), 'separator' => 'before', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_style', [ 'label' => esc_html__( 'Creative Button', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs( 'tabs_creative_button_style' ); $this->start_controls_tab( 'tab_creative_button_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'creative_button_text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button, {{WRAPPER}} .bdt-ep-creative-button--dione span' => 'color: {{VALUE}};', ], 'condition' => [ 'button_style!' => ['surtur'] ] ] ); $this->add_control( 'creative_button_line_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--fenrir .progress__circle, {{WRAPPER}} .bdt-ep-creative-button--fenrir .progress__path' => 'stroke: {{VALUE}};', '{{WRAPPER}} .bdt-ep-creative-button--janus::after' => 'border-color: {{VALUE}};', ], 'condition' => [ 'button_style' => ['fenrir', 'janus'] ] ] ); $this->add_control( 'creative_button_stroke_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--surtur svg *' => 'stroke: {{VALUE}};', ], 'condition' => [ 'button_style' => ['surtur'] ] ] ); $this->add_control( 'creative_button_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button, {{WRAPPER}} .bdt-ep-creative-button--anthe::before, {{WRAPPER}} .bdt-ep-creative-button--bestia .bdt-ep-creative-button__bg, {{WRAPPER}} .bdt-ep-creative-button--dione::before, {{WRAPPER}} .bdt-ep-creative-button--greip::before, {{WRAPPER}} .bdt-ep-creative-button--hyperion::before, {{WRAPPER}} .bdt-ep-creative-button--janus::before, {{WRAPPER}} .bdt-ep-creative-button--mimas::before, {{WRAPPER}} .bdt-ep-creative-button--narvi::before, {{WRAPPER}} .bdt-ep-creative-button--pan::before, {{WRAPPER}} .bdt-ep-creative-button--pandora span, {{WRAPPER}} .bdt-ep-creative-button--rhea::before, {{WRAPPER}} .bdt-ep-creative-button--skoll::before' => 'background: {{VALUE}};', '{{WRAPPER}} .bdt-ep-creative-button--dione::after' => 'border-color: {{VALUE}};', ], 'condition' => [ 'button_style!' => ['fenrir', 'hati', 'surtur', 'reklo'] ] ] ); $this->add_control( 'secondary_creative_button_background_color', [ 'label' => esc_html__( 'Secondary Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button.bdt-ep-creative-button--pandora' => 'background: {{VALUE}};' ], 'condition' => [ 'button_style' => ['pandora'] ] ] ); $this->add_control( 'creative_button_helene_shadow_color', [ 'label' => esc_html__( 'Shadow Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--helene::before' => 'background: {{VALUE}};' ], 'condition' => [ 'button_style' => ['helene'] ] ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'creative_button_border', 'selector' => '{{WRAPPER}} .bdt-ep-creative-button, {{WRAPPER}} .bdt-ep-creative-button--bestia .bdt-ep-creative-button__bg', 'condition' => [ 'button_style!' => ['fenrir', 'janus', 'surtur', 'pandora', 'narvi', 'reklo'] ] ] ); $this->add_responsive_control( 'creative_button_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button, {{WRAPPER}} .bdt-ep-creative-button--bestia .bdt-ep-creative-button__bg, {{WRAPPER}} .bdt-ep-creative-button--pandora span, {{WRAPPER}} .bdt-ep-creative-button--dione::before, {{WRAPPER}} .bdt-ep-creative-button--dione::after' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'button_style!' => ['fenrir', 'janus', 'surtur', 'narvi', 'reklo'] ] ] ); $this->add_responsive_control( 'creative_button_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button, {{WRAPPER}} .bdt-ep-creative-button--bestia .bdt-ep-creative-button__bg span, {{WRAPPER}} .bdt-ep-creative-button-marquee span' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'button_style!' => ['fenrir', 'janus', 'surtur', 'pandora', 'rhea', 'reklo'] ] ] ); $this->add_responsive_control( 'creative_button_pandora_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--pandora span' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'button_style' => ['pandora'] ] ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'creative_button_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-creative-button, {{WRAPPER}} .bdt-ep-creative-button--bestia .bdt-ep-creative-button__bg', 'condition' => [ 'button_style!' => ['fenrir', 'janus', 'surtur', 'reklo'] ] ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'creative_button_typography', 'selector' => '{{WRAPPER}} .bdt-ep-creative-button', 'condition' => [ 'button_style!' => ['surtur'] ] ] ); $this->add_responsive_control( 'creative_button_size', [ 'label' => __( 'Size', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 50, 'max' => 500, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--surtur .textcircle' => 'width: {{SIZE}}{{UNIT}};', ], 'condition' => [ 'button_style' => ['surtur'] ] ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_creative_button_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'creative_button_hover_text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button:hover, {{WRAPPER}} .bdt-ep-creative-button--dione:hover span' => 'color: {{VALUE}};', ], 'condition' => [ 'button_style!' => ['surtur'] ] ] ); $this->add_control( 'creative_button_hover_stroke_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--surtur:hover svg *' => 'stroke: {{VALUE}};', ], 'condition' => [ 'button_style' => ['surtur'] ] ] ); $this->add_control( 'creative_button_hover_line_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--fenrir .progress__path' => 'stroke: {{VALUE}};', '{{WRAPPER}} .bdt-ep-creative-button--janus:hover::after' => 'border-color: {{VALUE}};', ], 'condition' => [ 'button_style' => ['fenrir', 'janus', 'pandora', 'narvi'] ] ] ); $this->add_control( 'creative_button_hover_background_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button:hover, {{WRAPPER}} .bdt-ep-creative-button--anthe:hover::before, {{WRAPPER}} .bdt-ep-creative-button--bestia .bdt-ep-creative-button__bg::before, {{WRAPPER}} .bdt-ep-creative-button--bestia .bdt-ep-creative-button__bg::after, {{WRAPPER}} .bdt-ep-creative-button--calypso::before, {{WRAPPER}} .bdt-ep-creative-button--calypso::after, {{WRAPPER}} .bdt-ep-creative-button--dione:hover::before, {{WRAPPER}} .bdt-ep-creative-button--greip, {{WRAPPER}} .bdt-ep-creative-button--hyperion, {{WRAPPER}} .bdt-ep-creative-button--janus:hover::before, {{WRAPPER}} .bdt-ep-creative-button--mimas, {{WRAPPER}} .bdt-ep-creative-button--narvi:hover::before, {{WRAPPER}} .bdt-ep-creative-button--pan, {{WRAPPER}} .bdt-ep-creative-button--pandora:hover span, {{WRAPPER}} .bdt-ep-creative-button--rhea:hover::before, {{WRAPPER}} .bdt-ep-creative-button--skoll, {{WRAPPER}} .bdt-ep-creative-button--telesto::before, {{WRAPPER}} .bdt-ep-creative-button--telesto::after' => 'background: {{VALUE}};', '{{WRAPPER}} .bdt-ep-creative-button--dione:hover::after' => 'border-color: {{VALUE}};', ], 'condition' => [ 'button_style!' => ['fenrir', 'hati', 'surtur', 'reklo'] ] ] ); $this->add_control( 'secondary_creative_button_background_hover', [ 'label' => esc_html__( 'Secondary Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button.bdt-ep-creative-button--pandora:hover' => 'background: {{VALUE}};' ], 'condition' => [ 'button_style' => ['pandora'] ] ] ); $this->add_control( 'button_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button:hover, {{WRAPPER}} .bdt-ep-creative-button--bestia:hover .bdt-ep-creative-button__bg' => 'border-color: {{VALUE}};', ], 'condition' => [ 'creative_button_border_border!' => '', 'button_style!' => ['fenrir', 'janus', 'surtur', 'narvi', 'reklo'] ] ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'creative_button_hover_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-creative-button:hover, {{WRAPPER}} .bdt-ep-creative-button--bestia:hover .bdt-ep-creative-button__bg', 'condition' => [ 'button_style!' => ['fenrir', 'janus', 'surtur', 'reklo'] ] ] ); $this->add_control( 'hover_animation', [ 'label' => esc_html__( 'Hover Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HOVER_ANIMATION, ] ); //icon color $this->add_control( 'creative_button_hover_icon_heading', [ 'label' => esc_html__( 'Icon Style', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::HEADING, 'condition' => [ 'button_style' => ['reklo'] ], 'separator' => 'before' ] ); $this->add_control( 'creative_button_hover_icon_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--reklo:hover i' => 'color: {{VALUE}};', ], 'condition' => [ 'button_style' => ['reklo'] ] ] ); $this->add_control( 'creative_button_hover_icon_bg_color', [ 'label' => esc_html__( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--reklo i' => 'background-color: {{VALUE}};', ], 'condition' => [ 'button_style' => ['reklo'] ] ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'icon_border', 'selector' => '{{WRAPPER}} .bdt-ep-creative-button--reklo i', 'condition' => [ 'button_style' => ['reklo'] ] ] ); $this->add_responsive_control( 'icon_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--reklo i' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'button_style' => ['reklo'] ] ] ); $this->add_responsive_control( 'icon_size', [ 'label' => esc_html__('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 10, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--reklo i' => 'height: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}}; line-height: {{SIZE}}{{UNIT}};', ], 'condition' => [ 'button_style' => ['reklo'] ] ] ); $this->add_responsive_control( 'icon_gap', [ 'label' => esc_html__('Space Between', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-creative-button--reklo' => 'gap: {{SIZE}}{{UNIT}};', ], 'condition' => [ 'button_style' => ['reklo'] ] ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); if ( ! empty( $settings['link']['url'] ) ) { $this->add_link_attributes( 'creative_button', $settings['link'] ); } if ( $settings['link']['nofollow'] ) { $this->add_render_attribute( 'creative_button', 'rel', 'nofollow' ); } if ($settings['onclick']) { $this->add_render_attribute( 'creative_button', 'onclick', $settings['onclick_event'] ); } if ( $settings['add_custom_attributes'] and ! empty( $settings['custom_attributes'] ) ) { $attributes = explode( "\n", $settings['custom_attributes'] ); $reserved_attr = [ 'href', 'target' ]; foreach ( $attributes as $attribute ) { if ( ! empty( $attribute ) ) { $attr = explode( '|', $attribute, 2 ); if ( ! isset( $attr[1] ) ) { $attr[1] = ''; } if ( ! in_array( strtolower( $attr[0] ), $reserved_attr ) ) { $this->add_render_attribute( 'creative_button', trim( $attr[0] ), trim( $attr[1] ) ); } } } } $this->add_render_attribute( 'creative_button', 'class', 'bdt-ep-creative-button' ); $this->add_render_attribute( 'creative_button', 'class', 'bdt-ep-creative-button--' . esc_attr($settings['button_style']) ); if ( $settings['hover_animation'] ) { $this->add_render_attribute( 'creative_button', 'class', 'elementor-animation-' . $settings['hover_animation'] ); } if ( ! empty( $settings['button_css_id'] ) ) { $this->add_render_attribute( 'creative_button', 'id', $settings['button_css_id'] ); } ?> <?php if ( $settings['button_style'] == 'hyperion' or $settings['button_style'] == 'telesto' or $settings['button_style'] == 'narvi' or $settings['button_style'] == 'helene' or $settings['button_style'] == 'greip' or $settings['button_style'] == 'skoll' ) : ?> <a <?php $this->print_render_attribute_string( 'creative_button' ); ?>><span><span><?php echo esc_html($settings['text']); ?></span></span></a> <?php elseif ( $settings['button_style'] == 'atlas' or $settings['button_style'] == 'kari' ) : ?> <a <?php $this->print_render_attribute_string( 'creative_button' ); ?>> <span><?php echo esc_html($settings['text']); ?></span> <div class="bdt-ep-creative-button-marquee" aria-hidden="true"> <div class="bdt-ep-creative-button-marquee__inner"> <span><?php echo esc_html($settings['text']); ?></span> <span><?php echo esc_html($settings['text']); ?></span> <span><?php echo esc_html($settings['text']); ?></span> <span><?php echo esc_html($settings['text']); ?></span> </div> </div> </a> <?php elseif ( $settings['button_style'] == 'pallene' ) : ?> <a <?php $this->print_render_attribute_string( 'creative_button' ); ?>><?php echo esc_html($settings['text']); ?></a> <?php elseif ( $settings['button_style'] == 'bestia' ) : ?> <a <?php $this->print_render_attribute_string( 'creative_button' ); ?>> <div class="bdt-ep-creative-button__bg"></div><span><?php echo esc_html($settings['text']); ?></span> </a> <?php elseif ( $settings['button_style'] == 'surtur' ) : ?> <a <?php $this->print_render_attribute_string( 'creative_button' ); ?>> <svg class="textcircle" viewBox="0 0 500 500"> <title><?php echo esc_html($settings['text']); ?></title> <defs><path id="textcircle" d="M250,400 a150,150 0 0,1 0,-300a150,150 0 0,1 0,300Z" /></defs> <text><textPath xlink:href="#textcircle" aria-label="<?php echo esc_html($settings['text']); ?>" textLength="900"><?php echo esc_html($settings['text']); ?></textPath></text> </svg> <svg aria-hidden="true" class="eye" width="70" height="70" viewBox="0 0 70 70" xmlns="http://www.w3.org/2000/svg"> <path class="eye__outer" d="M10.5 35.308c5.227-7.98 14.248-13.252 24.5-13.252s19.273 5.271 24.5 13.252c-5.227 7.98-14.248 13.253-24.5 13.253s-19.273-5.272-24.5-13.253z"/> <path class="eye__lashes-up" d="M35 8.802v8.836M49.537 11.383l-3.31 8.192M20.522 11.684l3.31 8.192" /> <path class="eye__lashes-down" d="M35 61.818v-8.836 8.836zM49.537 59.237l-3.31-8.193 3.31 8.193zM20.522 58.936l3.31-8.193-3.31 8.193z" /> <circle class="eye__iris" cx="35" cy="35.31" r="5.221" /> <circle class="eye__inner" cx="35" cy="35.31" r="10.041" /> </svg> </a> <?php elseif ( $settings['button_style'] == 'fenrir' ) : ?> <a <?php $this->print_render_attribute_string( 'creative_button' ); ?>> <svg aria-hidden="true" class="progress" width="70" height="70" viewbox="0 0 70 70"> <path class="progress__circle" d="m35,2.5c17.955803,0 32.5,14.544199 32.5,32.5c0,17.955803 -14.544197,32.5 -32.5,32.5c-17.955803,0 -32.5,-14.544197 -32.5,-32.5c0,-17.955801 14.544197,-32.5 32.5,-32.5z" /> <path class="progress__path" d="m35,2.5c17.955803,0 32.5,14.544199 32.5,32.5c0,17.955803 -14.544197,32.5 -32.5,32.5c-17.955803,0 -32.5,-14.544197 -32.5,-32.5c0,-17.955801 14.544197,-32.5 32.5,-32.5z" pathLength="1" /> </svg> <span><?php echo esc_html($settings['text']); ?></span> </a> <?php elseif ( $settings['button_style'] == 'reklo' ) : ?> <a <?php $this->print_render_attribute_string( 'creative_button' ); ?>> <span><?php echo esc_html($settings['text']); ?></span> <i class="ep-icon-arrow-right-0 bdt-flex bdt-flex-middle bdt-flex-center"></i> </a> <?php else: ?> <a <?php $this->print_render_attribute_string( 'creative_button' ); ?>><span><?php echo esc_html($settings['text']); ?></span></a> <?php endif; ?> <?php } } <?php namespace ElementPack\Modules\CreativeButton; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'creative-button'; } public function get_widgets() { $widgets = ['Creative_Button']; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Creative Button', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => false, 'has_style' => true, ]; <?php namespace ElementPack\Modules\BbpressSingleForum\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Background; use ElementPack\Includes\Controls\SelectInput\Dynamic_Select; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Bbpress_Single_Forum extends Module_Base { public function get_name() { return 'bdt-bbpress-single-forum'; } public function get_title() { return BDTEP . esc_html__('bbPress Single Forum', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-bbpress-single-forum'; } public function get_categories() { return ['element-pack-bbpress']; } public function get_keywords() { return ['bbpress', 'forum', 'community', 'discussion', 'support']; } public function get_custom_help_url() { return 'https://youtu.be/7vkAHZ778c4'; } protected function register_controls() { $this->start_controls_section( 'section_bbpress_content', [ 'label' => esc_html__('Layout', 'bdthemes-element-pack'), ] ); $this->add_control( 'bbpress_single_id', [ 'label' => __('Single Forums', 'bdthemes-element-pack'), 'type' => Dynamic_Select::TYPE, 'label_block' => true, 'placeholder' => __('Type and select Single Forum', 'bdthemes-element-pack'), 'query_args' => [ 'query' => 'bbpress_single_forum', ], ] ); $this->add_control( 'show_breadcrumb', [ 'label' => __('Show Breadcrumb', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before' ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'section_style_bbpress_breadcrumb', [ 'label' => esc_html__('Breadcrumb', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_breadcrumb' => 'yes' ] ] ); $this->add_control( 'breadcrumb_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-breadcrumb *, {{WRAPPER}} #bbpress-forums a.subscription-toggle' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'breadcrumb_hover_color', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-breadcrumb a:hover, {{WRAPPER}} #bbpress-forums a.subscription-toggle:hover' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'breadcrumb_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bbp-breadcrumb, {{WRAPPER}} #bbpress-forums a.subscription-toggle' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'breadcrumb_typography', 'selector' => '{{WRAPPER}} .bbp-breadcrumb, {{WRAPPER}} #bbpress-forums a.subscription-toggle', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_header', [ 'label' => esc_html__('Header', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'header_title_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums li.bbp-header li' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'header_background', 'selector' => '{{WRAPPER}} #bbpress-forums li.bbp-header', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'header_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #bbpress-forums li.bbp-header', 'separator' => 'before', ] ); $this->add_responsive_control( 'header_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums li.bbp-header' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'header_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums li.bbp-header' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'header_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums li.bbp-header' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'header_typography', 'selector' => '{{WRAPPER}} #bbpress-forums li.bbp-header li', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_body', [ 'label' => esc_html__('Body', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'forum_body_odd_color', [ 'label' => esc_html__('Odd Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums div.odd, {{WRAPPER}} #bbpress-forums ul.odd' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'forum_body_even_color', [ 'label' => esc_html__('Even Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums div.even, {{WRAPPER}} #bbpress-forums ul.even' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'forum_body_list_border_color', [ 'label' => esc_html__('Odd/Even Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums li.bbp-body ul.forum' => 'border-top-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'odd_even_forum_body_padding', [ 'label' => esc_html__( 'Odd/Even Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} #bbpress-forums div.even, {{WRAPPER}} #bbpress-forums ul.even, {{WRAPPER}} #bbpress-forums div.odd, {{WRAPPER}} #bbpress-forums ul.odd' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'forum_body_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #bbpress-forums ul.bbp-forums, {{WRAPPER}} #bbpress-forums ul.bbp-topics', 'separator' => 'before', ] ); $this->add_responsive_control( 'forum_body_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums ul.bbp-forums, {{WRAPPER}} #bbpress-forums ul.bbp-topics' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'forum_body_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums ul.bbp-forums, {{WRAPPER}} #bbpress-forums ul.bbp-topics' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'forum_body_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums ul.bbp-forums, {{WRAPPER}} #bbpress-forums ul.bbp-topics' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_forum_title', [ 'label' => esc_html__('Forum Title', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'forum_title_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-forum-title, {{WRAPPER}} #bbpress-forums .bbp-topic-permalink' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'forum_title_color_hover', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-forum-title:hover, {{WRAPPER}} #bbpress-forums .bbp-topic-permalink:hover' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'forum_title_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-forum-title, {{WRAPPER}} #bbpress-forums .bbp-topic-permalink' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'forum_title_typography', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-forum-title, {{WRAPPER}} #bbpress-forums .bbp-topic-permalink', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_forum_text', [ 'label' => esc_html__('Forum Text', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'forum_text_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-forum-content' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'forum_text_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-forum-content' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'forum_text_typography', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-forum-content', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_forum_title_list', [ 'label' => esc_html__('Forums List', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'forum_title_list_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-forums-list a' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'forum_title_list_color_hover', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-forums-list a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'forum_title_list_divider_color', [ 'label' => esc_html__( 'Divider Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-forums-list' => 'border-left-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'forum_title_list_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bbp-forums-list a' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'forum_title_list_typography', 'selector' => '{{WRAPPER}} .bbp-forums-list a', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_forum_count', [ 'label' => esc_html__('Topics/Posts Count', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'forum_count_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-forum-topic-count, {{WRAPPER}} .bbp-forum-reply-count, {{WRAPPER}} .bbp-topic-voice-count, {{WRAPPER}} .bbp-topic-reply-count' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'forum_count_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bbp-forum-topic-count, {{WRAPPER}} .bbp-forum-reply-count, {{WRAPPER}} .bbp-topic-voice-count, {{WRAPPER}} .bbp-topic-reply-count' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'forum_count_typography', 'selector' => '{{WRAPPER}} .bbp-forum-topic-count, {{WRAPPER}} .bbp-forum-reply-count, {{WRAPPER}} .bbp-topic-voice-count, {{WRAPPER}} .bbp-topic-reply-count', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_bbpress_forum_meta', [ 'label' => esc_html__('Forum Meta', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'forum_meta_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-forum-freshness a, {{WRAPPER}} #bbpress-forums .bbp-topic-freshness a, {{WRAPPER}} #bbpress-forums .bbp-topic-meta *' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'forum_meta_color_hover', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-forum-freshness a:hover, {{WRAPPER}} #bbpress-forums .bbp-topic-freshness a:hover, {{WRAPPER}} #bbpress-forums .bbp-topic-meta a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'forum_meta_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-forum-freshness a, {{WRAPPER}} #bbpress-forums .bbp-topic-freshness a, {{WRAPPER}} #bbpress-forums .bbp-topic-meta *' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'forum_meta_typography', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-forum-freshness a, {{WRAPPER}} #bbpress-forums .bbp-topic-freshness a, {{WRAPPER}} #bbpress-forums .bbp-topic-meta *', ] ); $this->end_controls_section(); //form $this->start_controls_section( 'section_style_bbpress_forum_form', [ 'label' => esc_html__('Forum Form', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'main_title_color', [ 'label' => esc_html__('Title Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-form legend' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'form_border', 'label' => esc_html__('Border', 'elementor-addons'), 'fields_options' => [ 'border' => [ 'default' => 'solid', ], 'width' => [ 'default' => [ 'top' => '1', 'right' => '1', 'bottom' => '1', 'left' => '1', 'unit' => 'px', 'isLinked' => false, ], ], 'color' => [ 'default' => '#c0c0c0', ], ], 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-topic-form .bbp-form', ] ); $this->add_responsive_control( 'form_border_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-form .bbp-form' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'form_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-form .bbp-form' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'form_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-form .bbp-form' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'main_title_typography', 'label' => esc_html__('Title Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-topic-form legend', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_label', [ 'label' => esc_html__('Label', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'label_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-form label' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'label_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-form label' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'label_typography', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-topic-form label', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_input', [ 'label' => esc_html__('Input', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'input_placeholder_color', [ 'label' => esc_html__('Placeholder Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-form input::placeholder' => 'color: {{VALUE}};', '{{WRAPPER}} #bbpress-forums .bbp-topic-form textarea::placeholder' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'input_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums input[type="text"], {{WRAPPER}} #bbpress-forums input[type="date"], {{WRAPPER}} #bbpress-forums input[type="email"], {{WRAPPER}} #bbpress-forums input[type="number"], {{WRAPPER}} #bbpress-forums input[type="password"], {{WRAPPER}} #bbpress-forums input[type="search"], {{WRAPPER}} #bbpress-forums input[type="tel"], {{WRAPPER}} #bbpress-forums input[type="url"], {{WRAPPER}} #bbpress-forums select, {{WRAPPER}} #bbpress-forums textarea' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'input_text_background', 'selector' => '{{WRAPPER}} #bbpress-forums input[type="text"], {{WRAPPER}} #bbpress-forums input[type="date"], {{WRAPPER}} #bbpress-forums input[type="email"], {{WRAPPER}} #bbpress-forums input[type="number"], {{WRAPPER}} #bbpress-forums input[type="password"], {{WRAPPER}} #bbpress-forums input[type="search"], {{WRAPPER}} #bbpress-forums input[type="tel"], {{WRAPPER}} #bbpress-forums input[type="url"], {{WRAPPER}} #bbpress-forums select, {{WRAPPER}} #bbpress-forums textarea', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'input_border', 'label' => esc_html__('Border', 'elementor-addons'), 'fields_options' => [ 'border' => [ 'default' => 'solid', ], 'width' => [ 'default' => [ 'top' => '1', 'right' => '1', 'bottom' => '1', 'left' => '1', 'unit' => 'px', 'isLinked' => false, ], ], 'color' => [ 'default' => '#c0c0c0', ], ], 'selector' => '{{WRAPPER}} #bbpress-forums input[type="text"], {{WRAPPER}} #bbpress-forums input[type="date"], {{WRAPPER}} #bbpress-forums input[type="email"], {{WRAPPER}} #bbpress-forums input[type="number"], {{WRAPPER}} #bbpress-forums input[type="password"], {{WRAPPER}} #bbpress-forums input[type="search"], {{WRAPPER}} #bbpress-forums input[type="tel"], {{WRAPPER}} #bbpress-forums input[type="url"], {{WRAPPER}} #bbpress-forums select, {{WRAPPER}} #bbpress-forums textarea', ] ); $this->add_responsive_control( 'input_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums input[type="text"], {{WRAPPER}} #bbpress-forums input[type="date"], {{WRAPPER}} #bbpress-forums input[type="email"], {{WRAPPER}} #bbpress-forums input[type="number"], {{WRAPPER}} #bbpress-forums input[type="password"], {{WRAPPER}} #bbpress-forums input[type="search"], {{WRAPPER}} #bbpress-forums input[type="tel"], {{WRAPPER}} #bbpress-forums input[type="url"], {{WRAPPER}} #bbpress-forums select, {{WRAPPER}} #bbpress-forums textarea' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'input_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums input[type="text"], {{WRAPPER}} #bbpress-forums input[type="date"], {{WRAPPER}} #bbpress-forums input[type="email"], {{WRAPPER}} #bbpress-forums input[type="number"], {{WRAPPER}} #bbpress-forums input[type="password"], {{WRAPPER}} #bbpress-forums input[type="search"], {{WRAPPER}} #bbpress-forums input[type="tel"], {{WRAPPER}} #bbpress-forums input[type="url"], {{WRAPPER}} #bbpress-forums select, {{WRAPPER}} #bbpress-forums textarea' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'input_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} #bbpress-forums input[type="text"], {{WRAPPER}} #bbpress-forums input[type="date"], {{WRAPPER}} #bbpress-forums input[type="email"], {{WRAPPER}} #bbpress-forums input[type="number"], {{WRAPPER}} #bbpress-forums input[type="password"], {{WRAPPER}} #bbpress-forums input[type="search"], {{WRAPPER}} #bbpress-forums input[type="tel"], {{WRAPPER}} #bbpress-forums input[type="url"], {{WRAPPER}} #bbpress-forums select, {{WRAPPER}} #bbpress-forums textarea', ] ); $this->add_responsive_control( 'input_height', [ 'label' => esc_html__( 'Input Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 500, ], ], 'selectors' => [ '{{WRAPPER}} #bbpress-forums input[type="text"], {{WRAPPER}} #bbpress-forums input[type="date"], {{WRAPPER}} #bbpress-forums input[type="email"], {{WRAPPER}} #bbpress-forums input[type="number"], {{WRAPPER}} #bbpress-forums input[type="password"], {{WRAPPER}} #bbpress-forums input[type="search"], {{WRAPPER}} #bbpress-forums input[type="tel"], {{WRAPPER}} #bbpress-forums input[type="url"], {{WRAPPER}} #bbpress-forums select' => 'height: {{SIZE}}{{UNIT}}; min-height: {{SIZE}}{{UNIT}};', ], 'separator' => 'before', ] ); $this->add_responsive_control( 'textarea_height', [ 'label' => esc_html__('Textarea Height', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 200, ], 'range' => [ 'px' => [ 'min' => 30, 'max' => 500, ], ], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-topic-form textarea#bbp_forum_content' => 'height: {{SIZE}}{{UNIT}}; width: 100%;', ], ] ); $this->add_responsive_control( 'input_textarea_width', [ 'label' => esc_html__( 'Input/Textarea Width(%)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} #bbpress-forums input[type="text"], {{WRAPPER}} #bbpress-forums input[type="date"], {{WRAPPER}} #bbpress-forums input[type="email"], {{WRAPPER}} #bbpress-forums input[type="number"], {{WRAPPER}} #bbpress-forums input[type="password"], {{WRAPPER}} #bbpress-forums input[type="search"], {{WRAPPER}} #bbpress-forums input[type="tel"], {{WRAPPER}} #bbpress-forums input[type="url"], {{WRAPPER}} #bbpress-forums select, {{WRAPPER}} #bbpress-forums textarea' => 'width: {{SIZE}}%; max-width: {{SIZE}}%;', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_submit_button', [ 'label' => esc_html__('Submit Button', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'button_alignment', [ 'label' => esc_html__('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => esc_html__('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => esc_html__('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => esc_html__('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => esc_html__('Justify', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-justify', ], ], 'selectors_dictionary' => [ 'left' => 'text-align: left; float: inherit;', 'right' => 'text-align: right; float: inherit;', 'center' => 'text-align: center; float: inherit;', 'justify' => 'text-align: justify; float: inherit; width: 100%;', ], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper' => '{{VALUE}};', '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button' => '{{VALUE}};', ], ] ); $this->start_controls_tabs('tabs_button_style'); $this->start_controls_tab( 'tab_button_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'button_background_color', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'button_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button', 'separator' => 'before', ] ); $this->add_responsive_control( 'button_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'button_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'button_typography', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_box_shadow', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_button_hover', [ 'label' => esc_html__('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_hover_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'button_background_color_hover', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button:hover', ] ); $this->add_control( 'button_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'button_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-submit-wrapper button:hover' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_pagination', [ 'label' => esc_html__('Pagination', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'pagination_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bbp-pagination-count' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'pagination_text_typography', 'label' => esc_html__('Text Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bbp-pagination-count', ] ); $this->start_controls_tabs('tabs_pagination_style'); $this->start_controls_tab( 'tab_pagination_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'pagination_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'pagination_background_color', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'pagination_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current', 'separator' => 'before', ] ); $this->add_responsive_control( 'pagination_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'pagination_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'pagination_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'pagination_typography', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'pagination_box_shadow', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a, {{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_pagination_hover', [ 'label' => esc_html__('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'pagination_hover_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'pagination_background_color_hover', 'selector' => '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a:hover:hover', ] ); $this->add_control( 'pagination_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'pagination_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links a:hover:hover' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_pagination_active', [ 'label' => esc_html__('Active', 'bdthemes-element-pack'), ] ); $this->add_control( 'pagination_active_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #bbpress-forums .bbp-pagination-links span.current' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'pagination_background_color_active', 'selector' => '#bbpress-forums .bbp-pagination-links span.current', ] ); $this->add_control( 'pagination_active_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'pagination_border_border!' => '', ], 'selectors' => [ '#bbpress-forums .bbp-pagination-links span.current' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_notice', [ 'label' => esc_html__( 'Notice', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'notice_text_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} div.bbp-template-notice, {{WRAPPER}} div.indicator-hint' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'notice_background_color', 'selector' => '{{WRAPPER}} div.bbp-template-notice, {{WRAPPER}} div.indicator-hint', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'notice_border', 'label' => esc_html__('Border', 'elementor-addons'), 'fields_options' => [ 'border' => [ 'default' => 'solid', ], 'width' => [ 'default' => [ 'top' => '1', 'right' => '1', 'bottom' => '1', 'left' => '1', 'unit' => 'px', 'isLinked' => false, ], ], 'color' => [ 'default' => '#c0c0c0', ], ], 'selector' => '{{WRAPPER}} div.bbp-template-notice, {{WRAPPER}} div.indicator-hint', ] ); $this->add_responsive_control( 'notice_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} div.bbp-template-notice, {{WRAPPER}} div.indicator-hint' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'notice_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} div.bbp-template-notice, {{WRAPPER}} div.indicator-hint' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'notice_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} div.bbp-template-notice, {{WRAPPER}} div.indicator-hint' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'notice_text_typography', 'label' => esc_html__( 'Title Typography', 'bdthemes-element-pack' ), 'selector' => '{{WRAPPER}} div.bbp-template-notice p, {{WRAPPER}} div.bbp-template-notice li', ] ); $this->end_controls_section(); } protected function render_loop_single_forum() { ?> <ul id="bbp-forum-<?php bbp_forum_id(); ?>" <?php bbp_forum_class(); ?>> <li class="bbp-forum-info"> <?php if (bbp_is_user_home() && bbp_is_subscriptions()) : ?> <span class="bbp-row-actions"> <?php do_action('bbp_theme_before_forum_subscription_action'); ?> <?php bbp_forum_subscription_link(array('before' => '', 'subscribe' => '+', 'unsubscribe' => '×')); ?> <?php do_action('bbp_theme_after_forum_subscription_action'); ?> </span> <?php endif; ?> <?php do_action('bbp_theme_before_forum_title'); ?> <a class="bbp-forum-title bdt-inline" href="<?php bbp_forum_permalink(); ?>"><?php bbp_forum_title(); ?></a> <?php do_action('bbp_theme_after_forum_title'); ?> <?php do_action('bbp_theme_before_forum_description'); ?> <div class="bbp-forum-content"><?php bbp_forum_content(); ?></div> <?php do_action('bbp_theme_after_forum_description'); ?> <?php do_action('bbp_theme_before_forum_sub_forums'); ?> <?php bbp_list_forums(); ?> <?php do_action('bbp_theme_after_forum_sub_forums'); ?> <?php bbp_forum_row_actions(); ?> </li> <li class="bbp-forum-topic-count"><?php bbp_forum_topic_count(); ?></li> <li class="bbp-forum-reply-count"><?php bbp_show_lead_topic() ? bbp_forum_reply_count() : bbp_forum_post_count(); ?></li> <li class="bbp-forum-freshness"> <?php do_action('bbp_theme_before_forum_freshness_link'); ?> <?php bbp_forum_freshness_link(); ?> <?php do_action('bbp_theme_after_forum_freshness_link'); ?> <p class="bbp-topic-meta"> <?php do_action('bbp_theme_before_topic_author'); ?> <span class="bbp-topic-freshness-author"><?php bbp_author_link(array('post_id' => bbp_get_forum_last_active_id(), 'size' => 14)); ?></span> <?php do_action('bbp_theme_after_topic_author'); ?> </p> </li> </ul> <?php } protected function render_loop_forum() { do_action('bbp_template_before_forums_loop'); ?> <ul id="forums-list-<?php bbp_forum_id(); ?>" class="bbp-forums"> <li class="bbp-header"> <ul class="forum-titles"> <li class="bbp-forum-info"><?php esc_html_e('Forum', 'bbpress'); ?></li> <li class="bbp-forum-topic-count"><?php esc_html_e('Topics', 'bbpress'); ?></li> <li class="bbp-forum-reply-count"><?php bbp_show_lead_topic() ? esc_html_e('Replies', 'bbpress') : esc_html_e('Posts', 'bbpress'); ?></li> <li class="bbp-forum-freshness"><?php esc_html_e('Last Post', 'bbpress'); ?></li> </ul> </li><!-- .bbp-header --> <li class="bbp-body"> <?php while (bbp_forums()) : bbp_the_forum(); ?> <?php $this->render_loop_single_forum(); ?> <?php endwhile; ?> </li><!-- .bbp-body --> <li class="bbp-footer"> <div class="tr"> <p class="td colspan4"> </p> </div><!-- .tr --> </li><!-- .bbp-footer --> </ul><!-- .forums-directory --> <?php do_action('bbp_template_after_forums_loop'); } protected function render_protected_form() { ?> <div id="bbpress-forums" class="bbpress-wrapper"> <fieldset class="bbp-form" id="bbp-protected"> <Legend><?php esc_html_e('Protected', 'bbpress'); ?></legend> <?php echo get_the_password_form(); ?> </fieldset> </div> <?php } protected function render_user_login_form() { ?> <form method="post" action="<?php bbp_wp_login_action(array('context' => 'login_post')); ?>" class="bbp-login-form"> <fieldset class="bbp-form"> <legend><?php esc_html_e('Log In', 'bbpress'); ?></legend> <div class="bbp-username"> <label for="user_login"><?php esc_html_e('Username', 'bbpress'); ?>: </label> <input type="text" name="log" value="<?php bbp_sanitize_val('user_login', 'text'); ?>" size="20" maxlength="100" id="user_login" autocomplete="off" /> </div> <div class="bbp-password"> <label for="user_pass"><?php esc_html_e('Password', 'bbpress'); ?>: </label> <input type="password" name="pwd" value="<?php bbp_sanitize_val('user_pass', 'password'); ?>" size="20" id="user_pass" autocomplete="off" /> </div> <div class="bbp-remember-me"> <input type="checkbox" name="rememberme" value="forever" <?php checked(bbp_get_sanitize_val('rememberme', 'checkbox')); ?> id="rememberme" /> <label for="rememberme"><?php esc_html_e('Keep me signed in', 'bbpress'); ?></label> </div> <?php do_action('login_form'); ?> <div class="bbp-submit-wrapper"> <button type="submit" name="user-submit" id="user-submit" class="button submit user-submit"><?php esc_html_e('Log In', 'bbpress'); ?></button> <?php bbp_user_login_fields(); ?> </div> </fieldset> </form> <?php } protected function render_alert_topic_lock() { do_action('bbp_theme_before_alert_topic_lock'); ?> <?php if (bbp_show_topic_lock_alert()) : ?> <div class="bbp-alert-outer"> <div class="bbp-alert-inner"> <p class="bbp-alert-description"><?php bbp_topic_lock_description(); ?></p> <p class="bbp-alert-actions"> <a class="bbp-alert-back" href="<?php bbp_forum_permalink(bbp_get_topic_forum_id()); ?>"><?php esc_html_e('Leave', 'bbpress'); ?></a> <a class="bbp-alert-close" href="#"><?php esc_html_e('Stay', 'bbpress'); ?></a> </p> </div> </div> <?php endif; do_action('bbp_theme_after_alert_topic_lock'); } protected function render_form_anonymous() { if (bbp_current_user_can_access_anonymous_user_form()) : ?> <?php do_action('bbp_theme_before_anonymous_form'); ?> <fieldset class="bbp-form"> <legend><?php (bbp_is_topic_edit() || bbp_is_reply_edit()) ? esc_html_e('Author Information', 'bbpress') : esc_html_e('Your information:', 'bbpress'); ?></legend> <?php do_action('bbp_theme_anonymous_form_extras_top'); ?> <p> <label for="bbp_anonymous_author"><?php esc_html_e('Name (required):', 'bbpress'); ?></label><br /> <input type="text" id="bbp_anonymous_author" value="<?php bbp_author_display_name(); ?>" size="40" maxlength="100" name="bbp_anonymous_name" autocomplete="off" /> </p> <p> <label for="bbp_anonymous_email"><?php esc_html_e('Mail (will not be published) (required):', 'bbpress'); ?></label><br /> <input type="text" id="bbp_anonymous_email" value="<?php bbp_author_email(); ?>" size="40" maxlength="100" name="bbp_anonymous_email" /> </p> <p> <label for="bbp_anonymous_website"><?php esc_html_e('Website:', 'bbpress'); ?></label><br /> <input type="text" id="bbp_anonymous_website" value="<?php bbp_author_url(); ?>" size="40" maxlength="200" name="bbp_anonymous_website" /> </p> <?php do_action('bbp_theme_anonymous_form_extras_bottom'); ?> </fieldset> <?php do_action('bbp_theme_after_anonymous_form'); ?> <?php endif; } protected function render_form_topic() { $settings = $this->get_settings_for_display(); if (!bbp_is_single_forum()) : ?> <div id="bbpress-forums" class="bbpress-wrapper"> <?php if ($settings['show_breadcrumb']) : ?> <?php bbp_breadcrumb(); ?> <?php endif; ?> <?php endif; ?> <?php if (bbp_is_topic_edit()) : ?> <?php bbp_topic_tag_list(bbp_get_topic_id()); ?> <?php bbp_single_topic_description(array('topic_id' => bbp_get_topic_id())); ?> <?php $this->render_alert_topic_lock(); ?> <?php endif; ?> <?php if (bbp_current_user_can_access_create_topic_form()) : ?> <div id="new-topic-<?php bbp_topic_id(); ?>" class="bbp-topic-form"> <form id="new-post" name="new-post" method="post"> <?php do_action('bbp_theme_before_topic_form'); ?> <fieldset class="bbp-form"> <legend> <?php if (bbp_is_topic_edit()) : printf(esc_html__('Now Editing “%s”', 'bbpress'), bbp_get_topic_title()); else : (bbp_is_single_forum() && bbp_get_forum_title()) ? printf(esc_html__('Create New Topic in “%s”', 'bbpress'), bbp_get_forum_title()) : esc_html_e('Create New Topic', 'bbpress'); endif; ?> </legend> <?php do_action('bbp_theme_before_topic_form_notices'); ?> <?php if (!bbp_is_topic_edit() && bbp_is_forum_closed()) : ?> <div class="bbp-template-notice"> <ul> <li><?php esc_html_e('This forum is marked as closed to new topics, however your posting capabilities still allow you to create a topic.', 'bbpress'); ?></li> </ul> </div> <?php endif; ?> <?php if (current_user_can('unfiltered_html')) : ?> <div class="bbp-template-notice"> <ul> <li><?php esc_html_e('Your account has the ability to post unrestricted HTML content.', 'bbpress'); ?></li> </ul> </div> <?php endif; ?> <?php do_action('bbp_template_notices'); ?> <div> <?php $this->render_form_anonymous(); ?> <?php do_action('bbp_theme_before_topic_form_title'); ?> <p> <label for="bbp_topic_title"><?php printf(esc_html__('Topic Title (Maximum Length: %d):', 'bbpress'), bbp_get_title_max_length()); ?></label><br /> <input type="text" id="bbp_topic_title" value="<?php bbp_form_topic_title(); ?>" size="40" name="bbp_topic_title" maxlength="<?php bbp_title_max_length(); ?>" /> </p> <?php do_action('bbp_theme_after_topic_form_title'); ?> <?php do_action('bbp_theme_before_topic_form_content'); ?> <?php bbp_the_content(array('context' => 'topic')); ?> <?php do_action('bbp_theme_after_topic_form_content'); ?> <?php if (!(bbp_use_wp_editor() || current_user_can('unfiltered_html'))) : ?> <p class="form-allowed-tags"> <label><?php printf(esc_html__('You may use these %s tags and attributes:', 'bbpress'), '<abbr title="HyperText Markup Language">HTML</abbr>'); ?></label><br /> <code><?php bbp_allowed_tags(); ?></code> </p> <?php endif; ?> <?php if (bbp_allow_topic_tags() && current_user_can('assign_topic_tags', bbp_get_topic_id())) : ?> <?php do_action('bbp_theme_before_topic_form_tags'); ?> <p> <label for="bbp_topic_tags"><?php esc_html_e('Topic Tags:', 'bbpress'); ?></label><br /> <input type="text" value="<?php bbp_form_topic_tags(); ?>" size="40" name="bbp_topic_tags" id="bbp_topic_tags" <?php disabled(bbp_is_topic_spam()); ?> /> </p> <?php do_action('bbp_theme_after_topic_form_tags'); ?> <?php endif; ?> <?php if (!bbp_is_single_forum()) : ?> <?php do_action('bbp_theme_before_topic_form_forum'); ?> <p> <label for="bbp_forum_id"><?php esc_html_e('Forum:', 'bbpress'); ?></label><br /> <?php bbp_dropdown(array( 'show_none' => esc_html__('— No forum —', 'bbpress'), 'selected' => bbp_get_form_topic_forum() )); ?> </p> <?php do_action('bbp_theme_after_topic_form_forum'); ?> <?php endif; ?> <?php if (current_user_can('moderate', bbp_get_topic_id())) : ?> <?php do_action('bbp_theme_before_topic_form_type'); ?> <p> <label for="bbp_stick_topic"><?php esc_html_e('Topic Type:', 'bbpress'); ?></label><br /> <?php bbp_form_topic_type_dropdown(); ?> </p> <?php do_action('bbp_theme_after_topic_form_type'); ?> <?php do_action('bbp_theme_before_topic_form_status'); ?> <p> <label for="bbp_topic_status"><?php esc_html_e('Topic Status:', 'bbpress'); ?></label><br /> <?php bbp_form_topic_status_dropdown(); ?> </p> <?php do_action('bbp_theme_after_topic_form_status'); ?> <?php endif; ?> <?php if (bbp_is_subscriptions_active() && !bbp_is_anonymous() && (!bbp_is_topic_edit() || (bbp_is_topic_edit() && !bbp_is_topic_anonymous()))) : ?> <?php do_action('bbp_theme_before_topic_form_subscriptions'); ?> <p> <input name="bbp_topic_subscription" id="bbp_topic_subscription" type="checkbox" value="bbp_subscribe" <?php bbp_form_topic_subscribed(); ?> /> <?php if (bbp_is_topic_edit() && (bbp_get_topic_author_id() !== bbp_get_current_user_id())) : ?> <label for="bbp_topic_subscription"><?php esc_html_e('Notify the author of follow-up replies via email', 'bbpress'); ?></label> <?php else : ?> <label for="bbp_topic_subscription"><?php esc_html_e('Notify me of follow-up replies via email', 'bbpress'); ?></label> <?php endif; ?> </p> <?php do_action('bbp_theme_after_topic_form_subscriptions'); ?> <?php endif; ?> <?php if (bbp_allow_revisions() && bbp_is_topic_edit()) : ?> <?php do_action('bbp_theme_before_topic_form_revisions'); ?> <fieldset class="bbp-form"> <legend> <input name="bbp_log_topic_edit" id="bbp_log_topic_edit" type="checkbox" value="1" <?php bbp_form_topic_log_edit(); ?> /> <label for="bbp_log_topic_edit"><?php esc_html_e('Keep a log of this edit:', 'bbpress'); ?></label><br /> </legend> <div> <label for="bbp_topic_edit_reason"><?php printf(esc_html__('Optional reason for editing:', 'bbpress'), bbp_get_current_user_name()); ?></label><br /> <input type="text" value="<?php bbp_form_topic_edit_reason(); ?>" size="40" name="bbp_topic_edit_reason" id="bbp_topic_edit_reason" /> </div> </fieldset> <?php do_action('bbp_theme_after_topic_form_revisions'); ?> <?php endif; ?> <?php do_action('bbp_theme_before_topic_form_submit_wrapper'); ?> <div class="bbp-submit-wrapper"> <?php do_action('bbp_theme_before_topic_form_submit_button'); ?> <button type="submit" id="bbp_topic_submit" name="bbp_topic_submit" class="button submit"><?php esc_html_e('Submit', 'bbpress'); ?></button> <?php do_action('bbp_theme_after_topic_form_submit_button'); ?> </div> <?php do_action('bbp_theme_after_topic_form_submit_wrapper'); ?> </div> <?php bbp_topic_form_fields(); ?> </fieldset> <?php do_action('bbp_theme_after_topic_form'); ?> </form> </div> <?php elseif (bbp_is_forum_closed()) : ?> <div id="forum-closed-<?php bbp_forum_id(); ?>" class="bbp-forum-closed"> <div class="bbp-template-notice"> <ul> <li><?php printf(esc_html__('The forum ‘%s’ is closed to new topics and replies.', 'bbpress'), bbp_get_forum_title()); ?></li> </ul> </div> </div> <?php else : ?> <div id="no-topic-<?php bbp_forum_id(); ?>" class="bbp-no-topic"> <div class="bbp-template-notice"> <ul> <li><?php is_user_logged_in() ? esc_html_e('You cannot create new topics.', 'bbpress') : esc_html_e('You must be logged in to create new topics.', 'bbpress'); ?></li> </ul> </div> <?php if (!is_user_logged_in()) : ?> <?php $this->render_user_login_form(); ?> <?php endif; ?> </div> <?php endif; ?> <?php if (!bbp_is_single_forum()) : ?> </div> <?php endif; } protected function render_loop_single_topic() { ?> <ul id="bbp-topic-<?php bbp_topic_id(); ?>" <?php bbp_topic_class(); ?>> <li class="bbp-topic-title"> <?php if (bbp_is_user_home()) : ?> <?php if (bbp_is_favorites()) : ?> <span class="bbp-row-actions"> <?php do_action('bbp_theme_before_topic_favorites_action'); ?> <?php bbp_topic_favorite_link(array('before' => '', 'favorite' => '+', 'favorited' => '×')); ?> <?php do_action('bbp_theme_after_topic_favorites_action'); ?> </span> <?php elseif (bbp_is_subscriptions()) : ?> <span class="bbp-row-actions"> <?php do_action('bbp_theme_before_topic_subscription_action'); ?> <?php bbp_topic_subscription_link(array('before' => '', 'subscribe' => '+', 'unsubscribe' => '×')); ?> <?php do_action('bbp_theme_after_topic_subscription_action'); ?> </span> <?php endif; ?> <?php endif; ?> <?php do_action('bbp_theme_before_topic_title'); ?> <a class="bbp-topic-permalink bdt-inline" href="<?php bbp_topic_permalink(); ?>"><?php bbp_topic_title(); ?></a> <?php do_action('bbp_theme_after_topic_title'); ?> <?php bbp_topic_pagination(); ?> <?php do_action('bbp_theme_before_topic_meta'); ?> <p class="bbp-topic-meta"> <?php do_action('bbp_theme_before_topic_started_by'); ?> <span class="bbp-topic-started-by"><?php printf(esc_html__('Started by: %1$s', 'bbpress'), bbp_get_topic_author_link(array('size' => '14'))); ?></span> <?php do_action('bbp_theme_after_topic_started_by'); ?> <?php if (!bbp_is_single_forum() || (bbp_get_topic_forum_id() !== bbp_get_forum_id())) : ?> <?php do_action('bbp_theme_before_topic_started_in'); ?> <span class="bbp-topic-started-in"><?php printf(esc_html__('in: %1$s', 'bbpress'), '<a href="' . bbp_get_forum_permalink(bbp_get_topic_forum_id()) . '">' . bbp_get_forum_title(bbp_get_topic_forum_id()) . '</a>'); ?></span> <?php do_action('bbp_theme_after_topic_started_in'); ?> <?php endif; ?> </p> <?php do_action('bbp_theme_after_topic_meta'); ?> <?php bbp_topic_row_actions(); ?> </li> <li class="bbp-topic-voice-count"><?php bbp_topic_voice_count(); ?></li> <li class="bbp-topic-reply-count"><?php bbp_show_lead_topic() ? bbp_topic_reply_count() : bbp_topic_post_count(); ?></li> <li class="bbp-topic-freshness"> <?php do_action('bbp_theme_before_topic_freshness_link'); ?> <?php bbp_topic_freshness_link(); ?> <?php do_action('bbp_theme_after_topic_freshness_link'); ?> <p class="bbp-topic-meta"> <?php do_action('bbp_theme_before_topic_freshness_author'); ?> <span class="bbp-topic-freshness-author"><?php bbp_author_link(array('post_id' => bbp_get_topic_last_active_id(), 'size' => 14)); ?></span> <?php do_action('bbp_theme_after_topic_freshness_author'); ?> </p> </li> </ul> <?php } protected function render_loop_topics() { do_action('bbp_template_before_topics_loop'); ?> <ul id="bbp-forum-<?php bbp_forum_id(); ?>" class="bbp-topics"> <li class="bbp-header"> <ul class="forum-titles"> <li class="bbp-topic-title"><?php esc_html_e('Topic', 'bbpress'); ?></li> <li class="bbp-topic-voice-count"><?php esc_html_e('Voices', 'bbpress'); ?></li> <li class="bbp-topic-reply-count"><?php bbp_show_lead_topic() ? esc_html_e('Replies', 'bbpress') : esc_html_e('Posts', 'bbpress'); ?></li> <li class="bbp-topic-freshness"><?php esc_html_e('Last Post', 'bbpress'); ?></li> </ul> </li> <li class="bbp-body"> <?php while (bbp_topics()) : bbp_the_topic(); ?> <?php $this->render_loop_single_topic(); ?> <?php endwhile; ?> </li> <li class="bbp-footer"> <div class="tr"> <p> <span class="td colspan<?php echo (bbp_is_user_home() && (bbp_is_favorites() || bbp_is_subscriptions())) ? '5' : '4'; ?>"> </span> </p> </div> </li> </ul> <?php do_action('bbp_template_after_topics_loop'); } protected function render_pagination_topics() { do_action('bbp_template_before_pagination_loop'); ?> <div class="bbp-pagination"> <div class="bbp-pagination-count"><?php bbp_forum_pagination_count(); ?></div> <div class="bbp-pagination-links"><?php bbp_forum_pagination_links(); ?></div> </div> <?php do_action('bbp_template_after_pagination_loop'); } protected function render_feedback_no_topics() { ?> <div class="bbp-template-notice"> <ul> <li><?php esc_html_e('Oh, bother! No topics were found here.', 'bbpress'); ?></li> </ul> </div> <?php } protected function render_feedback_no_access() { ?> <div id="forum-private" class="bbp-forum-content"> <h1 class="entry-title"><?php esc_html_e('Private', 'bbpress'); ?></h1> <div class="entry-content"> <div class="bbp-template-notice info"> <ul> <li><?php esc_html_e('You do not have permission to view this forum.', 'bbpress'); ?></li> </ul> </div> </div> </div><!-- #forum-private --> <?php } protected function content_single_form() { ?> <div id="bbpress-forums" class="bbpress-wrapper"> <?php bbp_breadcrumb(); ?> <?php bbp_forum_subscription_link(); ?> <?php do_action('bbp_template_before_single_forum'); ?> <?php if (post_password_required()) : ?> <?php $this->render_protected_form(); ?> <?php else : ?> <?php bbp_single_forum_description(); ?> <?php if (bbp_has_forums()) : ?> <?php $this->render_loop_forum(); ?> <?php endif; ?> <?php if (!bbp_is_forum_category() && bbp_has_topics()) : $this->render_pagination_topics(); $this->render_loop_topics(); $this->render_pagination_topics(); $this->render_form_topic(); ?> <?php elseif (!bbp_is_forum_category()) : ?> <?php $this->render_feedback_no_topics(); ?> <?php $this->render_form_topic(); ?> <?php endif; ?> <?php endif; ?> <?php do_action('bbp_template_after_single_forum'); ?> </div> <?php } public function render() { $forum_id = bbpress()->current_forum_id = $this->get_settings_for_display('bbpress_single_id'); if (!bbp_is_forum($forum_id)) { return false; } bbp_set_query_name('bbp_single_forum'); // Start output buffer if (bbp_user_can_view_forum(array('forum_id' => $forum_id))) { $this->content_single_form(); // Forum is private and user does not have caps } elseif (bbp_is_forum_private($forum_id, false)) { $this->render_feedback_no_access(); } wp_reset_postdata(); } } <?php namespace ElementPack\Modules\BbpressSingleForum; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'bbpress-single-forum'; } public function get_widgets() { $widgets = [ 'Bbpress_Single_Forum', ]; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('bbPress Single Forum', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, ]; <?php namespace ElementPack\Modules\ProductGrid\Widgets; use ElementPack\Base\Module_Base; use Elementor\Group_Control_Css_Filter; use Elementor\Repeater; use Elementor\Controls_Manager; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Image_Size; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Text_Shadow; use Elementor\Group_Control_Background; use Elementor\Group_Control_Border; use Elementor\Icons_Manager; use ElementPack\Utils; use ElementPack\Traits\Global_Mask_Controls; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Product_Grid extends Module_Base { use Global_Mask_Controls; public function get_name() { return 'bdt-product-grid'; } public function get_title() { return BDTEP . esc_html__( 'Product Grid', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-product-grid'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'product', 'grid', 'client', 'logo', 'showcase' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-styles' ]; } else { return [ 'ep-font', 'ep-product-grid' ]; } } public function get_custom_help_url() { return 'https://youtu.be/-UJhU-ak5_k'; } protected function register_controls() { $this->start_controls_section( 'ep_section_product', [ 'label' => __( 'Product Items', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $repeater = new Repeater(); $repeater->add_control( 'image', [ 'label' => __( 'Image', 'bdthemes-element-pack' ), 'type' => Controls_Manager::MEDIA, 'default' => [ 'url' => Utils::get_placeholder_image_src(), ], ] ); $repeater->add_control( 'title', [ 'label' => __( 'Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => __( 'product title here', 'bdthemes-element-pack' ), 'label_block' => true, 'dynamic' => [ 'active' => true ], ] ); $repeater->add_control( 'price', [ 'label' => __( 'Price', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true, ], 'default' => '$204', 'label_block' => true, ] ); $repeater->add_control( 'text', [ 'label' => __( 'Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::WYSIWYG, 'dynamic' => [ 'active' => true, ], 'default' => __( 'Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'bdthemes-element-pack' ), 'placeholder' => __( 'Enter your text', 'bdthemes-element-pack' ), ] ); $repeater->add_control( 'readmore_link', [ 'label' => esc_html__( 'Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::URL, 'dynamic' => [ 'active' => true ], 'placeholder' => 'http://your-link.com', 'default' => [ 'url' => '#', ], ] ); $repeater->add_control( 'rating_number', [ 'label' => __( 'Rating', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px' ], 'default' => [ 'size' => 4.5, ], 'range' => [ 'px' => [ 'min' => .5, 'max' => 5, 'step' => .5, ], ], 'dynamic' => [ 'active' => true, ], ] ); $repeater->add_control( 'rating_count', [ 'label' => __( 'Rating Count', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true, ], 'default' => '(10,678)', 'label_block' => true, ] ); $repeater->add_control( 'time', [ 'label' => __( 'Time', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true, ], 'default' => __( '1 hour 10 mins', 'bdthemes-element-pack' ), 'label_block' => true, ] ); $repeater->add_control( 'badge_text', [ 'label' => __( 'Badge Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => 'Sale', 'placeholder' => 'Type Badge text', 'dynamic' => [ 'active' => true, ], ] ); $this->add_control( 'product_items', [ 'show_label' => false, 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(), 'title_field' => '{{{ title }}}', 'default' => [ [ 'title' => __( 'Pizza', 'bdthemes-element-pack' ) ], [ 'title' => __( 'Burger', 'bdthemes-element-pack' ) ], [ 'title' => __( 'Chicken', 'bdthemes-element-pack' ) ], [ 'title' => __( 'Milkshake', 'bdthemes-element-pack' ) ], [ 'title' => __( 'Ice Tea', 'bdthemes-element-pack' ) ], [ 'title' => __( 'Pasta', 'bdthemes-element-pack' ) ], ] ] ); $this->end_controls_section(); $this->start_controls_section( 'section_additional_settings', [ 'label' => __( 'Additional Settings', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_responsive_control( 'columns', [ 'label' => __( 'Columns', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'desktop_default' => 3, 'tablet_default' => 2, 'mobile_default' => 1, 'options' => [ 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid' => 'grid-template-columns: repeat({{SIZE}}, 1fr);', ], ] ); $this->add_responsive_control( 'column_gap', [ 'label' => esc_html__( 'Column Gap', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 20, ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid' => 'grid-column-gap: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'row_gap', [ 'label' => esc_html__( 'Row Gap', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 20, ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid' => 'grid-row-gap: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'show_title', [ 'label' => __( 'Show Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before', ] ); $this->add_control( 'title_tag', [ 'label' => __( 'Title HTML Tag', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'h3', 'options' => element_pack_title_tags(), 'condition' => [ 'show_title' => 'yes', ] ] ); $this->add_control( 'show_price', [ 'label' => __( 'Show Price', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before', ] ); $this->add_control( 'show_time', [ 'label' => __( 'Show Time', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before', ] ); $this->add_control( 'show_text', [ 'label' => __( 'Show Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before', ] ); $this->add_control( 'readmore_link_to', [ 'label' => __( 'Link to', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'button', 'options' => [ 'button' => __( 'Button', 'bdthemes-element-pack' ), 'title' => __( 'Title', 'bdthemes-element-pack' ), 'image' => __( 'Image', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'show_rating', [ 'label' => __( 'Show Rating', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before' ] ); $this->add_control( 'rating_type', [ 'label' => __( 'Rating Type', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'number', 'options' => [ 'star' => __( 'Star', 'bdthemes-element-pack' ), 'number' => __( 'Number', 'bdthemes-element-pack' ), ], 'condition' => [ 'show_rating' => 'yes' ] ] ); $this->add_control( 'badge', [ 'label' => __( 'Badge', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'separator' => 'before', ] ); $this->add_control( 'show_image', [ 'label' => __( 'Show Iamge', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Image_Size::get_type(), [ 'name' => 'thumbnail_size', 'default' => 'medium', 'condition' => [ 'show_image' => 'yes' ] ] ); $this->add_control( 'image_mask_popover', [ 'label' => esc_html__( 'Image Mask', 'bdthemes-element-pack' ), 'type' => Controls_Manager::POPOVER_TOGGLE, 'render_type' => 'template', 'return_value' => 'yes', 'condition' => [ 'show_image' => 'yes' ] ] ); //Global Image Mask Controls $this->register_image_mask_controls(); $this->add_responsive_control( 'text_align', [ 'label' => __( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => __( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-item' => 'text-align: {{VALUE}};', ], 'separator' => 'before' ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_readmore', [ 'label' => esc_html__( 'Read More', 'bdthemes-element-pack' ), 'condition' => [ 'readmore_link_to' => 'button', ], ] ); $this->add_control( 'readmore_text', [ 'label' => esc_html__( 'Read More Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Read More', 'bdthemes-element-pack' ), 'placeholder' => esc_html__( 'Read More', 'bdthemes-element-pack' ), ] ); $this->add_control( 'readmore_icon', [ 'label' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::ICONS, 'label_block' => false, 'skin' => 'inline' ] ); $this->add_control( 'icon_align', [ 'label' => esc_html__( 'Icon Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'default' => 'right', 'toggle' => false, 'options' => [ 'left' => [ 'title' => __( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-left', ], 'right' => [ 'title' => __( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-right', ], ], 'condition' => [ 'readmore_icon[value]!' => '', ], ] ); $this->add_responsive_control( 'icon_indent', [ 'label' => esc_html__( 'Icon Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 8, ], 'range' => [ 'px' => [ 'max' => 50, ], ], 'condition' => [ 'readmore_icon[value]!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-readmore .bdt-button-icon-align-right' => is_rtl() ? 'margin-right: {{SIZE}}{{UNIT}};' : 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-ep-product-grid-readmore .bdt-button-icon-align-left' => is_rtl() ? 'margin-left: {{SIZE}}{{UNIT}};' : 'margin-right: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_badge', [ 'label' => __( 'Badge', 'bdthemes-element-pack' ), 'condition' => [ 'badge' => 'yes', ], ] ); $this->add_control( 'badge_position', [ 'label' => esc_html__( 'Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'top-right', 'options' => element_pack_position(), ] ); $this->add_control( 'badge_offset_toggle', [ 'label' => __( 'Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::POPOVER_TOGGLE, 'label_off' => __( 'None', 'bdthemes-element-pack' ), 'label_on' => __( 'Custom', 'bdthemes-element-pack' ), 'return_value' => 'yes', ] ); $this->start_popover(); $this->add_responsive_control( 'badge_horizontal_offset', [ 'label' => __( 'Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -300, 'step' => 1, 'max' => 300, ], ], 'condition' => [ 'badge_offset_toggle' => 'yes' ], 'render_type' => 'ui', 'selectors' => [ '{{WRAPPER}}' => '--ep-badge-h-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'badge_vertical_offset', [ 'label' => __( 'Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -300, 'step' => 1, 'max' => 300, ], ], 'condition' => [ 'badge_offset_toggle' => 'yes' ], 'render_type' => 'ui', 'selectors' => [ '{{WRAPPER}}' => '--ep-badge-v-offset: {{SIZE}}px;' ], ] ); $this->add_responsive_control( 'badge_rotate', [ 'label' => esc_html__( 'Rotate', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0, ], 'tablet_default' => [ 'size' => 0, ], 'mobile_default' => [ 'size' => 0, ], 'range' => [ 'px' => [ 'min' => -360, 'max' => 360, 'step' => 5, ], ], 'condition' => [ 'badge_offset_toggle' => 'yes' ], 'render_type' => 'ui', 'selectors' => [ '{{WRAPPER}}' => '--ep-badge-rotate: {{SIZE}}deg;' ], ] ); $this->end_popover(); $this->end_controls_section(); //Style $this->start_controls_section( 'section_style_carousel_items', [ 'label' => esc_html__( 'Items', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'content_padding', [ 'label' => esc_html__( 'Content Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->start_controls_tabs( 'tabs_item_style' ); $this->start_controls_tab( 'tab_item_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'item_background', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-item', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'item_border', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-item', 'separator' => 'before', ] ); $this->add_responsive_control( 'item_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'item_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_box_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-item', ] ); $this->add_responsive_control( 'item_shadow_padding', [ 'label' => __( 'Match Padding', 'bdthemes-element-pack' ), 'description' => __( 'You have to add padding for matching overlaping normal/hover box shadow when you used Box Shadow option.', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'step' => 1, 'max' => 50, ] ], 'selectors' => [ '{{WRAPPER}} .swiper-carousel' => 'padding: {{SIZE}}{{UNIT}}; margin: 0 -{{SIZE}}{{UNIT}};' ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_item_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'item_hover_background', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-item:hover', ] ); $this->add_control( 'item_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'item_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-item:hover' => 'border-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_hover_box_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-item:hover', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_image', [ 'label' => __( 'Image', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_image' => 'yes' ] ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'image_border', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-image img' ] ); $this->add_control( 'iamge_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'iamge_padding', [ 'label' => __( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-image img' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'image_spacing', [ 'label' => __( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-image' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-image img', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'img_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-image img' ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_title', [ 'label' => __( 'Title', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_title' => 'yes', ] ] ); $this->add_control( 'title_color', [ 'label' => __( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-title' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'title_bottom_space', [ 'label' => __( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-title' => 'padding-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-title', ] ); $this->add_group_control( Group_Control_Text_Shadow::get_type(), [ 'name' => 'title_shadow', 'label' => __( 'Text Shadow', 'bdthemes-element-pack' ), 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-title', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_price', [ 'label' => __( 'Price', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_price' => 'yes', ] ] ); $this->add_control( 'price_color', [ 'label' => __( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-price' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'price_bottom_space', [ 'label' => __( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-price' => 'padding-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'price_typography', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-price', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_text', [ 'label' => __( 'Text', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_text' => 'yes', ] ] ); $this->add_control( 'text_color', [ 'label' => __( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-text' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'text_typography', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-text', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_readmore', [ 'label' => esc_html__( 'Read More', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'readmore_link_to' => 'button', ], ] ); $this->start_controls_tabs( 'tabs_readmore_style' ); $this->start_controls_tab( 'tab_readmore_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'readmore_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-readmore' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-ep-product-grid-readmore svg' => 'fill: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'readmore_background', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-readmore', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'readmore_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-readmore', 'separator' => 'before', ] ); $this->add_control( 'readmore_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-readmore' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'readmore_box_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-readmore', ] ); $this->add_responsive_control( 'readmore_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-readmore' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'readmore_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-readmore' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'readmore_typography', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-readmore', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_readmore_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'readmore_hover_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-readmore:hover' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-ep-product-grid-readmore:hover svg' => 'fill: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'readmore_hover_background', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-readmore:hover', ] ); $this->add_control( 'readmore_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'readmore_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-readmore:hover' => 'border-color: {{VALUE}};', ], ] ); $this->add_control( 'readmore_hover_animation', [ 'label' => esc_html__( 'Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HOVER_ANIMATION, ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_rating', [ 'label' => esc_html__( 'Rating', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_rating' => 'yes', ], ] ); $this->add_control( 'rating_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#e7e7e7', 'selectors' => [ '{{WRAPPER}} .epsc-rating-item' => 'color: {{VALUE}};', ], 'condition' => [ 'rating_type' => 'star', ], ] ); $this->add_control( 'active_rating_color', [ 'label' => esc_html__( 'Active Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#FFCC00', 'selectors' => [ '{{WRAPPER}} .epsc-rating[class*=" epsc-rating-0"] .epsc-rating-item:nth-child(1) i:after, {{WRAPPER}} .epsc-rating[class*=" epsc-rating-1"] .epsc-rating-item:nth-child(-n+1) i:after, {{WRAPPER}} .epsc-rating[class*=" epsc-rating-2"] .epsc-rating-item:nth-child(-n+2) i:after, {{WRAPPER}} .epsc-rating[class*=" epsc-rating-3"] .epsc-rating-item:nth-child(-n+3) i:after, {{WRAPPER}} .epsc-rating[class*=" epsc-rating-4"] .epsc-rating-item:nth-child(-n+4) i:after, {{WRAPPER}} .epsc-rating[class*=" epsc-rating-5"] .epsc-rating-item:nth-child(-n+5) i:after, .epsc-rating.epsc-rating-0-5 .epsc-rating-item:nth-child(1) i:after, {{WRAPPER}} .epsc-rating.epsc-rating-1-5 .epsc-rating-item:nth-child(2) i:after, {{WRAPPER}} .epsc-rating.epsc-rating-2-5 .epsc-rating-item:nth-child(3) i:after, {{WRAPPER}} .epsc-rating.epsc-rating-3-5 .epsc-rating-item:nth-child(4) i:after, {{WRAPPER}} .epsc-rating.epsc-rating-4-5 .epsc-rating-item:nth-child(5) i:after' => 'color: {{VALUE}};', ], 'condition' => [ 'rating_type' => 'star', ], ] ); $this->add_control( 'rating_number_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#FFCC00', 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-rating' => 'color: {{VALUE}};', ], 'condition' => [ 'rating_type' => 'number', ], ] ); $this->add_control( 'rating_background_color', [ 'label' => __( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-rating' => 'background-color: {{VALUE}};', ], 'condition' => [ 'rating_type' => 'number', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'rating_border', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-rating', 'condition' => [ 'rating_type' => 'number', ], ] ); $this->add_responsive_control( 'rating_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-rating' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'rating_type' => 'number', ], ] ); $this->add_responsive_control( 'rating_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-rating' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'rating_type' => 'number', ], ] ); $this->add_responsive_control( 'rating_margin', [ 'label' => esc_html__( 'Margin', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-rating' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'rating_size', [ 'label' => esc_html__( 'Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-rating' => 'font-size: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'rating_space_between', [ 'label' => esc_html__( 'Space Between', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 10, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-rating i + i' => 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-ep-product-grid-rating span' => 'margin-right: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'rating_count_color', [ 'label' => esc_html__( 'Count Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-rating-count' => 'color: {{VALUE}};', ], 'separator' => 'before' ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'rating_count_typography', 'label' => esc_html__( 'Count Text Typography', 'bdthemes-element-pack' ), 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-rating-count', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_time', [ 'label' => __( 'Time', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_time' => 'yes', ] ] ); $this->add_control( 'time_color', [ 'label' => __( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-time' => 'color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'time_bottom_space', [ 'label' => __( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-time' => 'padding-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'time_typography', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-time', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_badge', [ 'label' => __( 'Badge', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'badge' => 'yes', ], ] ); $this->add_control( 'badge_text_color', [ 'label' => __( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-badge span' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'badge_background', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-badge span', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'badge_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-badge span' ] ); $this->add_responsive_control( 'badge_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-badge span' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'badge_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-badge span', ] ); $this->add_responsive_control( 'badge_padding', [ 'label' => __( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-product-grid-badge span' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'badge_margin', [ 'label' => __( 'Margin', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-interactive-card .bdt-ep-product-grid-badge.bdt-position-small' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'badge_typography', 'selector' => '{{WRAPPER}} .bdt-ep-product-grid-badge span', ] ); $this->end_controls_section(); } public function render_image( $item, $image_key ) { $settings = $this->get_settings_for_display(); if ( ! $settings['show_image'] ) { return; } $thumb_url = Group_Control_Image_Size::get_attachment_image_src( $item['image']['id'], 'thumbnail_size', $settings ); if ( ! $thumb_url ) { $thumb_url = $item['image']['url']; } $this->add_render_attribute( $image_key, 'class', 'bdt-ep-product-grid-image-link bdt-position-z-index', true ); if ( ! empty( $item['readmore_link'] ) ) { $this->add_link_attributes( $image_key, $item['readmore_link'] ); } $image_mask = $settings['image_mask_popover'] == 'yes' ? ' bdt-image-mask' : ''; $this->add_render_attribute( 'image-wrap', 'class', 'bdt-ep-product-grid-image' . $image_mask ); ?> <div <?php $this->print_render_attribute_string( 'image-wrap' ); ?>> <?php $thumb_url = Group_Control_Image_Size::get_attachment_image_src( $item['image']['id'], 'thumbnail_size', $settings ); if ( ! $thumb_url ) { printf( '<img src="%1$s" alt="%2$s">', esc_url( $item['image']['url'] ), esc_html( $item['title'] ) ); } else { print( wp_get_attachment_image( $item['image']['id'], $settings['thumbnail_size_size'], false, [ 'alt' => esc_html( $item['title'] ) ] ) ); } ?> <?php if ( $settings['readmore_link_to'] == 'image' ) : ?> <a <?php $this->print_render_attribute_string( $image_key ); ?>></a> <?php endif; ?> </div> <?php } public function render_title( $item, $title_key ) { $settings = $this->get_settings_for_display(); if ( ! $settings['show_title'] ) { return; } $this->add_render_attribute( $title_key, 'class', 'bdt-ep-product-grid-title-link', true ); if ( ! empty( $item['readmore_link'] ) ) { $this->add_link_attributes( $title_key, $item['readmore_link'] ); } $this->add_render_attribute( 'title-wrap', 'class', 'bdt-ep-product-grid-title', true ); ?> <?php if ( $item['title'] ) : ?> <<?php echo esc_attr( Utils::get_valid_html_tag( $settings['title_tag'] ) ); ?> <?php $this->print_render_attribute_string( 'title-wrap' ); ?>> <?php echo wp_kses( $item['title'], element_pack_allow_tags( 'title' ) ); ?> <?php if ( $settings['readmore_link_to'] == 'title' ) : ?> <a <?php $this->print_render_attribute_string( $title_key ); ?>></a> <?php endif; ?> </<?php echo esc_attr( Utils::get_valid_html_tag( $settings['title_tag'] ) ); ?>> <?php endif; ?> <?php } public function render_price( $item ) { $settings = $this->get_settings_for_display(); if ( ! $settings['show_price'] ) { return; } $this->add_render_attribute( 'price-wrap', 'class', 'bdt-ep-product-grid-price', true ); ?> <?php if ( $item['price'] ) : ?> <div <?php $this->print_render_attribute_string( 'price-wrap' ); ?>> <?php echo wp_kses( $item['price'], element_pack_allow_tags( 'price' ) ); ?> </div> <?php endif; ?> <?php } public function render_time( $item ) { $settings = $this->get_settings_for_display(); if ( ! $settings['show_time'] ) { return; } $this->add_render_attribute( 'time-wrap', 'class', 'bdt-ep-product-grid-time', true ); ?> <?php if ( $item['time'] ) : ?> <div <?php $this->print_render_attribute_string( 'time-wrap' ); ?>> <i class="ep-icon-clock-o" aria-hidden="true"></i> <?php echo wp_kses( $item['time'], element_pack_allow_tags( 'time' ) ); ?> </div> <?php endif; ?> <?php } public function render_text( $item ) { $settings = $this->get_settings_for_display(); if ( ! $settings['show_text'] ) { return; } ?> <?php if ( $item['text'] ) : ?> <div class="bdt-ep-product-grid-text"> <?php echo wp_kses_post( $item['text'] ); ?> </div> <?php endif; ?> <?php } public function render_readmore( $item, $readmore_key ) { $settings = $this->get_settings_for_display(); $this->add_render_attribute( [ $readmore_key => [ 'class' => [ 'bdt-ep-product-grid-readmore', $settings['readmore_hover_animation'] ? 'elementor-animation-' . $settings['readmore_hover_animation'] : '', ], ] ], '', '', true ); if ( ! empty( $item['readmore_link'] ) ) { $this->add_link_attributes( $readmore_key, $item['readmore_link'] ); } ?> <?php if ( ( ! empty( $item['readmore_link']['url'] ) ) && ( $settings['readmore_link_to'] == 'button' ) ) : ?> <div class="bdt-ep-product-grid-readmore-wrap"> <a <?php $this->print_render_attribute_string( $readmore_key ); ?>> <?php echo esc_html( $settings['readmore_text'] ); ?> <?php if ( $settings['readmore_icon']['value'] ) : ?> <span class="bdt-button-icon-align-<?php echo esc_attr( $settings['icon_align'] ); ?>"> <?php Icons_Manager::render_icon( $settings['readmore_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); ?> </span> <?php endif; ?> </a> </div> <?php endif; ?> <?php } public function render_review_rating( $item ) { $settings = $this->get_settings_for_display(); if ( ! $settings['show_rating'] ) { return; } $rating_number = $item['rating_number']['size']; if ( preg_match( '/\./', $rating_number ) ) { $ratingValue = explode( ".", $rating_number ); $firstVal = ( $ratingValue[0] <= 5 ) ? $ratingValue[0] : 5; $secondVal = ( $ratingValue[1] < 5 ) ? 0 : 5; } else { $firstVal = ( $rating_number <= 5 ) ? $rating_number : 5; $secondVal = 0; } $score = $firstVal . '-' . $secondVal; ?> <div> <div class="bdt-ep-product-grid-rating bdt-flex-inline bdt-flex-middle bdt-<?php echo esc_attr( $settings['rating_type'] ) ?>"> <?php if ( $settings['rating_type'] === 'number' ) : ?> <span> <?php echo esc_html( $item['rating_number']['size'] ); ?> </span> <i class="ep-icon-star-full" aria-hidden="true"></i> <?php else : ?> <span class="epsc-rating epsc-rating-<?php echo esc_attr( $score ); ?>"> <span class="epsc-rating-item"><i class="ep-icon-star" aria-hidden="true"></i></span> <span class="epsc-rating-item"><i class="ep-icon-star" aria-hidden="true"></i></span> <span class="epsc-rating-item"><i class="ep-icon-star" aria-hidden="true"></i></span> <span class="epsc-rating-item"><i class="ep-icon-star" aria-hidden="true"></i></span> <span class="epsc-rating-item"><i class="ep-icon-star" aria-hidden="true"></i></span> </span> <?php endif; ?> </div> <span class="bdt-ep-product-grid-rating-count"> <?php echo esc_html( $item['rating_count'] ); ?> </span> </div> <?php } public function render_badge( $item ) { $settings = $this->get_settings_for_display(); ?> <?php if ( $settings['badge'] and '' != $item['badge_text'] ) : ?> <div class="bdt-ep-product-grid-badge bdt-position-small bdt-position-<?php echo esc_attr( $settings['badge_position'] ); ?>"> <span class="bdt-badge bdt-padding-small"> <?php echo esc_html( $item['badge_text'] ); ?> </span> </div> <?php endif; ?> <?php } protected function render() { $settings = $this->get_settings_for_display(); if ( empty( $settings['product_items'] ) ) { return; } $this->add_render_attribute( 'product-grid', 'class', 'bdt-ep-product-grid' ); ?> <div <?php $this->print_render_attribute_string( 'product-grid' ); ?>> <?php foreach ( $settings['product_items'] as $index => $item ) : $this->add_render_attribute( 'item-wrap', 'class', 'bdt-ep-product-grid-item', true ); ?> <div <?php $this->print_render_attribute_string( 'item-wrap' ); ?>> <?php $this->render_image( $item, 'image_' . $index ); ?> <div class="bdt-ep-product-grid-content"> <div class="bdt-ep-product-grid-title-price bdt-flex bdt-flex-middle bdt-flex-between"> <?php $this->render_title( $item, 'title_' . $index ); ?> <?php $this->render_price( $item ); ?> </div> <?php $this->render_text( $item ); ?> <?php $this->render_readmore( $item, 'link_' . $index ); ?> <div class="bdt-ep-product-grid-rating-time bdt-flex bdt-flex-middle bdt-flex-between"> <?php $this->render_review_rating( $item ); ?> <?php $this->render_time( $item ); ?> </div> </div> <?php $this->render_badge( $item ); ?> </div> <?php endforeach; ?> </div> <?php } } <?php namespace ElementPack\Modules\ProductGrid; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'product-grid'; } public function get_widgets() { $widgets = [ 'Product_Grid', ]; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Product Grid', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => false, 'has_style' => true, ]; <?php namespace ElementPack\Modules\AdvancedButton\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Background; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Box_Shadow; use Elementor\Icons_Manager; if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } class Advanced_Button extends Module_Base { public function get_name() { return 'bdt-advanced-button'; } public function get_title() { return BDTEP . esc_html__( 'Advanced Button', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-advanced-button'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'button', 'advanced', 'link' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-styles' ]; } else { return [ 'ep-advanced-button' ]; } } public function get_custom_help_url() { return 'https://youtu.be/Lq_st2IWZiE'; } protected function register_controls() { $this->start_controls_section( 'section_button', [ 'label' => esc_html__( 'Button', 'bdthemes-element-pack' ), ] ); $this->add_control( 'text', [ 'label' => esc_html__( 'Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true ], 'default' => esc_html__( 'Click me', 'bdthemes-element-pack' ), 'placeholder' => esc_html__( 'Click me', 'bdthemes-element-pack' ), ] ); $this->add_control( 'link', [ 'label' => esc_html__( 'Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::URL, 'dynamic' => [ 'active' => true ], 'placeholder' => esc_html__( 'https://your-link.com', 'bdthemes-element-pack' ), 'default' => [ 'url' => '#', ], ] ); $this->add_control( 'add_custom_attributes', [ 'label' => __( 'Add Custom Attributes', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'custom_attributes', [ 'label' => __( 'Custom Attributes', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXTAREA, 'dynamic' => [ 'active' => true, ], 'placeholder' => __( 'key|value', 'bdthemes-element-pack' ), 'description' => sprintf( __( 'Set custom attributes for the price table button tag. Each attribute in a separate line. Separate attribute key from the value using %s character.', 'bdthemes-element-pack' ), '<code>|</code>' ), 'classes' => 'elementor-control-direction-ltr', 'condition' => [ 'add_custom_attributes' => 'yes' ] ] ); $this->add_control( 'button_size', [ 'label' => esc_html__( 'Button Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'md', 'options' => [ 'xs' => esc_html__( 'Extra Small', 'bdthemes-element-pack' ), 'sm' => esc_html__( 'Small', 'bdthemes-element-pack' ), 'md' => esc_html__( 'Medium', 'bdthemes-element-pack' ), 'lg' => esc_html__( 'Large', 'bdthemes-element-pack' ), 'xl' => esc_html__( 'Extra Large', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'onclick', [ 'label' => esc_html__( 'OnClick', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'onclick_event', [ 'label' => esc_html__( 'OnClick Event', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'placeholder' => 'myFunction()', 'description' => sprintf( esc_html__( 'For details please look <a href="%s" target="_blank">here</a>' ), 'https://www.w3schools.com/jsref/event_onclick.asp' ), 'condition' => [ 'onclick' => 'yes' ] ] ); $this->add_responsive_control( 'align', [ 'label' => esc_html__( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'prefix_class' => 'elementor%s-align-', 'default' => '', 'options' => [ 'left' => [ 'title' => __( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => __( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], ] ); $this->add_control( 'button_icon', [ 'label' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::ICONS, 'fa4compatibility' => 'icon', 'label_block' => false, 'skin' => 'inline' ] ); $this->add_control( 'icon_align_choose', [ 'label' => esc_html__( 'Icon & Text Align', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::CHOOSE, 'default' => 'center', 'options' => [ 'center' => [ 'title' => esc_html__( 'Both Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-justify-center-h', ], 'space-between' => [ 'title' => esc_html__( 'Space Between', 'bdthemes-element-pack' ), 'icon' => 'eicon-justify-space-between-h', ], 'left-right' => [ 'title' => esc_html__( 'Icon Left/Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-grow', ], ], 'condition' => [ 'button_icon[value]!' => '', 'icon_align' => [ 'left', 'right' ] ], 'selectors_dictionary' => [ 'center' => 'text-align: center;', 'space-between' => 'justify-content: space-between;', 'left-right' => 'flex-grow: 1;', ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-text' => '{{VALUE}};', '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-content-wrapper' => '{{VALUE}};', ], 'render_type' => 'template' ] ); $this->add_control( 'icon_align', [ 'label' => esc_html__( 'Icon Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'right', 'options' => [ 'left' => esc_html__( 'Left', 'bdthemes-element-pack' ), 'right' => esc_html__( 'Right', 'bdthemes-element-pack' ), 'top' => esc_html__( 'Top', 'bdthemes-element-pack' ), 'bottom' => esc_html__( 'Bottom', 'bdthemes-element-pack' ), ], 'condition' => [ 'button_icon[value]!' => '', ], ] ); $this->add_control( 'icon_indent', [ 'label' => esc_html__( 'Icon Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 100, ], ], 'default' => [ 'size' => 8, ], 'condition' => [ 'button_icon[value]!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button .bdt-flex-align-right' => is_rtl() ? 'margin-right: {{SIZE}}{{UNIT}};' : 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-ep-button .bdt-flex-align-left' => is_rtl() ? 'margin-left: {{SIZE}}{{UNIT}};' : 'margin-right: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-ep-button .bdt-flex-align-top' => 'margin-bottom: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-ep-button .bdt-flex-align-bottom' => 'margin-top: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'show_button_badge', [ 'label' => esc_html__( 'Show Badge', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'separator' => 'before', ] ); $this->add_control( 'badge_text', [ 'label' => __( 'Badge Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => __( 'Badge', 'bdthemes-element-pack' ), 'dynamic' => [ 'active' => true, ], 'condition' => [ 'show_button_badge' => 'yes', ], ] ); $this->add_control( 'badge_align', [ 'label' => esc_html__( 'Badge Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'toggle' => false, 'default' => 'right', 'options' => [ 'left' => [ 'title' => __( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-left', ], 'right' => [ 'title' => __( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-h-align-right', ], ], 'condition' => [ 'badge_text[value]!' => '', 'show_button_badge' => 'yes', ], ] ); $this->add_control( 'badge_indent', [ 'label' => esc_html__( 'Badge Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 100, ], ], 'default' => [ 'size' => 8, ], 'condition' => [ 'badge_text[value]!' => '', 'show_button_badge' => 'yes', ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button-badge.bdt-flex-align-right' => is_rtl() ? 'margin-right: {{SIZE}}{{UNIT}};' : 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-ep-button-badge.bdt-flex-align-left' => is_rtl() ? 'margin-left: {{SIZE}}{{UNIT}};' : 'margin-right: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'button_css_id', [ 'label' => __( 'Button ID', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true, ], 'default' => '', 'title' => __( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'bdthemes-element-pack' ), 'description' => __( 'Please make sure the ID is unique and not used elsewhere on the page this form is displayed. This field allows <code>A-z 0-9</code> & underscore chars without spaces.', 'bdthemes-element-pack' ), 'separator' => 'before', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_style', [ 'label' => esc_html__( 'Style', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'button_effect', [ 'label' => esc_html__( 'Effect', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'a', 'options' => [ 'a' => esc_html__( 'Effect A', 'bdthemes-element-pack' ), 'b' => esc_html__( 'Effect B', 'bdthemes-element-pack' ), 'c' => esc_html__( 'Effect C', 'bdthemes-element-pack' ), 'd' => esc_html__( 'Effect D', 'bdthemes-element-pack' ), 'e' => esc_html__( 'Effect E', 'bdthemes-element-pack' ), 'f' => esc_html__( 'Effect F', 'bdthemes-element-pack' ), 'g' => esc_html__( 'Effect G', 'bdthemes-element-pack' ), 'h' => esc_html__( 'Effect H', 'bdthemes-element-pack' ), 'i' => esc_html__( 'Effect I', 'bdthemes-element-pack' ), ], 'render_type' => 'template', ] ); $this->add_control( 'attention_button', [ 'label' => esc_html__( 'Attention', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, ] ); $this->start_controls_tabs( 'tabs_advanced_button_style' ); $this->start_controls_tab( 'tab_advanced_button_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'advanced_button_text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-button' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'button_background', 'types' => [ 'classic', 'gradient' ], 'selector' => '{{WRAPPER}} .bdt-ep-button, {{WRAPPER}} .bdt-ep-button.bdt-ep-button-effect-i .bdt-ep-button-content-wrapper:after, {{WRAPPER}} .bdt-ep-button.bdt-ep-button-effect-i .bdt-ep-button-content-wrapper:before, {{WRAPPER}} .bdt-ep-button.bdt-ep-button-effect-h:hover', ] ); $this->add_control( 'button_border_style', [ 'label' => esc_html__( 'Border Style', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'none' => esc_html__( 'None', 'bdthemes-element-pack' ), 'solid' => esc_html__( 'Solid', 'bdthemes-element-pack' ), 'dotted' => esc_html__( 'Dotted', 'bdthemes-element-pack' ), 'dashed' => esc_html__( 'Dashed', 'bdthemes-element-pack' ), 'groove' => esc_html__( 'Groove', 'bdthemes-element-pack' ), ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button' => 'border-style: {{VALUE}};', ], ] ); $this->add_responsive_control( 'button_border_width', [ 'label' => esc_html__( 'Border Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'default' => [ 'top' => 3, 'right' => 3, 'bottom' => 3, 'left' => 3, ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button' => 'border-width: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'button_border_style!' => 'none' ] ] ); $this->add_control( 'button_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#666', 'selectors' => [ '{{WRAPPER}} .bdt-ep-button' => 'border-color: {{VALUE}};', ], 'condition' => [ 'button_border_style!' => 'none' ], ] ); $this->add_responsive_control( 'advanced_button_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'advanced_button_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'advanced_button_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-button', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'advanced_button_typography', 'selector' => '{{WRAPPER}} .bdt-ep-button', ] ); //button width slider control $this->add_responsive_control( 'button_width', [ 'label' => esc_html__( 'Button Max Width', 'bdthemes-element-pack' ) . BDTEP_NC, 'description' => esc_html__( 'Set button max width in px or %, default width is 100% and alignment justify value also same max width.', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ '%', 'px' ], 'range' => [ '%' => [ 'min' => 10, 'max' => 100, ], 'px' => [ 'min' => 100, 'max' => 1000, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button' => 'max-width: {{SIZE}}{{UNIT}}; width: 100%;', ], 'separator' => 'before', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_advanced_button_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'advanced_button_hover_text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-button:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'button_hover_background', 'types' => [ 'classic', 'gradient' ], 'selector' => '{{WRAPPER}} .bdt-ep-button:after, {{WRAPPER}} .bdt-ep-button:hover, {{WRAPPER}} .bdt-ep-button.bdt-ep-button-effect-i, {{WRAPPER}} .bdt-ep-button.bdt-ep-button-effect-h:after', ] ); $this->add_control( 'button_hover_border_style', [ 'label' => esc_html__( 'Border Style', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'none' => esc_html__( 'None', 'bdthemes-element-pack' ), 'solid' => esc_html__( 'Solid', 'bdthemes-element-pack' ), 'dotted' => esc_html__( 'Dotted', 'bdthemes-element-pack' ), 'dashed' => esc_html__( 'Dashed', 'bdthemes-element-pack' ), 'groove' => esc_html__( 'Groove', 'bdthemes-element-pack' ), ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button:hover' => 'border-style: {{VALUE}};', ], ] ); $this->add_responsive_control( 'button_hover_border_width', [ 'label' => esc_html__( 'Border Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'default' => [ 'top' => 3, 'right' => 3, 'bottom' => 3, 'left' => 3, ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button:hover' => 'border-width: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'button_hover_border_style!' => 'none' ] ] ); $this->add_control( 'button_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-button:hover' => 'border-color: {{VALUE}};', ], 'condition' => [ 'button_hover_border_style!' => 'none' ] ] ); $this->add_responsive_control( 'advanced_button_hover_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'advanced_button_hover_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-button:hover', ] ); $this->add_control( 'hover_animation', [ 'label' => esc_html__( 'Hover Animation', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HOVER_ANIMATION, ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_icon', [ 'label' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'button_icon[value]!' => '', ], ] ); $this->start_controls_tabs( 'tabs_advanced_button_icon_style' ); $this->start_controls_tab( 'tab_advanced_button_icon_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'advanced_button_icon_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-icon' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-icon svg' => 'fill: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'advanced_button_icon_background', 'selector' => '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-icon .bdt-ep-button-icon-inner', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'advanced_button_icon_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-icon .bdt-ep-button-icon-inner', ] ); $this->add_responsive_control( 'advanced_button_icon_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-icon .bdt-ep-button-icon-inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'advanced_button_icon_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-icon .bdt-ep-button-icon-inner' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'advanced_button_icon_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-icon .bdt-ep-button-icon-inner', ] ); $this->add_responsive_control( 'advanced_button_icon_size', [ 'label' => __( 'Icon Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 10, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-icon .bdt-ep-button-icon-inner' => 'font-size: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_advanced_button_icon_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'advanced_button_hover_icon_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-button:hover .bdt-ep-button-icon' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-ep-button:hover .bdt-ep-button-icon svg' => 'fill: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'advanced_button_icon_hover_background', 'selector' => '{{WRAPPER}} .bdt-ep-button:hover .bdt-ep-button-icon .bdt-ep-button-icon-inner', ] ); $this->add_control( 'icon_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'advanced_button_icon_border_border!' => '' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button:hover .bdt-ep-button-icon .bdt-ep-button-icon-inner' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_badge', [ 'label' => esc_html__( 'Badge', 'bdthemes-element-pack' ) . BDTEP_NC, 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'badge_text[value]!' => '', 'show_button_badge' => 'yes' ], ] ); $this->start_controls_tabs( 'tabs_badge_style' ); $this->start_controls_tab( 'tab_badge_normal', [ 'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'badge_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-badge-inner' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'badge_background', 'selector' => '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-badge-inner', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'badge_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-badge-inner', ] ); $this->add_responsive_control( 'badge_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-badge-inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'badge_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-badge-inner' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'badge_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-badge-inner', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'badge_typography', 'selector' => '{{WRAPPER}} .bdt-ep-button .bdt-ep-button-badge-inner', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_badge_hover', [ 'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'badge_hover_icon_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-button:hover .bdt-ep-button-badge-inner' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'badge_hover_background', 'selector' => '{{WRAPPER}} .bdt-ep-button:hover .bdt-ep-button-badge-inner', ] ); $this->add_control( 'badge_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'condition' => [ 'badge_border_border!' => '' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-button:hover .bdt-ep-button-badge-inner' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } public function render_text() { $settings = $this->get_settings_for_display(); $this->add_render_attribute( 'content-wrapper', 'class', 'bdt-ep-button-content-wrapper' ); //if ( 'left' == $settings['icon_align'] or 'right' == $settings['icon_align'] ) { $this->add_render_attribute( 'content-wrapper', 'class', 'bdt-flex bdt-flex-middle bdt-flex-center' ); //} $this->add_render_attribute( 'content-wrapper', 'class', ( 'top' == $settings['icon_align'] ) ? 'bdt-flex bdt-flex-column' : '' ); $this->add_render_attribute( 'content-wrapper', 'class', ( 'bottom' == $settings['icon_align'] ) ? 'bdt-flex bdt-flex-column-reverse' : '' ); $this->add_render_attribute( 'content-wrapper', 'data-text', esc_attr( $settings['text'] ) ); $this->add_render_attribute( 'icon-align', 'class', 'elementor-align-icon-' . $settings['icon_align'] ); $this->add_render_attribute( 'icon-align', 'class', 'bdt-ep-button-icon' ); $this->add_render_attribute( 'text', 'class', 'bdt-ep-button-text' ); $this->add_inline_editing_attributes( 'text', 'none' ); $migrated = isset( $settings['__fa4_migrated']['button_icon'] ); $is_new = empty( $settings['icon'] ) && Icons_Manager::is_migration_allowed(); ?> <div <?php $this->print_render_attribute_string( 'content-wrapper' ); ?>> <?php if ( ! empty( $settings['button_icon']['value'] ) ) : ?> <div class="bdt-ep-button-icon bdt-flex-center bdt-flex-align-<?php echo esc_attr( $settings['icon_align'] ); ?>"> <div class="bdt-ep-button-icon-inner"> <?php if ( $is_new || $migrated ) : Icons_Manager::render_icon( $settings['button_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ] ); else : ?> <i class="<?php echo esc_attr( $settings['icon'] ); ?>" aria-hidden="true"></i> <?php endif; ?> </div> </div> <?php endif; ?> <div <?php $this->print_render_attribute_string( 'text' ); ?>> <span class="avdbtn-text"> <?php echo esc_html( $settings['text'] ); ?> </span> <?php if ( 'g' == $settings['button_effect'] ) : ?> <span class="avdbtn-alt-text"> <?php echo esc_html( $settings['text'] ); ?> </span> <?php endif; ?> </div> <?php if ( $settings['show_button_badge'] == 'yes' ) : ?> <div class="bdt-ep-button-badge bdt-flex-center bdt-flex-align-<?php echo esc_attr( $settings['badge_align'] ); ?>"> <div class="bdt-ep-button-badge-inner"> <?php echo esc_html( $settings['badge_text'] ); ?> </div> </div> <?php endif; ?> </div> <?php } protected function render() { $settings = $this->get_settings_for_display(); $this->add_render_attribute( 'wrapper', 'class', 'bdt-ep-button-wrapper' ); if ( ! empty( $settings['link']['url'] ) ) { $this->add_link_attributes( 'advanced_button', $settings['link'] ); } else { $this->add_render_attribute( 'advanced_button', 'href', 'javascript:void(0);' ); } if ( $settings['onclick'] ) { $this->add_render_attribute( 'advanced_button', 'onclick', $settings['onclick_event'] ); } if ( $settings['add_custom_attributes'] and ! empty( $settings['custom_attributes'] ) ) { $attributes = explode( "\n", $settings['custom_attributes'] ); $reserved_attr = [ 'href', 'target' ]; foreach ( $attributes as $attribute ) { if ( ! empty( $attribute ) ) { $attr = explode( '|', $attribute, 2 ); if ( ! isset( $attr[1] ) ) { $attr[1] = ''; } if ( ! in_array( strtolower( $attr[0] ), $reserved_attr ) ) { $this->add_render_attribute( 'advanced_button', trim( $attr[0] ), trim( $attr[1] ) ); } } } } if ( $settings['attention_button'] ) { $this->add_render_attribute( 'advanced_button', 'class', 'bdt-ep-attention-button' ); } $this->add_render_attribute( 'advanced_button', 'class', 'bdt-ep-button' ); $this->add_render_attribute( 'advanced_button', 'class', 'bdt-ep-button-effect-' . esc_attr( $settings['button_effect'] ) ); $this->add_render_attribute( 'advanced_button', 'class', 'bdt-ep-button-size-' . esc_attr( $settings['button_size'] ) ); if ( $settings['hover_animation'] ) { $this->add_render_attribute( 'advanced_button', 'class', 'elementor-animation-' . $settings['hover_animation'] ); } if ( ! empty( $settings['button_css_id'] ) ) { $this->add_render_attribute( 'advanced_button', 'id', $settings['button_css_id'] ); } ?> <div <?php $this->print_render_attribute_string( 'wrapper' ); ?>> <a <?php $this->print_render_attribute_string( 'advanced_button' ); ?>> <?php $this->render_text(); ?> </a> </div> <?php } protected function content_template() { ?> <# view.addRenderAttribute( 'text' , 'class' , 'bdt-ep-button-text' ); view.addInlineEditingAttributes( 'text' , 'none' ); view.addRenderAttribute( 'button' , 'onclick' , settings.onclick_event ); var animation=(settings.hover_animation) ? ' elementor-animation-' + settings.hover_animation : '' ; var attention=(settings.attention_button) ? ' bdt-ep-attention-button' : '' ; view.addRenderAttribute( 'content-wrapper' , 'class' , 'bdt-ep-button-content-wrapper' ); if (settings.icon_align=='left' || settings.icon_align=='right' ) { view.addRenderAttribute( 'content-wrapper' , 'class' , 'bdt-flex bdt-flex-middle bdt-flex-center' ); } if (settings.icon_align=='top' ) { view.addRenderAttribute( 'content-wrapper' , 'class' , 'bdt-flex bdt-flex-column' ); } if (settings.icon_align=='bottom' ) { view.addRenderAttribute( 'content-wrapper' , 'class' , 'bdt-flex bdt-flex-column-reverse' ); } view.addRenderAttribute( 'content-wrapper' , 'data-text' , settings.readmore_text); var iconHTML=elementor.helpers.renderIcon( view, settings.button_icon, { 'aria-hidden' : true }, 'i' , 'object' ); var migrated=elementor.helpers.isIconMigrated( settings, 'button_icon' ); #> <div class="bdt-ep-button-wrapper"> <a id="{{ settings.button_css_id }}" class="bdt-ep-button bdt-ep-button-effect-{{ settings.button_effect }} bdt-ep-button-size-{{ settings.button_size }}{{animation}}{{attention}}" href="{{ settings.link.url }}" role="button" {{{ view.getRenderAttributeString( 'button' ) }}}> <div {{{ view.getRenderAttributeString( 'content-wrapper' ) }}}> <# if ( settings.button_icon.value ) { #> <div class="bdt-ep-button-icon bdt-flex-center bdt-flex-align-{{ settings.icon_align }}"> <div class="bdt-ep-button-icon-inner"> <# if ( iconHTML && iconHTML.rendered && ( ! settings.icon || migrated ) ) { #> {{{ iconHTML.value }}} <# } else { #> <i class="{{ settings.icon }}" aria-hidden="true"></i> <# } #> </div> </div> <# } #> <div {{{ view.getRenderAttributeString( 'text' ) }}}>{{{ settings.text }}}</div> <# if ( settings.show_button_badge ) { #> <div class="bdt-ep-button-badge bdt-flex-center bdt-flex-align-{{ settings.badge_align }}"> <div class="bdt-ep-button-badge-inner"> {{{ settings.badge_text }}} </div> </div> <# } #> </div> </a> </div> <?php } } <?php namespace ElementPack\Modules\AdvancedButton; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'advanced-button'; } public function get_widgets() { $widgets = ['Advanced_Button']; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Advanced Button', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_style' => true, 'has_script' => false, ]; <?php namespace ElementPack\Modules\EddTabs\Widgets; use Elementor\Repeater; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Border; use Elementor\Group_Control_Background; use Elementor\Group_Control_Box_Shadow; use Elementor\Icons_Manager; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class EDD_Tabs extends Module_Base { public function get_name() { return 'bdt-edd-tabs'; } public function get_title() { return BDTEP . esc_html__('EDD Tabs', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-edd-tabs bdt-new'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['tabs', 'toggle', 'accordion']; } public function is_reload_preview_required() { return false; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-edd-tabs']; } } public function get_script_depends() { if ($this->ep_is_edit_mode()) { return ['ep-scripts']; } else { return ['ep-edd-tabs']; } } public function get_custom_help_url() { return 'https://youtu.be/1BmS_8VpBF4'; } protected function register_controls() { $this->start_controls_section( 'section_title', [ 'label' => __('EDD Tabs', 'bdthemes-element-pack'), ] ); $repeater = new Repeater(); $repeater->add_control( 'tab_title', [ 'label' => esc_html__('Title', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Tab Title', 'bdthemes-element-pack'), 'placeholder' => esc_html__('Tab Title', 'bdthemes-element-pack'), 'label_block' => true, ] ); $repeater->add_control( 'tab_content', [ 'label' => esc_html__('Tab Content', 'bdthemes-element-pack'), 'default' => esc_html__('Tab Content', 'bdthemes-element-pack'), 'placeholder' => esc_html__('Tab Content', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'label_block' => true, 'options' => [ 'download_history' => esc_html__('Download History', 'bdthemes-element-pack'), 'download_discounts' => esc_html__('Download Discounts', 'bdthemes-element-pack'), 'purchase_history' => esc_html__('Purchase History', 'bdthemes-element-pack'), 'edd_profile_editor' => esc_html__('Profile Editor', 'bdthemes-element-pack'), 'edd_subscriptions' => esc_html__('Subscriptions', 'bdthemes-element-pack'), 'edd_wish_lists' => esc_html__('Wishlist Items', 'bdthemes-element-pack'), 'edd_wish_lists_edit' => esc_html__('Edit Wishlist Items', 'bdthemes-element-pack'), 'edd_deposit' => esc_html__('Deposits', 'bdthemes-element-pack'), 'edd_license_keys' => esc_html__('License Keys', 'bdthemes-element-pack') ], 'default' => 'purchase_history' ] ); $repeater->add_control( 'tab_select_icon', [ 'label' => __('Icon', 'bdthemes-element-pack'), 'type' => Controls_Manager::ICONS, 'fa4compatibility' => 'tab_icon', ] ); $this->add_control( 'tabs', [ 'label' => esc_html__('Tabs Items', 'bdthemes-element-pack'), 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(), 'default' => [ [ 'tab_title' => esc_html__('Purchase History', 'bdthemes-element-pack'), 'tab_content' => 'purchase_history', ], [ 'tab_title' => esc_html__('Download History', 'bdthemes-element-pack'), 'tab_content' => 'download_history', ], ], 'title_field' => '{{{ tab_title }}}', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_layout', [ 'label' => __('Layout', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'tab_layout', [ 'label' => esc_html__('Layout', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'default', 'options' => [ 'default' => esc_html__('Top (Default)', 'bdthemes-element-pack'), 'bottom' => esc_html__('Bottom', 'bdthemes-element-pack'), 'left' => esc_html__('Left', 'bdthemes-element-pack'), 'right' => esc_html__('Right', 'bdthemes-element-pack'), ], ] ); $this->add_control( 'align', [ 'label' => __('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ '' => [ 'title' => __('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => __('Justified', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-justify', ], ], 'condition' => [ 'tab_layout' => ['default', 'bottom'] ], ] ); $this->add_responsive_control( 'item_spacing', [ 'label' => __('Nav Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item' => 'padding-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-tab' => 'margin-left: -{{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-tab.bdt-tab-left .bdt-tabs-item, {{WRAPPER}} .bdt-tab.bdt-tab-right .bdt-tabs-item' => 'padding-top: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-tab.bdt-tab-left, {{WRAPPER}} .bdt-tab.bdt-tab-right' => 'margin-top: -{{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'nav_spacing', [ 'label' => __('Nav Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 50, 'max' => 500, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-grid:not(.bdt-grid-stack) .bdt-tab-wrapper' => 'width: {{SIZE}}{{UNIT}};', ], 'condition' => [ 'tab_layout' => ['left', 'right'] ], ] ); $this->add_responsive_control( 'content_spacing', [ 'label' => __('Content Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'default' => [ 'size' => 20, ], 'selectors' => [ '{{WRAPPER}} .bdt-tabs-default .bdt-switcher-wrapper' => 'margin-top: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-tabs-bottom .bdt-switcher-wrapper' => 'margin-bottom: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-tabs-left .bdt-grid:not(.bdt-grid-stack) .bdt-switcher-wrapper' => 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-tabs-right .bdt-grid:not(.bdt-grid-stack) .bdt-switcher-wrapper' => 'margin-right: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-tabs-left .bdt-grid-stack .bdt-switcher-wrapper, {{WRAPPER}} .bdt-tabs-right .bdt-grid-stack .bdt-switcher-wrapper' => 'margin-top: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_additional', [ 'label' => __('Additional', 'bdthemes-element-pack'), ] ); $this->add_control( 'active_item', [ 'label' => __('Active Item No', 'bdthemes-element-pack'), 'type' => Controls_Manager::NUMBER, 'min' => 1, 'max' => 20, ] ); $this->add_control( 'tab_transition', [ 'label' => esc_html__('Transition', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => element_pack_transition_options(), 'default' => '', ] ); $this->add_control( 'duration', [ 'label' => __('Animation Duration', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 1, 'max' => 501, 'step' => 50, ], ], 'default' => [ 'size' => 200, ], 'condition' => [ 'tab_transition!' => '' ], ] ); $this->add_control( 'media', [ 'label' => __('Turn On Horizontal mode', 'bdthemes-element-pack'), 'description' => __('It means that tabs nav will switch vertical to horizontal on mobile mode', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 960 => [ 'title' => __('On Tablet', 'bdthemes-element-pack'), 'icon' => 'eicon-device-tablet', ], 768 => [ 'title' => __('On Mobile', 'bdthemes-element-pack'), 'icon' => 'eicon-device-mobile', ], ], 'default' => 960, 'condition' => [ 'tab_layout' => ['left', 'right'] ], ] ); $this->add_control( 'nav_sticky_mode', [ 'label' => esc_html__('Tabs Nav Sticky', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'tab_layout!' => 'bottom', ], ] ); $this->add_control( 'nav_sticky_offset', [ 'label' => esc_html__('Offset', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'condition' => [ 'nav_sticky_mode' => 'yes', 'tab_layout!' => 'bottom', ], ] ); $this->add_control( 'nav_sticky_on_scroll_up', [ 'label' => esc_html__('Sticky on Scroll Up', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'description' => esc_html__('Set sticky options when you scroll up your mouse.', 'bdthemes-element-pack'), 'condition' => [ 'nav_sticky_mode' => 'yes', 'tab_layout!' => 'bottom', ], ] ); $this->add_control( 'fullwidth_on_mobile', [ 'label' => esc_html__('Fullwidth Nav on Mobile', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'description' => esc_html__('If you have long test tab so this can help design issue.', 'bdthemes-element-pack') ] ); $this->add_control( 'swiping_on_mobile', [ 'label' => esc_html__('Swiping Tab on Mobile', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'description' => esc_html__('If you set yes so tab will swiping on mobile device by touch.', 'bdthemes-element-pack'), 'default' => 'yes', ] ); $this->add_control( 'active_hash', [ 'label' => esc_html__('Hash Location', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'no', ] ); $this->add_control( 'hash_top_offset', [ 'label' => esc_html__('Top Offset ', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px', ''], 'range' => [ 'px' => [ 'min' => 1, 'max' => 1000, 'step' => 5, ], ], 'default' => [ 'unit' => 'px', 'size' => 70, ], 'condition' => [ 'active_hash' => 'yes', 'nav_sticky_mode!' => 'yes', ], ] ); $this->add_control( 'hash_scrollspy_time', [ 'label' => esc_html__('Scrollspy Time', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['ms', ''], 'range' => [ 'px' => [ 'min' => 500, 'max' => 5000, 'step' => 1000, ], ], 'default' => [ 'unit' => 'px', 'size' => 1500, ], 'condition' => [ 'active_hash' => 'yes', 'nav_sticky_mode!' => 'yes', ], ] ); $this->add_control( 'tabs_match_height', [ 'label' => esc_html__('Equal Tab Height', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'description' => esc_html__('You can on/off tab item equal height feature.', 'bdthemes-element-pack'), 'default' => 'yes', 'separator' => 'before', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_toggle_style_title', [ 'label' => __('Tab', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_title_style'); $this->start_controls_tab( 'tab_title_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'title_background', 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} .bdt-tab .bdt-tabs-item-title', 'separator' => 'after', ] ); $this->add_control( 'title_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item-title' => 'color: {{VALUE}};', ], 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'title_shadow', 'selector' => '{{WRAPPER}} .bdt-tab .bdt-tabs-item .bdt-tabs-item-title', ] ); $this->add_responsive_control( 'title_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'title_margin', [ 'label' => __('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'title_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-tab .bdt-tabs-item .bdt-tabs-item-title', ] ); $this->add_control( 'title_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item .bdt-tabs-item-title' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography', 'selector' => '{{WRAPPER}} .bdt-tab .bdt-tabs-item-title', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_title_hover', [ 'label' => __('Hover', 'bdthemes-element-pack'), ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'hover_title_background', 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} .bdt-tab .bdt-tabs-item:hover .bdt-tabs-item-title', 'separator' => 'after', ] ); $this->add_control( 'hover_title_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item:hover .bdt-tabs-item-title' => 'color: {{VALUE}};', ], 'separator' => 'before', ] ); $this->add_control( 'title_hover_border', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::COLOR, 'condition' => [ 'title_border_border!' => '' ], 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item:hover .bdt-tabs-item-title' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_title_active', [ 'label' => __('Active', 'bdthemes-element-pack'), ] ); $this->add_control( 'active_style_color', [ 'label' => __('Style Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item.bdt-active .bdt-tabs-item-title:after' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'active_title_background', 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} .bdt-tab .bdt-tabs-item.bdt-active .bdt-tabs-item-title', 'separator' => 'after', ] ); $this->add_control( 'active_title_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item.bdt-active .bdt-tabs-item-title' => 'color: {{VALUE}};', ], 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'active_title_shadow', 'selector' => '{{WRAPPER}} .bdt-tab .bdt-tabs-item.bdt-active .bdt-tabs-item-title', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'active_title_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-tab .bdt-tabs-item.bdt-active .bdt-tabs-item-title', ] ); $this->add_control( 'active_title_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item.bdt-active .bdt-tabs-item-title' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_toggle_style_content', [ 'label' => __('Content', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'content_background_color', 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}} .bdt-tabs .bdt-switcher-item-content', 'separator' => 'after', ] ); $this->add_control( 'content_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'separator' => 'before', 'selectors' => [ '{{WRAPPER}} .bdt-tabs .bdt-switcher-item-content' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'content_link_color', [ 'label' => __('Link Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-tabs .bdt-switcher-item-content a' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'content_border', 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-tabs .bdt-switcher-item-content', ] ); $this->add_control( 'content_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-tabs .bdt-switcher-item-content' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_responsive_control( 'content_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-tabs .bdt-switcher-item-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'content_typography', 'selector' => '{{WRAPPER}} .bdt-tabs .bdt-switcher-item-content', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_icon', [ 'label' => __('Icon', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_icon_style'); $this->start_controls_tab( 'tab_icon_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'icon_align', [ 'label' => __('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __('Start', 'bdthemes-element-pack'), 'icon' => 'eicon-h-align-left', ], 'right' => [ 'title' => __('End', 'bdthemes-element-pack'), 'icon' => 'eicon-h-align-right', ], ], 'default' => is_rtl() ? 'right' : 'left', ] ); $this->add_control( 'icon_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-tab .bdt-tabs-item-title i' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-tab .bdt-tabs-item-title svg' => 'fill: {{VALUE}};', ], ] ); $this->add_responsive_control( 'icon_space', [ 'label' => __('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'default' => [ 'size' => 8, ], 'selectors' => [ '{{WRAPPER}} .bdt-tabs .bdt-tabs-item-title .bdt-button-icon-align-right' => is_rtl() ? 'margin-right: {{SIZE}}{{UNIT}};' : 'margin-left: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-tabs .bdt-tabs-item-title .bdt-button-icon-align-left' => is_rtl() ? 'margin-left: {{SIZE}}{{UNIT}};' : 'margin-right: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_icon_hover', [ 'label' => __('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'icon_hover_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-tabs .bdt-tabs-item:hover .bdt-tabs-item-title i' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-tabs .bdt-tabs-item:hover .bdt-tabs-item-title svg' => 'fill: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_icon_active', [ 'label' => __('Active', 'bdthemes-element-pack'), ] ); $this->add_control( 'icon_active_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-tabs .bdt-tabs-item.bdt-active .bdt-tabs-item-title i' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-tabs .bdt-tabs-item.bdt-active .bdt-tabs-item-title svg' => 'fill: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_tabs_sticky_style', [ 'label' => __('Sticky', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'sticky_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-tabs > div > .bdt-sticky.bdt-active' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'sticky_shadow', 'selector' => '{{WRAPPER}} .bdt-tabs > div > .bdt-sticky.bdt-active', ] ); $this->add_control( 'sticky_border_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-tabs > div > .bdt-sticky.bdt-active' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_label', [ 'label' => esc_html__('Label', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'label_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form label' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'label_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} #edd_profile_editor_form label', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_input', [ 'label' => esc_html__('Input', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'input_placeholder_color', [ 'label' => esc_html__('Placeholder Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input::placeholder' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_profile_editor_form textarea::placeholder' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'input_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_profile_editor_form .wpcf7-textarea' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'others_type_input_text_color', [ 'label' => esc_html__('Others Type Input Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#666666', 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form.select-state' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_profile_editor_form.select-gender' => 'color: {{VALUE}};', '{{WRAPPER}} #edd_profile_editor_form.accept-this-1' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'input_text_background', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input' => 'background-color: {{VALUE}};', '{{WRAPPER}} #edd_profile_editor_form .wpcf7-textarea' => 'background-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'textarea_height', [ 'label' => esc_html__('Textarea Height', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 125, ], 'range' => [ 'px' => [ 'min' => 30, 'max' => 500, ], ], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form .wpcf7-textarea' => 'height: {{SIZE}}{{UNIT}}; display: block;', ], 'separator' => 'before', ] ); $this->add_control( 'input_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input, {{WRAPPER}} #edd_profile_editor_form .wpcf7-textarea, {{WRAPPER}} #edd_profile_editor_form .select.edd-select' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'input_border_show', [ 'label' => esc_html__('Border Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'input_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #edd_profile_editor_form input, {{WRAPPER}} #edd_profile_editor_form textarea, {{WRAPPER}} #edd_profile_editor_form .select.edd-select', 'condition' => [ 'input_border_show' => 'yes', ], ] ); $this->add_control( 'input_border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', '{{WRAPPER}} #edd_profile_editor_form textarea' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', '{{WRAPPER}} #edd_profile_editor_form .select.edd-select' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_submit_button', [ 'label' => esc_html__('Submit Button', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_button_style'); $this->start_controls_tab( 'tab_button_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'button_text_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'background_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit', 'separator' => 'before', ] ); $this->add_control( 'border_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_shadow', 'selector' => '{{WRAPPER}} #edd_profile_editor_submit', ] ); $this->add_control( 'text_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'separator' => 'before', ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'button_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), //'scheme' => Schemes\Typography::TYPOGRAPHY_4, 'selector' => '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit', 'separator' => 'before', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_button_hover', [ 'label' => esc_html__('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'hover_color', [ 'label' => esc_html__('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit:hover' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'button_background_hover_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'button_hover_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit:hover' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_additional_option', [ 'label' => esc_html__('Profile Editor Additional', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'fullwidth_input', [ 'label' => esc_html__('Fullwidth Input', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => esc_html__('On', 'bdthemes-element-pack'), 'label_off' => esc_html__('Off', 'bdthemes-element-pack'), 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form input[type*="text"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="email"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="url"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="number"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="tel"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="date"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form input[type*="password"]' => 'width: 100%;', '{{WRAPPER}} #edd_profile_editor_form .select.edd-select' => 'width: 100%;', ], ] ); $this->add_control( 'fullwidth_textarea', [ 'label' => esc_html__('Fullwidth Texarea', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => esc_html__('On', 'bdthemes-element-pack'), 'label_off' => esc_html__('Off', 'bdthemes-element-pack'), 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form textarea' => 'width: 100%;', ], ] ); $this->add_control( 'fullwidth_button', [ 'label' => esc_html__('Fullwidth Button', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'label_on' => esc_html__('On', 'bdthemes-element-pack'), 'label_off' => esc_html__('Off', 'bdthemes-element-pack'), 'selectors' => [ '{{WRAPPER}} #edd_profile_editor_form #edd_profile_editor_submit' => 'width: 100%;', ], ] ); $this->add_control( 'profile_fieldset_color', [ 'label' => __('Fieldset Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-switcher-item-content fieldset' => 'border-color: {{VALUE}}', ], ] ); $this->add_responsive_control( 'profile_fieldset_width', [ 'label' => __('Fieldset Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px'], 'range' => [ 'px' => [ 'min' => 0, 'max' => 10, 'step' => 1, ] ], 'selectors' => [ '{{WRAPPER}} .bdt-switcher-item-content fieldset' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'profile_editor_fieldset_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-switcher-item-content fieldset' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); $id = $this->get_id(); $stickyClass = ''; if (isset($settings['nav_sticky_mode']) && $settings['nav_sticky_mode'] == 'yes') { $stickyClass = 'bdt-sticky-custom'; } $this->add_render_attribute( [ 'tabs_sticky_data' => [ 'data-settings' => [ wp_json_encode( array_filter([ "id" => 'bdt-tabs-' . $this->get_id(), "status" => $stickyClass, "activeHash" => $settings['active_hash'], "hashTopOffset" => (isset($settings['hash_top_offset']['size']) && !empty($settings['hash_top_offset']['size'])) ? $settings['hash_top_offset']['size'] : 70, "hashScrollspyTime" => (isset($settings['hash_scrollspy_time']['size']) ? $settings['hash_scrollspy_time']['size'] : 1500), "navStickyOffset" => (isset($settings['nav_sticky_offset']['size']) ? $settings['nav_sticky_offset']['size'] : 1), "activeItem" => (!empty($settings['active_item'])) ? $settings['active_item'] : NULL, "linkWidgetId" => $this->get_id(), ]) ), ], ], ] ); $this->add_render_attribute('tabs', 'id', 'bdt-tabs-' . esc_attr($id)); $this->add_render_attribute('tabs', 'class', 'bdt-tabs '); $this->add_render_attribute('tabs', 'class', 'bdt-tabs-' . $settings['tab_layout']); if ($settings['fullwidth_on_mobile']) { $this->add_render_attribute('tabs', 'class', 'fullwidth-on-mobile'); } ?> <div class="bdt-tabs-area"> <div <?php $this->print_render_attribute_string('tabs'); ?> <?php $this->print_render_attribute_string('tabs_sticky_data'); ?>> <?php if ('left' == $settings['tab_layout'] or 'right' == $settings['tab_layout']) { echo '<div class="bdt-grid-collapse" bdt-grid>'; } ?> <?php if ('bottom' == $settings['tab_layout']) : ?> <?php $this->tabs_content(); ?> <?php endif; ?> <?php $this->desktop_tab_items(); ?> <?php if ('bottom' != $settings['tab_layout']) : ?> <?php $this->tabs_content(); ?> <?php endif; ?> <?php if ('left' == $settings['tab_layout'] or 'right' == $settings['tab_layout']) { echo "</div>"; } ?> <a href="#" id="bottom-anchor-<?php echo esc_attr($id); ?>" data-bdt-hidden></a> </div> </div> <?php } public function tabs_content() { $settings = $this->get_settings_for_display(); $id = $this->get_id(); $this->add_render_attribute('switcher-width', 'class', 'bdt-switcher-wrapper'); if ('left' == $settings['tab_layout'] or 'right' == $settings['tab_layout']) { if (768 == $settings['media']) { $this->add_render_attribute('switcher-width', 'class', 'bdt-width-expand@s'); } else { $this->add_render_attribute('switcher-width', 'class', 'bdt-width-expand@m'); } } ?> <div <?php $this->print_render_attribute_string('switcher-width'); ?>> <div id="bdt-tab-content-<?php echo esc_attr($id); ?>" class="bdt-switcher bdt-switcher-item-content"> <?php foreach ($settings['tabs'] as $index => $item) : ?> <?php $tab_count = $index + 1; $tab_count_active = ''; if ($tab_count === $settings['active_item']) { $tab_count_active = 'bdt-active'; } ?> <div class="<?php echo esc_attr($tab_count_active); ?>" data-content-id="<?php echo esc_attr(strtolower(preg_replace('#[ -]+#', '-', trim(preg_replace("![^a-z0-9]+!i", " ", esc_html($item['tab_title'])))))); ?>"> <div> <?php if (!empty($item['tab_content'])) { echo do_shortcode('[' . $item['tab_content'] . ']'); } ?> </div> </div> <?php endforeach; ?> </div> </div> <?php } public function desktop_tab_items() { $settings = $this->get_settings_for_display(); $id = $this->get_id(); if ('left' == $settings['tab_layout'] or 'right' == $settings['tab_layout']) { $this->add_render_attribute('tabs-width', 'class', 'bdt-tab-wrapper'); if ('right' == $settings['tab_layout']) { $this->add_render_attribute('tabs-width', 'class', 'bdt-flex-last@m'); } if (768 == $settings['media']) { $this->add_render_attribute('tabs-width', 'class', 'bdt-width-auto@s'); if ('right' == $settings['tab_layout']) { $this->add_render_attribute('tabs-width', 'class', 'bdt-flex-last'); } } else { $this->add_render_attribute('tabs-width', 'class', 'bdt-width-auto@m'); } } $this->add_render_attribute( [ 'tab-settings' => [ 'class' => [ 'bdt-tab', ('' !== $settings['tab_layout']) ? 'bdt-tab-' . $settings['tab_layout'] : '', ('' != $settings['align'] and 'left' != $settings['tab_layout'] and 'right' != $settings['tab_layout']) ? (('justify' != $settings['align']) ? 'bdt-flex-' . $settings['align'] : 'bdt-child-width-expand') : '' ] ] ] ); $this->add_render_attribute('tab-settings', 'data-bdt-tab', 'connect: #bdt-tab-content-' . esc_attr($id) . ';'); if (isset($settings['tab_transition']) and $settings['tab_transition']) { $this->add_render_attribute('tab-settings', 'data-bdt-tab', 'animation: bdt-animation-' . $settings['tab_transition'] . ';'); } if (isset($settings['duration']['size']) and $settings['duration']['size']) { $this->add_render_attribute('tab-settings', 'data-bdt-tab', 'duration: ' . $settings['duration']['size'] . ';'); } if (isset($settings['media']) and $settings['media']) { $this->add_render_attribute('tab-settings', 'data-bdt-tab', 'media: ' . intval($settings['media']) . ';'); } if ('yes' != $settings['swiping_on_mobile']) { $this->add_render_attribute('tab-settings', 'data-bdt-tab', 'swiping: false;'); } if ($settings['tabs_match_height']) { $this->add_render_attribute('tab-settings', 'data-bdt-height-match', 'target: > .bdt-tabs-item > .bdt-tabs-item-title; row: false;'); } if (isset($settings['nav_sticky_mode']) && 'yes' == $settings['nav_sticky_mode']) { $this->add_render_attribute('tabs-sticky', 'data-bdt-sticky', 'bottom: #bottom-anchor-' . $id . ';'); if ($settings['nav_sticky_offset']['size']) { $this->add_render_attribute('tabs-sticky', 'data-bdt-sticky', 'offset: ' . $settings['nav_sticky_offset']['size'] . ';'); } if ($settings['nav_sticky_on_scroll_up']) { $this->add_render_attribute('tabs-sticky', 'data-bdt-sticky', 'show-on-up: true; animation: bdt-animation-slide-top'); } } ?> <div <?php $this->print_render_attribute_string('tabs-width'); ?>> <div <?php $this->print_render_attribute_string('tabs-sticky'); ?>> <div <?php $this->print_render_attribute_string('tab-settings'); ?>> <?php foreach ($settings['tabs'] as $index => $item) : $tab_count = $index + 1; $tab_id = ($item['tab_title']) ? element_pack_string_id($item['tab_title']) : $id . $tab_count; // $tab_id = 'bdt-tab-' . $tab_id; $tab_id = 'bdt-tab-' . strtolower(preg_replace('#[ -]+#', '-', trim(preg_replace("![^a-z0-9]+!i", " ", $tab_id)))); $this->add_render_attribute('tabs-item', 'class', 'bdt-tabs-item', true); if (empty($item['tab_title'])) { $this->add_render_attribute('tabs-item', 'class', 'bdt-has-no-title'); } if ($tab_count === $settings['active_item']) { $this->add_render_attribute('tabs-item', 'class', 'bdt-active'); } if (!isset($item['tab_icon']) && !Icons_Manager::is_migration_allowed()) { // add old default $item['tab_icon'] = 'fas fa-book'; } $migrated = isset($item['__fa4_migrated']['tab_select_icon']); $is_new = empty($item['tab_icon']) && Icons_Manager::is_migration_allowed(); $this->add_render_attribute('tab-link', 'data-title', strtolower(preg_replace('#[ -]+#', '-', trim(preg_replace("![^a-z0-9]+!i", " ", esc_html($item['tab_title']))))), true); if (empty($item['tab_title'])) { $this->add_render_attribute('tab-link', 'data-title', $this->get_id() . '-' . $tab_count, true); } $this->add_render_attribute('tab-link', 'class', 'bdt-tabs-item-title', true); $this->add_render_attribute('tab-link', 'id', esc_attr($tab_id), true); $this->add_render_attribute('tab-link', 'data-tab-index', esc_attr($index), true); $this->add_render_attribute('tab-link', 'href', '#', true); ?> <div <?php $this->print_render_attribute_string('tabs-item'); ?>> <a <?php $this->print_render_attribute_string('tab-link'); ?>> <div class="bdt-tab-text-wrapper bdt-flex-column"> <div class="bdt-tab-title-icon-wrapper"> <?php if ('' != $item['tab_select_icon']['value'] and 'left' == $settings['icon_align']) : ?> <span class="bdt-button-icon-align-<?php echo esc_html($settings['icon_align']); ?>"> <?php if ($is_new || $migrated) : Icons_Manager::render_icon($item['tab_select_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ]); else : ?> <i class="<?php echo esc_attr($item['tab_icon']); ?>" aria-hidden="true"></i> <?php endif; ?> </span> <?php endif; ?> <?php if ($item['tab_title']) : ?> <span class="bdt-tab-text"> <?php echo wp_kses($item['tab_title'], element_pack_allow_tags('title')); ?> </span> <?php endif; ?> <?php if ('' != $item['tab_select_icon']['value'] and 'right' == $settings['icon_align']) : ?> <span class="bdt-button-icon-align-<?php echo esc_html($settings['icon_align']); ?>"> <?php if ($is_new || $migrated) : Icons_Manager::render_icon($item['tab_select_icon'], [ 'aria-hidden' => 'true', 'class' => 'fa-fw' ]); else : ?> <i class="<?php echo esc_attr($item['tab_icon']); ?>" aria-hidden="true"></i> <?php endif; ?> </span> <?php endif; ?> </div> </div> </a> </div> <?php endforeach; ?> </div> </div> </div> <?php } } <?php namespace ElementPack\Modules\EddTabs; use ElementPack\Base\Element_Pack_Module_Base; if ( !defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'bdt-edd-tabs'; } public function get_widgets() { $widgets = [ 'EDD_Tabs', ]; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Edd Tabs', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => false, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\Honeycombs\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Background; use Elementor\Group_Control_Typography; use Elementor\Repeater; use Elementor\Group_Control_Css_Filter; use Elementor\Icons_Manager; use ElementPack\Element_Pack_Loader; use ElementPack\Utils; if (!defined('ABSPATH')) { exit(); } class Honeycombs extends Module_Base { public function get_name() { return 'bdt-honeycombs'; } public function get_title() { return BDTEP . esc_html__('Honeycombs', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-honeycombs'; } public function get_categories() { return ['element-pack']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-font', 'ep-honeycombs']; } } public function get_script_depends() { if ($this->ep_is_edit_mode()) { return ['honeycombs', 'ep-scripts']; } else { return ['honeycombs', 'ep-honeycombs']; } } public function get_keywords() { return ['hexagon', 'box', 'honeycomb']; } public function get_custom_help_url() { return 'https://youtu.be/iTWXzc329vQ'; } protected function register_controls() { $this->start_controls_section( 'section_honeycombs_item', [ 'label' => esc_html__('Layout', 'bdthemes-element-pack'), ] ); $this->add_control( 'honeycomb_style', [ 'label' => esc_html__('HoneyComb Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'default', 'options' => [ 'default' => esc_html__('Default', 'bdthemes-element-pack'), 'radius' => esc_html__('Radius', 'bdthemes-element-pack'), 'radius2' => esc_html__('Large Radius', 'bdthemes-element-pack'), 'zigzag' => esc_html__('Zigzag', 'bdthemes-element-pack'), ], ] ); $repeater = new Repeater(); $repeater->add_control( 'item_invisible', [ 'label' => esc_html__('Item Invisible', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, ] ); $repeater->start_controls_tabs('tabs_content', [ 'condition' => [ 'item_invisible' => '' ] ]); $repeater->start_controls_tab( 'tab_content_front', [ 'label' => esc_html__('Front', 'bdthemes-element-pack'), ] ); $repeater->add_control( 'honeycombs_item_icon', [ 'label' => esc_html__('Icon', 'bdthemes-element-pack'), 'type' => Controls_Manager::ICONS, 'label_block' => true, 'default' => [ 'value' => 'fas fa-check', 'library' => 'fa-solid', ], // 'condition' => [ // 'icon_display' => 'yes', // ], ] ); $repeater->add_control( 'honeycombs_title', [ 'label' => esc_html__('Title', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'label_block' => true, 'placeholder' => esc_html__('Title Item', 'bdthemes-element-pack'), 'default' => esc_html__('Title Item', 'bdthemes-element-pack'), 'dynamic' => [ 'active' => true, ], ] ); $repeater->add_control( 'title_color_item', [ 'label' => esc_html__('Title Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb{{CURRENT_ITEM}} .bdt-inner .bdt-wrapper .bdt-title' => 'color: {{VALUE}}', ], ] ); $repeater->add_control( 'background_item', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb{{CURRENT_ITEM}} .bdt-icon-hex-lg' => 'background-color: {{VALUE}}', ], 'render_type' => 'template', ] ); $repeater->add_control( 'honeycombs_bg_img', [ 'label' => esc_html__('Background Image', 'bdthemes-element-pack'), 'type' => Controls_Manager::MEDIA, 'dynamic' => [ 'active' => true, ], 'label_block' => true, 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-comb{{CURRENT_ITEM}} .bdt-icon-hex-lg' => 'background: url({{URL}}) no-repeat center center; background-size: cover; background-clip: text; -webkit-background-clip: text; color: transparent; background-position: center; filter: none;', ], ] ); $repeater->end_controls_tab(); $repeater->start_controls_tab( 'tab_content_back', [ 'label' => esc_html__('Back', 'bdthemes-element-pack'), ] ); $repeater->add_control( 'honeycombs_content', [ 'label' => esc_html__('Content', 'bdthemes-element-pack'), 'type' => Controls_Manager::WYSIWYG, 'default' => esc_html__("Default description. Lorem Ipsum is simply dummy text of the printing and typesetting industry. ", 'bdthemes-element-pack'), 'placeholder' => esc_html__('Type your description here', 'bdthemes-element-pack'), 'dynamic' => [ 'active' => true, ], ] ); $repeater->add_control( 'title_color_item_back', [ 'label' => esc_html__('Content Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb{{CURRENT_ITEM}} .bdt-inner .bdt-wrapper .bdt-content' => 'color: {{VALUE}}', ], ] ); $repeater->add_control( 'background_item_back', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb{{CURRENT_ITEM}}:hover .bdt-icon-hex-lg' => 'background-color: {{VALUE}} !important', ], 'render_type' => 'template', ] ); $repeater->add_control( 'honeycombs_bg_img_back', [ 'label' => esc_html__('Background Image', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::MEDIA, 'dynamic' => [ 'active' => true, ], 'label_block' => true, 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-comb{{CURRENT_ITEM}}:hover .bdt-icon-hex-lg' => 'background: url({{URL}}) no-repeat center center; background-size: cover; background-clip: text; -webkit-background-clip: text; color: transparent; background-position: center; filter: none;', ], ] ); $repeater->end_controls_tab(); $repeater->end_controls_tabs(); $repeater->add_control( 'honeycombs_link', [ 'label' => esc_html__('Link', 'bdthemes-element-pack'), 'type' => Controls_Manager::URL, 'dynamic' => [ 'active' => true, ], 'label_block' => true, 'placeholder' => esc_html__('https://your-link.com', 'bdthemes-element-pack'), 'condition' => [ 'item_invisible' => '' ] ] ); $this->add_control( 'honeycombs_list', [ 'label' => esc_html__('Items', 'bdthemes-element-pack'), 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(), 'separator' => 'before', 'default' => [ [ 'honeycombs_title' => esc_html__('Comb 1', 'bdthemes-element-pack'), 'honeycombs_content' => esc_html__('@1 Click edit button to change this text. Lorem agaca ipsum.', 'bdthemes-element-pack'), 'honeycombs_item_icon' => [ 'value' => 'far fa-moon', 'library' => 'fa-regular' ], ], [ 'honeycombs_title' => esc_html__('Comb 2', 'bdthemes-element-pack'), 'honeycombs_content' => esc_html__('@2 Click edit button to change this text. Lorem agaca ipsum. ', 'bdthemes-element-pack'), 'honeycombs_item_icon' => [ 'value' => 'far fa-smile', 'library' => 'fa-regular' ], ], [ 'honeycombs_title' => esc_html__('Comb 3', 'bdthemes-element-pack'), 'honeycombs_content' => esc_html__('@3 Click edit button to change this text. Lorem agaca ipsum. ', 'bdthemes-element-pack'), 'honeycombs_item_icon' => [ 'value' => 'far fa-heart', 'library' => 'fa-regular' ], ], ], 'title_field' => '{{{ honeycombs_title }}}', ] ); $this->add_control( 'icon_display', [ 'label' => esc_html__('Show Icon', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'no', 'separator' => 'before' ] ); $this->add_control( 'title_display', [ 'label' => esc_html__('Show Title', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes', ] ); $this->add_control( 'description_display', [ 'label' => esc_html__('Show Description', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_honeycombs_additional', [ 'label' => esc_html__('Additional', 'bdthemes-element-pack'), ] ); $this->add_responsive_control( 'items_width', [ 'label' => esc_html__('Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px', ''], 'range' => [ 'px' => [ 'min' => 100, 'max' => 600, 'step' => 5, ], ], 'default' => [ 'unit' => 'px', 'size' => 250, ], 'render_type' => 'template', ] ); $this->add_control( 'items_spacing', [ 'label' => esc_html__('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px', ''], 'range' => [ 'px' => [ 'min' => -50, 'max' => 100, 'step' => 5, ], ], 'default' => [ 'unit' => 'px', 'size' => -20, ], ] ); $this->add_control( 'title_tag', [ 'label' => esc_html__('Title Tag', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'h3', 'options' => element_pack_heading_size(), 'condition' => [ 'title_display' => 'yes', ], ] ); $this->add_control( 'comb_animation_type', [ 'label' => esc_html__('Comb Animation', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => '', 'options' => element_pack_transition_options(), 'separator' => 'before', ] ); $this->add_control( 'combs_anim_delay', [ 'label' => esc_html__('Animation delay', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['ms', ''], 'range' => [ 'ms' => [ 'min' => 0, 'max' => 1000, 'step' => 5, ], ], 'default' => [ 'unit' => 'ms', 'size' => 300, ], 'condition' => [ 'comb_animation_type!' => '', ], ] ); $this->end_controls_section(); //Style // content items $this->start_controls_section( 'section_style_items', [ 'label' => esc_html__('Item', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->start_controls_tabs('tabs_items_style'); $this->start_controls_tab( 'tab_items_front', [ 'label' => esc_html__('Front', 'bdthemes-element-pack'), ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'items_background', 'selector' => '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-icon-hex-lg', 'render_type' => 'template' ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'items_css_filters', 'selector' => '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-icon-hex-lg', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_items_back', [ 'label' => esc_html__('Back', 'bdthemes-element-pack'), ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'items_background_back', 'selector' => '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb:hover .bdt-comb-inner-wrapper .bdt-icon-hex-lg', 'render_type' => 'template' ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'items_css_filters_back', 'selector' => '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb:hover .bdt-icon-hex-lg', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); // end content items // icon $this->start_controls_section( 'section_style_icon', [ 'label' => esc_html__('Icon', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'icon_display' => 'yes', ], ] ); $this->add_control( 'icon_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-honeycombs-icon' => 'color: {{VALUE}}', '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-honeycombs-icon svg' => 'fill: {{VALUE}}', ], ] ); $this->add_responsive_control( 'icon_size', [ 'label' => esc_html__('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px', ''], 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, 'step' => 10, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-honeycombs-icon' => 'font-size: {{SIZE}}{{UNIT}}', '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-honeycombs-icon svg' => 'width: {{SIZE}}{{UNIT}}', ], ] ); $this->add_responsive_control( 'icon_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-honeycombs-icon' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-honeycombs-icon svg' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); // icon // title $this->start_controls_section( 'section_style_title', [ 'label' => esc_html__('Title', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'title_display' => 'yes', ], ] ); $this->add_control( 'title_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-title' => 'color: {{VALUE}}', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-title', ] ); $this->add_responsive_control( 'title_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); // end title // description $this->start_controls_section( 'section_style_description', [ 'label' => esc_html__('Description', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'description_display' => 'yes', ], ] ); $this->add_control( 'description_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-content' => 'color: {{VALUE}}', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'description_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-content', ] ); $this->add_responsive_control( 'description_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-honeycombs-area .bdt-honeycombs .bdt-honeycombs-inner-wrapper .bdt-comb .bdt-inner .bdt-wrapper .bdt-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); // end description } protected function render() { $settings = $this->get_settings_for_display(); $frontSideDisplay = ''; if ($settings['title_display'] == 'yes' || $settings['icon_display'] == 'yes') { $frontSideDisplay = 'yes'; } $titleTag = $settings['title_tag']; $honeycomb_style = 'honeycomb-style-' . $settings['honeycomb_style']; $honeycomb_des_visibility = ($settings['description_display'] == 'yes') ? ' ' : 'honeycomb-des-visibility-hide'; $honeycomb_title_visibility = ($frontSideDisplay == 'yes') ? ' ' : 'honeycomb-title-visibility-hide'; $elementor_vp_lg = get_option('elementor_viewport_lg'); $elementor_vp_md = get_option('elementor_viewport_md'); $viewport_lg = !empty($elementor_vp_lg) ? $elementor_vp_lg - 1 : 1023; $viewport_md = !empty($elementor_vp_md) ? $elementor_vp_md - 1 : 767; $items_width = isset($settings['items_width']['size']) ? $settings['items_width']['size'] : 250; $items_width_tablet = isset($settings['items_width_tablet']['size']) ? $settings['items_width_tablet']['size'] : 250; $items_width_mobile = isset($settings['items_width_mobile']['size']) ? $settings['items_width_mobile']['size'] : 250; $this->add_render_attribute( [ 'honeycombs' => [ 'data-settings' => [ wp_json_encode(array_filter([ "id" => $this->get_id(), "width" => $items_width, "width_tablet" => $items_width_tablet, "width_mobile" => $items_width_mobile, "viewport_lg" => $viewport_lg, "viewport_md" => $viewport_md, "margin" => $settings['items_spacing']['size'], ])), ], ], ] ); $this->add_render_attribute('honeycombs', 'class', 'bdt-honeycombs'); $this->add_render_attribute('honeycombs', 'class', $honeycomb_style); $this->add_render_attribute('honeycombs', 'class', $honeycomb_des_visibility . ' ' . $honeycomb_title_visibility); ?> <div class="bdt-honeycombs-area" <?php if ($settings['comb_animation_type'] !== '') { ?> bdt-grid bdt-scrollspy="cls: bdt-animation-<?php echo esc_attr($settings['comb_animation_type']); ?>; target: .bdt-comb-inner-wrapper; delay: <?php echo esc_attr( $settings['combs_anim_delay']['size'] ); ?>;" <?php } ?>> <div <?php $this->print_render_attribute_string('honeycombs'); ?>> <?php foreach ($settings['honeycombs_list'] as $index => $item) : ?> <?php if (!Element_Pack_Loader::elementor()->editor->is_edit_mode()) { if (!empty($item['honeycombs_link']['url'])) { $this->add_link_attributes('bdt-comb-link' . $index, $item['honeycombs_link']); } else { $this->add_render_attribute('bdt-comb-link' . $index, 'href', 'javascript:void(0);', true); $this->add_render_attribute('bdt-comb-link' . $index, 'target', '_self', true); } } else { $this->add_render_attribute('bdt-comb-link' . $index, 'href', 'javascript:void(0);', true); } $this->add_render_attribute('bdt-comb-link' . $index, 'class', 'bdt-comb elementor-repeater-item-' . esc_attr($item['_id']), true); if ($item['item_invisible']) { $this->add_render_attribute('bdt-comb-link' . $index, 'class', 'bdt-invisible bdt-comb placeholder hide elementor-repeater-item-' . esc_attr($item['_id']), true); } ?> <a <?php $this->print_render_attribute_string('bdt-comb-link' . $index); ?>> <div class="bdt-front-content"> <?php if ($settings['icon_display'] == 'yes') : ?> <?php if (!empty($item['honeycombs_item_icon']['value'])) : ?> <div class="bdt-honeycombs-icon"> <?php Icons_Manager::render_icon($item['honeycombs_item_icon'], ['aria-hidden' => 'true']); ?> </div> <?php endif; ?> <?php endif; ?> <?php if ($settings['title_display'] == 'yes') : ?> <<?php echo esc_attr(Utils::get_valid_html_tag($titleTag)); ?> class="bdt-title"> <?php echo wp_kses_post($item['honeycombs_title']); ?> </<?php echo esc_attr(Utils::get_valid_html_tag($titleTag)); ?>> <?php endif; ?> </div> <div class="bdt-back-content"> <?php if ($settings['description_display'] == 'yes') : ?> <div class="bdt-content"> <?php echo wp_kses_post($item['honeycombs_content']); ?> </div> <?php endif; ?> </div> </a> <?php endforeach; ?> </div> </div> <?php } } <?php namespace ElementPack\Modules\Honeycombs; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'honeycombs'; } public function get_widgets() { $widgets = ['Honeycombs']; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Honeycombs', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => false, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\TwitterSlider\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Border; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Css_Filter; use Elementor\Plugin; use ElementPack\Traits\Global_Swiper_Controls; use TwitterOAuth; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class Twitter_Slider extends Module_Base { use Global_Swiper_Controls; private $_query = null; public function get_name() { return 'bdt-twitter-slider'; } public function get_title() { return BDTEP . __('Twitter Slider', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-twitter-slider'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['twitter', 'slider']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-font', 'ep-twitter-slider']; } } public function get_script_depends() { if ($this->ep_is_edit_mode()) { return ['ep-scripts']; } else { return ['ep-twitter-slider']; } } public function on_import($element) { if (!get_post_type_object($element['settings']['posts_post_type'])) { $element['settings']['posts_post_type'] = 'post'; } return $element; } public function get_query() { return $this->_query; } public function get_custom_help_url() { return 'https://youtu.be/Bd3I7ipqMms'; } protected function register_controls() { $this->register_query_section_controls(); } private function register_query_section_controls() { $this->start_controls_section( 'section_carousel_layout', [ 'label' => __('Layout', 'bdthemes-element-pack'), ] ); $this->add_control( 'num_tweets', [ 'label' => __('Limit', 'bdthemes-element-pack'), 'type' => Controls_Manager::NUMBER, 'default' => 6, ] ); $this->add_control( 'cache_time', [ 'label' => __('Cache Time(m)', 'bdthemes-element-pack'), 'type' => Controls_Manager::NUMBER, 'default' => 60, ] ); $this->add_control( 'show_avatar', [ 'label' => __('Show Avatar', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'enable_twitter_auth2_api', [ 'label' => __('Enable Twitter Auth2 API', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'avatar_link', [ 'label' => __('Avatar Link', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'show_avatar' => 'yes' ] ] ); $this->add_control( 'show_time', [ 'label' => __('Show Time', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'long_time_format', [ 'label' => __('Long Time Format', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'show_time' => 'yes', ] ] ); $this->add_control( 'show_meta_button', [ 'label' => __('Execute Buttons', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'exclude_replies', [ 'label' => __('Exclude Replies', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'strip_emoji', [ 'label' => __('Strip Emoji', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, ] ); $this->end_controls_section(); //Navigation Controls $this->start_controls_section( 'section_content_navigation', [ 'label' => __('Navigation', 'bdthemes-element-pack'), ] ); //Global Navigation Controls $this->register_navigation_controls(); $this->end_controls_section(); $this->start_controls_section( 'section_content_slider_settins', [ 'label' => esc_html__('Slider Settings', 'bdthemes-element-pack'), ] ); $this->add_control( 'autoplay', [ 'label' => esc_html__('Auto Play', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'autoplay_speed', [ 'label' => esc_html__('Autoplay Speed', 'bdthemes-element-pack'), 'type' => Controls_Manager::NUMBER, 'default' => 5000, 'condition' => [ 'autoplay' => 'yes', ], ] ); $this->add_control( 'pauseonhover', [ 'label' => esc_html__('Pause on Hover', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, ] ); $this->add_control( 'speed', [ 'label' => __('Animation Speed (ms)', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 500, ], 'range' => [ 'px' => [ 'min' => 100, 'max' => 5000, 'step' => 50, ], ], ] ); $this->add_control( 'loop', [ 'label' => esc_html__('Loop', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'transition', [ 'label' => esc_html__('Transition', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'slide', 'options' => [ 'slide' => esc_html__('Slide', 'bdthemes-element-pack'), 'fade' => esc_html__('Fade', 'bdthemes-element-pack'), 'cube' => esc_html__('Cube', 'bdthemes-element-pack'), 'coverflow' => esc_html__('Coverflow', 'bdthemes-element-pack'), 'flip' => esc_html__('Flip', 'bdthemes-element-pack'), ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_layout', [ 'label' => __('Items', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'item_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-slider-item .bdt-twitter-text, {{WRAPPER}} .bdt-twitter-slider .bdt-twitter-slider-item .bdt-twitter-text *' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'item_background_color', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-slider-item .bdt-card-body' => 'background-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'item_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-card-body' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}', ], ] ); $this->add_control( 'alignment', [ 'label' => __('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'default' => 'center', 'options' => [ 'left' => [ 'title' => __('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-right', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-slider-item .bdt-card-body' => 'text-align: {{VALUE}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_avatar', [ 'label' => __('Avatar', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_avatar' => 'yes', ], ] ); $this->add_control( 'avatar_width', [ 'label' => __('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 48, 'min' => 15, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb-wrapper img' => 'width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'avatar_align', [ 'label' => __('Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-right', ], ], 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb' => 'text-align: {{VALUE}};', ], ] ); $this->add_control( 'avatar_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb-wrapper' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'avatar_border', 'label' => __('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb-wrapper', ] ); $this->add_responsive_control( 'avatar_border_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb-wrapper, {{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb-wrapper img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_responsive_control( 'avatar_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}', ], ] ); $this->add_responsive_control( 'avatar_margin', [ 'label' => __('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb-wrapper' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}', ], ] ); $this->add_control( 'avatar_opacity', [ 'label' => __('Opacity (%)', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb-wrapper img' => 'opacity: {{SIZE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'avatar_shadow', 'selector' => '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb-wrapper', ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'avatar_css_filters', 'selector' => '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-thumb-wrapper', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_meta', [ 'label' => __('Execute Buttons', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_meta_button' => 'yes', ], ] ); $this->add_control( 'meta_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-meta-button > a' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'meta_hover_color', [ 'label' => __('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-meta-button > a:hover' => 'color: {{VALUE}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_time', [ 'label' => __('Time', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_time' => 'yes', ], ] ); $this->add_control( 'time_color', [ 'label' => __('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-meta-wrapper a.bdt-twitter-time-link' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'time_hover_color', [ 'label' => __('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-twitter-slider .bdt-twitter-meta-wrapper a.bdt-twitter-time-link:hover' => 'color: {{VALUE}};', ], ] ); $this->end_controls_section(); //Navigation Style $this->start_controls_section( 'section_style_navigation', [ 'label' => __('Navigation', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'conditions' => [ 'relation' => 'or', 'terms' => [ [ 'name' => 'navigation', 'operator' => '!=', 'value' => 'none', ], [ 'name' => 'show_scrollbar', 'value' => 'yes', ], ], ], ] ); //Global Navigation Style Controls $this->register_navigation_style_controls('swiper-carousel'); $this->end_controls_section(); } public function getTwitterAuth2Data($consumerKey, $consumerSecret, $username) { $access_token = get_option('elementpack_twitter_access_token_' . $username); if (!$access_token) { $credentials = base64_encode($consumerKey . ':' . $consumerSecret); $response = wp_remote_post('https://api.twitter.com/oauth2/token', [ 'method' => 'POST', 'httpversion' => '1.1', 'sslverify' => false, 'blocking' => true, 'headers' => [ 'Authorization' => 'Basic ' . $credentials, 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8', ], 'body' => ['grant_type' => 'client_credentials'], ]); $body = json_decode(wp_remote_retrieve_body($response)); if ($body && isset($body->access_token)) { update_option('elementpack_twitter_access_token_' . $username, $body->access_token); $access_token = $body->access_token; } } $response = wp_remote_get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $username . '&count=999&tweet_mode=extended', [ 'httpversion' => '1.1', 'blocking' => true, 'sslverify' => false, 'headers' => [ 'Authorization' => "Bearer $access_token", ], ]); if ($response['response']['code'] == 200 && !empty($response['response'])) { return json_decode(wp_remote_retrieve_body($response), true); } } public function getTwitterAuth1Data($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret, $twitter_name) { $connection = new \TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret); $settings = $this->get_settings_for_display(); $exclude_replies = ('yes' === $settings['exclude_replies']) ? true : false; // If excluding replies, we need to fetch more than requested as the // total is fetched first, and then replies removed. $totalToFetch = ($exclude_replies) ? max(50, $settings['num_tweets'] * 3) : $settings['num_tweets']; $fetchedTweets = $connection->get( 'statuses/user_timeline', array( 'screen_name' => $twitter_name, 'count' => $totalToFetch, ) ); if ($connection->http_code == 200) { return $fetchedTweets; } } public function render_loop_twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret, $twitter_name) { $settings = $this->get_settings_for_display(); $isEnableAuth2 = isset($settings['enable_twitter_auth2_api']) && $settings['enable_twitter_auth2_api'] == 'yes'; $name = $twitter_name; $exclude_replies = ('yes' === $settings['exclude_replies']) ? true : false; $transName = 'bdt-tweets-' . $name; // Name of value in database. [added $name for multiple account use] $backupName = $transName . '-backup'; // Name of backup value in database. if ($isEnableAuth2) { $transName = 'bdt-tweets-auth2-' . $name; $backupName = $transName . '-backup'; } if ($isEnableAuth2) { if (!get_transient($name)) { $fetchedTweets = $this->getTwitterAuth2Data($consumerKey, $consumerSecret, $twitter_name); if ($fetchedTweets) { $fetchedTweets = json_decode(json_encode($fetchedTweets)); // convert array to json recursively. } } } else { if (!get_transient($name)) { $fetchedTweets = $this->getTwitterAuth1Data($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret, $twitter_name); } } // Did the fetch fail? if (!$fetchedTweets) : $tweets = get_option($backupName); // False if there has never been data saved. else : // Fetch succeeded. // Now update the array to store just what we need. // (Done here instead of PHP doing this for every page load) $limitToDisplay = min($settings['num_tweets'], count($fetchedTweets)); for ($i = 0; $i < $limitToDisplay; $i++) : $tweet = $fetchedTweets[$i]; // Core info. $name = $tweet->user->name; // COMMUNITY REQUEST !!!!!! (2) $screen_name = $tweet->user->screen_name; $permalink = 'https://twitter.com/' . $screen_name . '/status/' . $tweet->id_str; $tweet_id = $tweet->id_str; /* Alternative image sizes method: http://dev.twitter.com/doc/get/users/profile_image/:screen_name */ // Check for SSL via protocol https then display relevant image - thanks SO - this should do if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { // $protocol = 'https://'; $image = $tweet->user->profile_image_url_https; } else { // $protocol = 'http://'; $image = $tweet->user->profile_image_url; } // Process Tweets - Use Twitter entities for correct URL, hash and mentions $text = $this->process_links($tweet); // lets strip 4-byte emojis if ($settings['strip_emoji'] == 'yes') { $text = $this->twitter_api_strip_emoji($text); } // Need to get time in Unix format. $time = $tweet->created_at; $time = date_parse($time); $uTime = mktime($time['hour'], $time['minute'], $time['second'], $time['month'], $time['day'], $time['year']); // Now make the new array. $tweets[] = array( 'text' => $text, 'name' => $name, 'permalink' => $permalink, 'image' => $image, 'time' => $uTime, 'tweet_id' => $tweet_id ); endfor; set_transient($transName, $tweets, 60 * $settings['cache_time']); update_option($backupName, $tweets); endif; ?> <?php // Now display the tweets, if we can. if ($tweets) : ?> <?php foreach ((array) $tweets as $t) : // casting array to array just in case it's empty - then prevents PHP warning ?> <div class="bdt-twitter-slider-item swiper-slide"> <div class="bdt-card"> <div class="bdt-card-body"> <?php if ('yes' === $settings['show_avatar']) : ?> <?php if ('yes' === $settings['avatar_link']) : ?> <a href="https://twitter.com/<?php echo esc_attr($name); ?>"> <?php endif; ?> <div class="bdt-twitter-thumb"> <div class="bdt-twitter-thumb-wrapper"> <img src="<?php echo esc_url($t['image']); ?>" alt="<?php echo esc_html($t['name']); ?>" /> </div> </div> <?php if ('yes' === $settings['avatar_link']) : ?> </a> <?php endif; ?> <?php endif; ?> <div class="bdt-twitter-text bdt-clearfix"> <?php echo wp_kses_post($t['text']); ?> </div> <div class="bdt-twitter-meta-wrapper"> <?php if ('yes' === $settings['show_time']) : ?> <a href="<?php echo esc_url($t['permalink']); ?>" target="_blank" class="bdt-twitter-time-link"> <?php // Original - long time ref: hours... if ('yes' === $settings['long_time_format']) { // New - short Twitter style time ref: h... $timeDisplay = human_time_diff($t['time'], current_time('timestamp')); } else { $timeDisplay = element_pack_time_diff($t['time'], current_time('timestamp')); } $displayAgo = _x('ago', 'leading space is required', 'bdthemes-element-pack'); // Use to make il8n compliant printf(esc_html__('%1$s %2$s', 'bdthemes-element-pack'), wp_kses_post($timeDisplay), wp_kses_post($displayAgo)); ?> </a> <?php endif; ?> <?php if ('yes' === $settings['show_meta_button']) : ?> <div class="bdt-twitter-meta-button"> <a href="https://twitter.com/intent/tweet?in_reply_to=<?php echo esc_url($t['tweet_id']); ?>" data-lang="en" class="bdt-tmb-reply" title="<?php esc_html_e('Reply', 'bdthemes-element-pack'); ?>" target="_blank"> <i class="ep-icon-reply" aria-hidden="true"></i> </a> <a href="https://twitter.com/intent/retweet?tweet_id=<?php echo esc_url($t['tweet_id']); ?>" data-lang="en" class="bdt-tmb-retweet" title="<?php esc_html_e('Retweet', 'bdthemes-element-pack'); ?>" target="_blank"> <i class="ep-icon-refresh" aria-hidden="true"></i> </a> <a href="https://twitter.com/intent/favorite?tweet_id=<?php echo esc_url($t['tweet_id']); ?>" data-lang="en" class="bdt-tmb-favorite" title="<?php esc_html_e('Favourite', 'bdthemes-element-pack'); ?>" target="_blank"> <i class="ep-icon-star" aria-hidden="true"></i> </a> </div> <?php endif; ?> </div> </div> </div> </div> <?php endforeach; endif; } public function render() { if (!class_exists('TwitterOAuth')) { include BDTEP_PATH . 'includes/twitteroauth/twitteroauth.php'; } $settings = $this->get_settings_for_display(); $options = get_option('element_pack_api_settings'); $consumerKey = (!empty($options['twitter_consumer_key'])) ? $options['twitter_consumer_key'] : ''; $consumerSecret = (!empty($options['twitter_consumer_secret'])) ? $options['twitter_consumer_secret'] : ''; $accessToken = (!empty($options['twitter_access_token'])) ? $options['twitter_access_token'] : ''; $accessTokenSecret = (!empty($options['twitter_access_token_secret'])) ? $options['twitter_access_token_secret'] : ''; $twitter_name = (!empty($options['twitter_name'])) ? $options['twitter_name'] : ''; $this->render_loop_header(); if ($consumerKey and $consumerSecret and $accessToken and $accessTokenSecret) { $this->render_loop_twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret, $twitter_name); } else { ?> <div class="bdt-alert-warning" bdt-alert> <a class="bdt-alert-close" bdt-close></a> <?php $ep_setting_url = esc_url(admin_url('admin.php?page=element_pack_options#element_pack_api_settings')); ?> <p><?php printf(esc_html__('Please set your twitter API settings from here <a href="%s">element pack settings</a> to show your map correctly.', 'bdthemes-element-pack'), esc_url($ep_setting_url)); ?></p> </div> <?php } $this->render_footer(); } private function twitter_api_strip_emoji($text) { // four byte utf8: 11110www 10xxxxxx 10yyyyyy 10zzzzzz return preg_replace('/[\xF0-\xF7][\x80-\xBF]{3}/', '', $text); } private function process_links($tweet) { // Is the Tweet a ReTweet - then grab the full text of the original Tweet $fullText = isset($tweet->text) ? $tweet->text : (isset($tweet->full_text) ? $tweet->full_text : ''); if (isset($tweet->retweeted_status)) { // Split it so indices count correctly for @mentions etc. $rt_section = current(explode(":", $fullText)); $text = $rt_section . ": "; // Get Text $text .= $tweet->retweeted_status->text; } else { // Not a retweet - get Tweet $text = $fullText; } // NEW Link Creation from clickable items in the text $text = preg_replace('/((http)+(s)?:\/\/[^<>\s]+)/i', '<a href="$0" target="_blank" rel="nofollow">$0</a>', $text); // Clickable Twitter names $text = preg_replace('/[@]+([A-Za-z0-9-_]+)/', '<a href="http://twitter.com/$1" target="_blank" rel="nofollow">@$1</a>', $text); // Clickable Twitter hash tags $text = preg_replace('/[#]+([A-Za-z0-9-_]+)/', '<a href="http://twitter.com/search?q=%23$1" target="_blank" rel="nofollow">$0</a>', $text); // END TWEET CONTENT REGEX return $text; } protected function render_loop_header() { $id = 'bdt-twitter-slider-' . $this->get_id(); $settings = $this->get_settings_for_display(); $this->add_render_attribute('slider', 'id', $id); $this->add_render_attribute('slider', 'class', 'bdt-twitter-slider bdt-carousel'); if ('arrows' == $settings['navigation']) { $this->add_render_attribute('slider', 'class', 'bdt-arrows-align-' . $settings['arrows_position']); } elseif ('dots' == $settings['navigation']) { $this->add_render_attribute('slider', 'class', 'bdt-dots-align-' . $settings['dots_position']); } elseif ('both' == $settings['navigation']) { $this->add_render_attribute('slider', 'class', 'bdt-arrows-dots-align-' . $settings['both_position']); } elseif ('arrows-fraction' == $settings['navigation']) { $this->add_render_attribute('slider', 'class', 'bdt-arrows-dots-align-' . $settings['arrows_fraction_position']); } if ('arrows-fraction' == $settings['navigation']) { $pagination_type = 'fraction'; } elseif ('both' == $settings['navigation'] or 'dots' == $settings['navigation']) { $pagination_type = 'bullets'; } elseif ('progressbar' == $settings['navigation']) { $pagination_type = 'progressbar'; } else { $pagination_type = ''; } $this->add_render_attribute( [ 'slider' => [ 'data-settings' => [ wp_json_encode(array_filter([ 'autoplay' => ('yes' == $settings['autoplay']) ? ['delay' => $settings['autoplay_speed']] : false, 'loop' => ($settings['loop'] == 'yes') ? true : false, 'speed' => $settings['speed']['size'], 'pauseOnHover' => ('yes' == $settings['pauseonhover']) ? true : false, 'effect' => $settings['transition'], 'navigation' => [ 'nextEl' => '#' . $id . ' .bdt-navigation-next', 'prevEl' => '#' . $id . ' .bdt-navigation-prev', ], "pagination" => [ "el" => "#" . $id . " .swiper-pagination", "type" => $pagination_type, "clickable" => "true", 'autoHeight' => true, 'dynamicBullets' => ("yes" == $settings["dynamic_bullets"]) ? true : false, ], "scrollbar" => [ "el" => "#" . $id . " .swiper-scrollbar", "hide" => "true", ], ])) ] ] ] ); $swiper_class = Plugin::$instance->experiments->is_feature_active( 'e_swiper_latest' ) ? 'swiper' : 'swiper-container'; $this->add_render_attribute('swiper', 'class', 'swiper-carousel ' . $swiper_class); ?> <div <?php $this->print_render_attribute_string('slider'); ?>> <div <?php $this->print_render_attribute_string('swiper'); ?>> <div class="swiper-wrapper"> <?php } } <?php namespace ElementPack\Modules\TwitterSlider; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'twitter-slider'; } public function get_widgets() { $widgets = [ 'Twitter_Slider', ]; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Twitter Slider', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => false, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\AdvancedProgressBar\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Background; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Text_Shadow; use Elementor\Group_Control_Typography; use Elementor\Repeater; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Advanced_Progress_Bar extends Module_Base { public function get_name() { return 'bdt-advanced-progress-bar'; } public function get_title() { return BDTEP . esc_html__( 'Advanced Progress Bar', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-advanced-progress-bar'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'advanced bar', 'progress', 'skills', 'bars' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-styles' ]; } else { return [ 'ep-advanced-progress-bar' ]; } } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'ep-scripts' ]; } else { return [ 'ep-advanced-progress-bar' ]; } } public function get_custom_help_url() { return 'https://youtu.be/7hnmMdd2-Yo'; } protected function register_controls() { $this->start_controls_section( 'section_progress_bars', [ 'label' => esc_html__( 'Progress Bars', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $repeater = new Repeater(); $repeater->add_control( 'name', [ 'label' => esc_html__( 'Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Design', 'bdthemes-element-pack' ), 'placeholder' => esc_html__( 'Type a skill name', 'bdthemes-element-pack' ), ] ); $repeater->add_control( 'max_level', [ 'label' => esc_html__( 'Max Value', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => '%', 'size' => 100, ], 'size_units' => [ '%' ], 'range' => [ '%' => [ 'min' => 0, 'step' => 10, 'max' => 100, ], ], // 'selectors' => [ // '{{WRAPPER}} .bdt-ep-advanced-progress-bar-item{{CURRENT_ITEM}} ' => 'width: {{SIZE}}{{UNIT}};', // ], 'render_type' => 'template', ] ); $repeater->add_control( 'level', [ 'label' => esc_html__( 'Level (Out Of Max Value)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => '%', 'size' => 95, ], 'size_units' => [ '%' ], 'range' => [ '%' => [ 'min' => 0, 'max' => 100, ], ], ] ); $repeater->add_control( 'color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} {{CURRENT_ITEM}} .bdt-ep-advanced-progress-bar-content' => 'color: {{VALUE}}', ], ] ); $repeater->add_control( 'base_color', [ 'label' => esc_html__( 'Base Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} {{CURRENT_ITEM}} .bdt-ep-advanced-progress-bar-level' => 'background-color: {{VALUE}};', ], 'style_transfer' => true, ] ); $repeater->add_control( 'fill_color', [ 'label' => esc_html__( 'Fill Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} {{CURRENT_ITEM}} .bdt-ep-advanced-progress-bar-fill' => 'background-color: {{VALUE}};', ], 'style_transfer' => true, ] ); $this->add_control( 'progress_bars', [ 'show_label' => false, 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(), 'title_field' => '<# print((name || level.size) ? (name || "Skill") + " - " + level.size + level.unit : "Skill - 0%") #>', 'default' => [ [ 'name' => esc_html__( 'Design', 'bdthemes-element-pack' ), 'level' => [ 'size' => 97, 'unit' => '%' ], ], [ 'name' => esc_html__( 'UX', 'bdthemes-element-pack' ), 'level' => [ 'size' => 88, 'unit' => '%' ], ], [ 'name' => esc_html__( 'Coding', 'bdthemes-element-pack' ), 'level' => [ 'size' => 92, 'unit' => '%' ], ], [ 'name' => esc_html__( 'Speed', 'bdthemes-element-pack' ), 'level' => [ 'size' => 95, 'unit' => '%' ], ], [ 'name' => esc_html__( 'Passion', 'bdthemes-element-pack' ), 'level' => [ 'size' => 100, 'unit' => '%' ], ], ], ] ); $this->add_control( 'animation_speed', [ 'label' => esc_html__( 'Animation Speed (s)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => .1, 'max' => 10, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-fill' => '-webkit-transition: width {{SIZE}}s ease; -o-transition: width {{SIZE}}s ease; transition: width {{SIZE}}s ease;', ], ] ); $this->add_control( 'skills_style', [ 'type' => Controls_Manager::SELECT, 'label' => esc_html__( 'Progress Style', 'bdthemes-element-pack' ), 'separator' => 'before', 'default' => 'default', 'options' => [ 'default' => esc_html__( 'Default', 'bdthemes-element-pack' ), 'bdt-progress-with-perc' => esc_html__( 'Percentage With Progress', 'bdthemes-element-pack' ), 'bdt-progress-inner-perc' => esc_html__( 'Inner Content', 'bdthemes-element-pack' ), 'bdt-progress-inner-perc-and-name' => esc_html__( 'Inner Content Between', 'bdthemes-element-pack' ), ], 'style_transfer' => true, ] ); $this->add_control( 'text_position', [ 'type' => Controls_Manager::SELECT, 'label' => esc_html__( 'Text Position', 'bdthemes-element-pack' ), 'separator' => 'before', 'default' => 'outside-top', 'options' => [ 'outside-top' => esc_html__( 'Text Outside Top', 'bdthemes-element-pack' ), 'outside-bottom' => esc_html__( 'Text Outside Bottom', 'bdthemes-element-pack' ), ], 'style_transfer' => true, 'condition' => [ 'skills_style' => [ 'default', 'bdt-progress-with-perc' ], ], ] ); $this->add_control( 'skills_extra_style', [ 'type' => Controls_Manager::SELECT, 'label' => esc_html__( 'Additional Style', 'bdthemes-element-pack' ), 'separator' => 'before', 'default' => 'null', 'options' => [ 'null' => esc_html__( 'Default', 'bdthemes-element-pack' ), 'bdt-progress-fill-striped' => esc_html__( 'Striped', 'bdthemes-element-pack' ), 'bdt-progress-fill-striped bdt-progress-animated' => esc_html__( 'Striped With Animation', 'bdthemes-element-pack' ), 'bdt-progress-rainbow-animate' => esc_html__( 'Rainbow Animation', 'bdthemes-element-pack' ), ], 'style_transfer' => true, ] ); $this->add_control( 'rainbow_animation_speed', [ 'label' => esc_html__( 'Rainbow Animation Speed (s)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 1, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar.bdt-progress-rainbow-animate .bdt-ep-advanced-progress-bar-fill' => ' -webkit-animation: animateRainbow {{SIZE}}s ease infinite; animation: animateRainbow {{SIZE}}s ease infinite;', ], 'condition' => [ 'skills_extra_style' => [ 'bdt-progress-rainbow-animate' ], ], ] ); $this->add_control( 'show_perc', [ 'label' => esc_html__( 'Show Percentage', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_max_value', [ 'label' => esc_html__( 'Show Max Value', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'no', ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'section_style_progress_bars', [ 'label' => esc_html__( 'Progress Bars', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'height', [ 'label' => esc_html__( 'Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px' ], 'range' => [ 'px' => [ 'min' => 1, 'max' => 250, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-level' => 'height: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'spacing', [ 'label' => esc_html__( 'Spacing Between', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px' ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 250, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-item' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'border_radius_level', [ 'label' => esc_html__( 'Border Radius Level', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-level' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'border_radius', [ 'label' => esc_html__( 'Border Radius Fill', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-fill' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'box_shadow', 'exclude' => [ 'box_shadow_position', ], 'selector' => '{{WRAPPER}} .bdt-ep-advanced-progress-bar-level', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_style', [ 'label' => esc_html__( 'Content', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-content' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'progress_base_color', [ 'label' => esc_html__( 'Base Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-level' => 'background: {{VALUE}};', ], ] ); $this->add_control( 'show_progress_fill', [ 'label' => esc_html__( 'Fill Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'progress_fill_color', 'label' => esc_html__( 'Background', 'bdthemes-element-pack' ), 'types' => [ 'classic', 'gradient' ], 'selector' => '{{WRAPPER}} .bdt-ep-advanced-progress-bar-fill', 'condition' => [ 'skills_extra_style!' => [ 'bdt-progress-rainbow-animate' ], 'show_progress_fill' => 'yes' ], ] ); $this->add_control( 'rainbow_first_color', [ 'label' => esc_html__( 'Rainbow Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXTAREA, 'placeholder' => 'Input your colors. example: red, #9400d3, indigo', 'default' => 'red, orange, yellow, blue, indigo, violet', 'selectors' => [ '{{WRAPPER}} .bdt-progress-rainbow-animate .bdt-ep-advanced-progress-bar-fill' => 'background: linear-gradient(270deg, {{VALUE}} ); background-size: 300% 300%;', ], 'condition' => [ 'skills_extra_style' => [ 'bdt-progress-rainbow-animate' ], ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'info_typography', 'selector' => '{{WRAPPER}} .bdt-ep-advanced-progress-bar-content', ] ); $this->add_group_control( Group_Control_Text_Shadow::get_type(), [ 'name' => 'info_text_shadow', 'selector' => '{{WRAPPER}} .bdt-ep-advanced-progress-bar-content', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_percentage _style', [ 'label' => esc_html__( 'Percentage', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'skills_style' => [ 'bdt-progress-with-perc' ], ], ] ); $this->add_control( 'percentage_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-parcentage' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'percentage_bg_color', 'label' => esc_html__( 'Background', 'bdthemes-element-pack' ), 'types' => [ 'classic', 'gradient' ], 'selector' => '{{WRAPPER}} .bdt-ep-advanced-progress-bar-parcentage::before', ] ); $this->add_control( 'percentage_size', [ 'label' => esc_html__( 'Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px' ], 'range' => [ 'px' => [ 'min' => 20, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-parcentage' => 'height: {{SIZE}}{{UNIT}}; line-height: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-ep-advanced-progress-bar-parcentage::before' => 'height: {{SIZE}}{{UNIT}}; line-height: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'percentage_vertical_position', [ 'label' => esc_html__( 'Vertical Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px' ], 'range' => [ 'px' => [ 'min' => -100, 'max' => 150, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-parcentage' => 'top: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'percentage_horizontal_position', [ 'label' => esc_html__( 'Horizontal Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px' ], 'range' => [ 'px' => [ 'min' => -100, 'max' => 150, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-parcentage' => 'right: {{SIZE}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'percentage_typography', 'selector' => '{{WRAPPER}} .bdt-ep-advanced-progress-bar-parcentage, {{WRAPPER}} .bdt-ep-advanced-progress-bar-parcentage::before', ] ); $this->add_control( 'percentage_border_radius', [ 'label' => esc_html__( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-ep-advanced-progress-bar-parcentage::before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); } public function render() { $settings = $this->get_settings_for_display(); $this->add_render_attribute( 'progress-bar', 'class', 'bdt-ep-advanced-progress-bar ' . $settings['skills_style'] . ' ' . $settings['skills_extra_style'] . ' ' ); ?> <div <?php $this->print_render_attribute_string( 'progress-bar' ); ?>> <?php foreach ( $settings['progress_bars'] as $progress ) : ?> <!-- was here render-bak-code from below --> <?php if ( $settings['skills_style'] == 'bdt-progress-with-perc' ) { ?> <div class="bdt-ep-advanced-progress-bar-item elementor-repeater-item-<?php echo esc_attr( $progress['_id'] ); ?>"> <?php if ( $settings['text_position'] == 'outside-top' ) { ?> <div class="bdt-ep-advanced-progress-bar-content"> <span class="bdt-ep-advanced-progress-bar-name"> <?php echo esc_html( $progress['name'] ); ?> </span> </div> <?php } ?> <div class="bdt-ep-advanced-progress-bar-level"> <div class="bdt-ep-advanced-progress-bar-fill " data-max-value="<?php echo esc_attr( $progress['max_level']['size'] > 0 ? $progress['max_level']['size'] : '100' ); ?>" data-width="<?php echo esc_attr( $progress['level']['size'] ) ?>%"> <span class="bdt-ep-advanced-progress-bar-parcentage"> <?php echo esc_html( $progress['level']['size'] ); ?> <?php echo esc_html( $settings['show_max_value'] == 'yes' ? ' / ' . $progress['max_level']['size'] : '' ); ?> <?php echo ( $settings['show_perc'] == 'yes' ) ? '%' : ''; ?> </span> </div> </div> <?php if ( $settings['text_position'] == 'outside-bottom' ) { ?> <div class="bdt-ep-advanced-progress-bar-content"> <span class="bdt-ep-advanced-progress-bar-name"> <?php echo esc_html( $progress['name'] ); ?> </span> </div> <?php } ?> </div> <?php } elseif ( $settings['skills_style'] == 'bdt-progress-inner-perc' ) { ?> <div class="bdt-ep-advanced-progress-bar-item elementor-repeater-item-<?php echo esc_attr( $progress['_id'] ); ?>"> <div class="bdt-ep-advanced-progress-bar-level"> <div class="bdt-ep-advanced-progress-bar-fill " data-max-value="<?php echo esc_attr( $progress['max_level']['size'] > 0 ? $progress['max_level']['size'] : '100' ) ?>" data-width="<?php echo esc_attr( $progress['level']['size'] ) ?>%"> <div class="bdt-ep-advanced-progress-bar-content"> <span class="bdt-ep-advanced-progress-bar-name"> <?php echo esc_html( $progress['name'] ); ?> </span> <span class="bdt-ep-advanced-progress-bar-parcentage"> <?php echo esc_html( $progress['level']['size'] ); ?> <?php echo esc_html( $settings['show_max_value'] == 'yes' ? ' / ' . $progress['max_level']['size'] : '' ); ?> <?php echo ( $settings['show_perc'] == 'yes' ? '%' : '' ) ?> </span> </div> </div> </div> </div> <?php } elseif ( $settings['skills_style'] == 'bdt-progress-inner-perc-and-name' ) { ?> <div class="bdt-ep-advanced-progress-bar-item elementor-repeater-item-<?php echo esc_attr( $progress['_id'] ); ?>"> <div class="bdt-ep-advanced-progress-bar-level"> <div class="bdt-ep-advanced-progress-bar-fill " data-max-value="<?php echo esc_attr( $progress['max_level']['size'] > 0 ? $progress['max_level']['size'] : '100' ) ?>" data-width="<?php echo esc_attr( $progress['level']['size'] ) ?>%"> <div class="bdt-ep-advanced-progress-bar-content"> <span class="bdt-ep-advanced-progress-bar-name"> <?php echo esc_html( $progress['name'] ); ?> </span> <span class="bdt-ep-advanced-progress-bar-parcentage"> <?php echo esc_html( $progress['level']['size'] ); ?> <?php echo esc_html( $settings['show_max_value'] == 'yes' ? ' / ' . $progress['max_level']['size'] : '' ); ?> <?php echo ( $settings['show_perc'] == 'yes' ? '%' : '' ) ?> </span> </div> </div> </div> </div> <?php } else { ?> <div class="bdt-ep-advanced-progress-bar-item elementor-repeater-item-<?php echo esc_attr( $progress['_id'] ); ?>"> <?php if ( $settings['text_position'] == 'outside-top' ) { ?> <div class="bdt-ep-advanced-progress-bar-content"> <span class="bdt-ep-advanced-progress-bar-name"> <?php echo esc_html( $progress['name'] ); ?> </span> <span class="bdt-ep-advanced-progress-bar-parcentage"> <?php echo esc_html( $progress['level']['size'] ); ?> <?php echo esc_html( $settings['show_max_value'] == 'yes' ? ' / ' . $progress['max_level']['size'] : '' ); ?> <?php echo ( $settings['show_perc'] == 'yes' ? '%' : '' ) ?> </span> </div> <?php } ?> <div class="bdt-ep-advanced-progress-bar-level"> <div class="bdt-ep-advanced-progress-bar-fill " data-max-value="<?php echo esc_attr( $progress['max_level']['size'] > 0 ? $progress['max_level']['size'] : '100' ) ?>" data-width="<?php echo esc_attr( $progress['level']['size'] ) ?>%"> </div> </div> <?php if ( $settings['text_position'] == 'outside-bottom' ) { ?> <div class="bdt-ep-advanced-progress-bar-content"> <span class="bdt-ep-advanced-progress-bar-name"> <?php echo esc_html( $progress['name'] ); ?> </span> <span class="bdt-ep-advanced-progress-bar-parcentage"> <?php echo esc_html( $progress['level']['size'] ) ?> <?php echo esc_html( $settings['show_max_value'] == 'yes' ? ' / ' . $progress['max_level']['size'] : '' ); ?> <?php echo ( $settings['show_perc'] == 'yes' ? '%' : '' ) ?> </span> </div> <?php } ?> </div> <?php } ?> <?php endforeach; ?> </div> <?php } } <?php namespace ElementPack\Modules\AdvancedProgressBar; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'advanced-progress-bar'; } public function get_widgets() { $widgets = [ 'Advanced_Progress_Bar', ]; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Advanced Progress Bar', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => false, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\CursorEffects; use Elementor\Controls_Manager; use Elementor\Utils; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Border; use Elementor\Group_Control_Background; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function __construct() { parent::__construct(); $this->add_actions(); } public function get_name() { return 'bdt-cursor-effects'; } public function register_section($element) { $element->start_controls_section( 'element_pack_cursor_effects_section', [ 'tab' => Controls_Manager::TAB_ADVANCED, 'label' => BDTEP_CP . esc_html__('Cursor Effects', 'bdthemes-element-pack'), ] ); $element->end_controls_section(); } public function register_controls($section, $args) { $section->add_control( 'element_pack_cursor_effects_show', [ 'label' => __('Show Cursor Effects', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'prefix_class' => 'bdt-cursor-effects-', 'frontend_available' => true, 'render_type' => 'template', ] ); $section->start_controls_tabs( 'element_pack_cursor_effects_tabs' ); $section->start_controls_tab( 'element_pack_cursor_effects_tab_layout', [ 'label' => esc_html__('Layout', 'bdthemes-element-pack'), 'condition' => [ 'element_pack_cursor_effects_show' => 'yes' ], ] ); $section->add_control( 'element_pack_cursor_effects_source', [ 'label' => esc_html__('Source', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'default', 'frontend_available' => true, 'render_type' => 'none', 'options' => [ 'default' => esc_html__('Default', 'bdthemes-element-pack'), 'text' => esc_html__('Text', 'bdthemes-element-pack'), 'image' => esc_html__('Image', 'bdthemes-element-pack'), 'icons' => esc_html__('Icons', 'bdthemes-element-pack'), ], 'condition' => [ 'element_pack_cursor_effects_show' => 'yes' ], ] ); $section->add_control( 'element_pack_cursor_effects_image_src', [ 'label' => esc_html__('Image', 'bdthemes-element-pack'), 'type' => Controls_Manager::MEDIA, 'frontend_available' => true, 'render_type' => 'template', 'default' => [ 'url' => Utils::get_placeholder_image_src(), ], 'condition' => [ 'element_pack_cursor_effects_source' => 'image' ] ] ); $section->add_control( 'element_pack_cursor_effects_icons', [ 'label' => esc_html__('Icons', 'bdthemes-element-pack'), 'type' => Controls_Manager::ICONS, 'frontend_available' => true, 'render_type' => 'template', 'condition' => [ 'element_pack_cursor_effects_source' => 'icons' ], 'default' => [ 'value' => 'fas fa-laugh-wink', 'library' => 'fa-solid', ], ] ); $section->add_control( 'element_pack_cursor_effects_style', [ 'label' => __('Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'ep-cursor-style-1', 'options' => [ 'ep-cursor-style-1' => __('Style 1', 'bdthemes-element-pack'), 'ep-cursor-style-2' => __('Style 2', 'bdthemes-element-pack'), 'ep-cursor-style-3' => __('Style 3', 'bdthemes-element-pack'), ], 'frontend_available' => true, 'render_type' => 'template', 'condition' => [ 'element_pack_cursor_effects_show' => 'yes', 'element_pack_cursor_effects_source' => 'default' ] ] ); $section->add_control( 'element_pack_cursor_effects_text_label', [ 'label' => esc_html__('Text Label', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, // 'default' => esc_html__('HELLO', 'bdthemes-element-pack'), 'frontend_available' => true, 'render_type' => 'template', 'condition' => [ 'element_pack_cursor_effects_source' => 'text', 'element_pack_cursor_effects_show' => 'yes' ] ] ); $section->add_control( 'element_pack_cursor_effects_speed', [ 'label' => __('Speed', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'size_units' => ['px'], 'range' => [ 'px' => [ 'min' => 0, 'max' => 1, 'step' => 0.001, ] ], 'default' => [ 'unit' => 'px', 'size' => 0.075, ], 'frontend_available' => true, 'render_type' => 'none', 'condition' => [ 'element_pack_cursor_effects_show' => 'yes', 'element_pack_cursor_effects_source' => 'default' ] ] ); $section->add_control( 'element_pack_cursor_effects_disable_default_cursor', [ 'label' => __('Disable Default Cursor', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'separator' => 'before', 'condition' => [ 'element_pack_cursor_effects_show' => 'yes' ], 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes' => 'cursor: none' ] ] ); $section->end_controls_tab(); $section->start_controls_tab( 'element_pack_cursor_effects_tab_style', [ 'label' => esc_html__('Style', 'bdthemes-element-pack'), 'condition' => [ 'element_pack_cursor_effects_show' => 'yes' ], ] ); $section->add_control( 'element_pack_cursor_effects_primary', [ 'label' => esc_html__('Primary', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, 'separator' => 'before', 'condition' => [ 'element_pack_cursor_effects_source' => 'default' ] ] ); $section->add_control( 'element_pack_cursor_effects_primary_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes' => '--cursor-ball-color: {{VALUE}}', ], 'condition' => [ 'element_pack_cursor_effects_source' => ['default', 'icons'] ] ] ); $section->add_responsive_control( 'element_pack_cursor_effects_primary_size', [ 'label' => esc_html__('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes' => '--cursor-ball-size:{{SIZE}}{{UNIT}};', ], 'condition' => [ 'element_pack_cursor_effects_source' => 'default' ] ] ); $section->add_control( 'element_pack_cursor_effects_secondary', [ 'label' => esc_html__('Secondary', 'bdthemes-element-pack'), 'type' => Controls_Manager::HEADING, 'separator' => 'before', 'condition' => [ 'element_pack_cursor_effects_source' => 'default' ] ] ); $section->add_control( 'element_pack_cursor_effects_secondary_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes' => '--cursor-circle-color: {{VALUE}}', ], 'condition' => [ 'element_pack_cursor_effects_source' => 'default' ] ] ); $section->add_responsive_control( 'element_pack_cursor_effects_secondary_size', [ 'label' => esc_html__('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes' => '--cursor-circle-size:{{SIZE}}{{UNIT}};', ], 'condition' => [ 'element_pack_cursor_effects_source' => 'default' ] ] ); //TEXT $section->add_control( 'element_pack_cursor_effects_text_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes .bdt-cursor-text' => 'color: {{VALUE}}', ], 'condition' => [ 'element_pack_cursor_effects_source' => 'text' ] ] ); $section->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'element_pack_cursor_effects_text_background', 'label' => esc_html__('Background', 'bdthemes-element-pack'), 'types' => ['classic', 'gradient'], 'selector' => '{{WRAPPER}}.bdt-cursor-effects-yes .bdt-cursor-text', 'condition' => [ 'element_pack_cursor_effects_source' => 'text' ] ] ); $section->add_responsive_control( 'element_pack_cursor_effects_text_padding', [ 'label' => esc_html__('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes .bdt-cursor-text' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'element_pack_cursor_effects_source' => 'text' ] ] ); $section->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'element_pack_cursor_effects_text_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}}.bdt-cursor-effects-yes .bdt-cursor-text', 'condition' => [ 'element_pack_cursor_effects_source' => 'text' ] ] ); $section->add_responsive_control( 'element_pack_cursor_effects_text_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes .bdt-cursor-text' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'element_pack_cursor_effects_source' => 'text' ] ] ); $section->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'element_pack_cursor_effects_text_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}}.bdt-cursor-effects-yes .bdt-cursor-text', 'condition' => [ 'element_pack_cursor_effects_source' => 'text' ] ] ); $section->add_responsive_control( 'element_pack_cursor_effects_image_size', [ 'label' => esc_html__('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes .bdt-cursor-image' => 'width:{{SIZE}}{{UNIT}}; height:{{SIZE}}{{UNIT}};', ], 'condition' => [ 'element_pack_cursor_effects_source' => 'image' ] ] ); $section->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'element_pack_cursor_effects_image_border', 'label' => esc_html__('Border', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}}.bdt-cursor-effects-yes .bdt-cursor-image', 'condition' => [ 'element_pack_cursor_effects_source' => 'image' ] ] ); $section->add_responsive_control( 'element_pack_cursor_effects_image_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes .bdt-cursor-image' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ 'element_pack_cursor_effects_source' => 'image' ] ] ); $section->add_responsive_control( 'element_pack_cursor_effects_icons_size', [ 'label' => esc_html__('Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}}.bdt-cursor-effects-yes .bdt-cursor-icons' => 'font-size:{{SIZE}}{{UNIT}};', ], 'condition' => [ 'element_pack_cursor_effects_source' => 'icons' ] ] ); $section->end_controls_tab(); $section->end_controls_tabs(); } public function enqueue_scripts() { wp_enqueue_script('cotton-js', BDTEP_ASSETS_URL . 'vendor/js/cotton.min.js', '5.3.5', true); } public function should_script_enqueue($section) { if ('yes' === $section->get_settings_for_display('element_pack_cursor_effects_show')) { $this->enqueue_scripts(); wp_enqueue_style('ep-cursor-effects'); wp_enqueue_script('ep-cursor-effects'); } } protected function add_actions() { add_action('elementor/element/common/_section_style/after_section_end', [$this, 'register_section']); add_action('elementor/element/common/element_pack_cursor_effects_section/before_section_end', [$this, 'register_controls'], 10, 2); // render scripts add_action('elementor/frontend/widget/before_render', [$this, 'should_script_enqueue']); add_action('elementor/preview/enqueue_scripts', [$this, 'enqueue_scripts']); } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Cursor Effects', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\BuddypressGroup\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Border; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Buddypress_Group extends Module_Base { public function get_name() { return 'bdt-buddypress-group'; } public function get_title() { return BDTEP . esc_html__( 'BuddyPress Group', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-buddypress-group'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'buddypress', 'user', 'group', 'activity', 'streams', 'profiles' ]; } public function get_custom_help_url() { return 'https://youtu.be/CccODcBw_9w'; } protected function register_controls() { $this->start_controls_section( 'section_content_layout', [ 'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ), ] ); $this->add_control( 'groups_type', [ 'label' => esc_html__( 'Groups Type', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'newest', 'options' => [ 'newest' => esc_html__('Newest', 'bdthemes-element-pack'), 'popular' => esc_html__('Popular', 'bdthemes-element-pack'), 'active' => esc_html__('Active', 'bdthemes-element-pack'), ], ] ); $this->add_responsive_control( 'max_groups', [ 'label' => esc_html__( 'Max Groups', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 5, ], 'range' => [ 'px' => [ 'min' => 1, 'max' => 20, 'step' => 1, ], ], ] ); $this->add_responsive_control( 'columns', [ 'label' => esc_html__( 'Columns', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => '6', 'tablet_default' => '4', 'mobile_default' => '2', 'options' => [ '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', 'auto' => 'Auto', ], ] ); $this->add_responsive_control( 'column_gap', [ 'label' => esc_html__( 'Column Gap', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 15, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, 'step' => 5, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-buddypress-groups .bdt-grid' => 'margin-left: -{{SIZE}}px', '{{WRAPPER}} .bdt-buddypress-groups .bdt-grid > *' => 'padding-left: {{SIZE}}px', ], ] ); $this->add_responsive_control( 'row_gap', [ 'label' => esc_html__( 'Row Gap', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 15, ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, 'step' => 5, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-buddypress-groups .bdt-grid' => 'margin-top: -{{SIZE}}px', '{{WRAPPER}} .bdt-buddypress-groups .bdt-grid > *' => 'margin-top: {{SIZE}}px', ], ] ); $this->add_control( 'align', [ 'label' => __( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], ], 'default' => 'center', ] ); $this->add_control( 'show_avatar', [ 'label' => esc_html__( 'Show Avatar', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_title', [ 'label' => esc_html__( 'Show Title', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_meta_as_tooltip', [ 'label' => esc_html__( 'Show Meta as Tooltip', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_avatar', [ 'label' => esc_html__( 'Avatar', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_avatar' => 'yes', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'avatar_border', 'label' => __( 'Border', 'bdthemes-element-pack' ), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-bp-group-avatar img', 'separator' => 'before', ] ); $this->add_control( 'avatar_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-bp-group-avatar img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;', ], ] ); $this->add_control( 'avatar_opacity', [ 'label' => __( 'Opacity (%)', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-bp-group-avatar img' => 'opacity: {{SIZE}};', ], ] ); $this->add_control( 'avatar_spacing', [ 'label' => __( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-bp-group-avatar img' => 'margin-bottom: {{SIZE}}{{UNIT}}', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_title', [ 'label' => __( 'Title', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_title' => 'yes', ], ] ); $this->add_control( 'title_color', [ 'label' => __( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-bp-group-title a' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography', 'selector' => '{{WRAPPER}} .bdt-bp-group-title a', //'scheme' => Schemes\Typography::TYPOGRAPHY_4, ] ); $this->end_controls_section(); } public function render() { $settings = $this->get_settings_for_display(); $type = $settings['groups_type']; $groups_args = array( 'user_id' => 0, 'type' => esc_attr($type), 'per_page' => esc_attr($settings['max_groups']['size']), 'max' => esc_attr($settings['max_groups']['size']), ); if ( bp_is_active( 'groups' ) and bp_has_groups( $groups_args ) ) : ?> <div class="bdt-buddypress-groups"> <div class="bdt-grid bdt-grid-small bdt-text-<?php echo esc_attr($settings['align']); ?> bdt-flex-<?php echo esc_attr($settings['align']); ?>" bdt-grid> <?php while ( bp_groups() ) : bp_the_group(); ?> <?php $this->add_render_attribute('bp-group', 'class', 'bdt-bp-group'); if ('auto' !== $settings['columns']) { $columns_mobile = isset($settings['columns_mobile']) ? $settings['columns_mobile'] : 2; $columns_tablet = isset($settings['columns_tablet']) ? $settings['columns_tablet'] : 4; $columns = isset($settings['columns']) ? $settings['columns'] : 6; $this->add_render_attribute('bp-group', 'class', 'bdt-width-1-'. $columns_mobile); $this->add_render_attribute('bp-group', 'class', 'bdt-width-1-'. $columns_tablet .'@s'); $this->add_render_attribute('bp-group', 'class', 'bdt-width-1-'. $columns .'@m'); } else { $this->add_render_attribute('bp-group', 'class', 'bdt-width-auto'); } ?> <?php if ($settings['show_meta_as_tooltip']) : ?> <?php if ( 'active' === $type ) : ?> <?php $this->add_render_attribute('bp-group', 'bdt-tooltip', 'title: ' . bp_get_group_last_active(), true); ?> <?php elseif ( 'newest' === $type ) : ?> <?php $this->add_render_attribute('bp-group', 'bdt-tooltip', 'title: ' . bp_get_group_date_created(), true); ?> <?php elseif ( 'popular' === $type ) : ?> <?php $this->add_render_attribute('bp-group', 'bdt-tooltip', 'title: ' . bp_get_group_member_count(), true); ?> <?php endif; ?> <?php endif; ?> <div <?php $this->print_render_attribute_string('bp-group'); ?>> <?php if ($settings['show_avatar']) : ?> <div class="bdt-bp-group-avatar"> <a href="<?php bp_group_permalink() ?>"><?php bp_group_avatar_thumb() ?></a> </div> <?php endif; ?> <?php if ($settings['show_title']) : ?> <div class="bdt-bp-group-title"><?php bp_group_link(); ?></div> <?php endif; ?> </div> <?php endwhile; ?> </div> </div> <?php else: ?> <div class="bdt-alert-warning" bdt-alert>No groups matched the current filter or group not active. </div> <?php endif; } } <?php namespace ElementPack\Modules\BuddypressGroup; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'buddypress-group'; } public function get_widgets() { $widgets = ['Buddypress_Group']; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Buddypress Group', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, ]; <?php namespace ElementPack\Modules\BackgroundOverlay; use Elementor\Controls_Manager; use Elementor\Group_Control_Background; use Elementor\Group_Control_Css_Filter; use Elementor\Plugin; use ElementPack; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function __construct() { parent::__construct(); $this->add_actions(); } public function get_name() { return 'bdt-background-overlay'; } // public function get_extension_script_depends() { // return ['ep-background-overlay']; // } public function register_controls($widget, $args) { $widget->start_controls_section( 'ep_section_background_overlay', [ 'label' => BDTEP_CP . esc_html__('Background Over/Underlay', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_ADVANCED, 'condition' => [ '_background_background' => ['classic', 'gradient'], ], ] ); $widget->start_controls_tabs('ep_tabs_background_overlay'); $widget->start_controls_tab( 'ep_tab_background_overlay_normal', [ 'label' => esc_html__('Normal', 'bdthemes-element-pack'), ] ); $widget->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'ep_background_overlay', 'selector' => '{{WRAPPER}}.bdt-background-overlay-yes > .elementor-widget-container:before', ] ); $widget->add_control( 'ep_background_overlay_opacity', [ 'label' => esc_html__('Opacity', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => .5, ], 'range' => [ 'px' => [ 'max' => 1, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}}.bdt-background-overlay-yes > .elementor-widget-container:before' => 'opacity: {{SIZE}};', ], 'condition' => [ 'ep_background_overlay_background' => ['classic', 'gradient'], ], ] ); $widget->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'ep_css_filters', 'selector' => '{{WRAPPER}}.bdt-background-overlay-yes > .elementor-widget-container:before', ] ); $widget->add_control( 'ep_overlay_blend_mode', [ 'label' => esc_html__('Blend Mode', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => [ '' => esc_html__('Normal', 'bdthemes-element-pack'), 'multiply' => 'Multiply', 'screen' => 'Screen', 'overlay' => 'Overlay', 'darken' => 'Darken', 'lighten' => 'Lighten', 'color-dodge' => 'Color Dodge', 'saturation' => 'Saturation', 'color' => 'Color', 'luminosity' => 'Luminosity', ], 'selectors' => [ '{{WRAPPER}}.bdt-background-overlay-yes > .elementor-widget-container:before' => 'mix-blend-mode: {{VALUE}}', ], ] ); $widget->add_responsive_control( 'ep_background_overlay_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'separator' => 'before', 'selectors' => [ '{{WRAPPER}}.bdt-background-overlay-yes > .elementor-widget-container:before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $widget->end_controls_tab(); $widget->start_controls_tab( 'ep_tab_background_overlay_hover', [ 'label' => esc_html__('Hover', 'bdthemes-element-pack'), ] ); $widget->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'ep_background_overlay_hover', 'selector' => '{{WRAPPER}}.bdt-background-overlay-yes:hover > .elementor-widget-container:before', ] ); $widget->add_control( 'ep_background_overlay_hover_opacity', [ 'label' => esc_html__('Opacity', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => .5, ], 'range' => [ 'px' => [ 'max' => 1, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}}.bdt-background-overlay-yes:hover > .elementor-widget-container:before' => 'opacity: {{SIZE}};', ], 'condition' => [ 'ep_background_overlay_hover_background' => ['classic', 'gradient'], ], ] ); $widget->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'ep_css_filters_hover', 'selector' => '{{WRAPPER}}.bdt-background-overlay-yes:hover > .elementor-widget-container:before', ] ); // $widget->add_control( // 'ep_background_overlay_hover_transition', // [ // 'label' => esc_html__( 'Transition', 'bdthemes-element-pack' ), // 'type' => Controls_Manager::SELECT, // 'options' => [ // '' => esc_html__( 'None', 'bdthemes-element-pack' ), // 'zoom' => 'Zoom', // 'rotate' => 'Rotate', // ], // 'prefix_class' => 'bdt-bg-o-t-', // // ] // ); $widget->add_control( 'ep_background_overlay_hover_transition_duration', [ 'label' => esc_html__('Transition Duration', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 0.3, ], 'range' => [ 'px' => [ 'max' => 3, 'step' => 0.1, ], ], 'separator' => 'before', 'selectors' => [ '{{WRAPPER}}.bdt-background-overlay-yes > .elementor-widget-container:before' => 'transition: background {{SIZE}}s;', ] ] ); $widget->add_responsive_control( 'ep_background_overlay_hover_radius', [ 'label' => esc_html__('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'separator' => 'before', 'selectors' => [ '{{WRAPPER}}.bdt-background-overlay-yes > .elementor-widget-container:hover:before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $widget->end_controls_tab(); $widget->end_controls_tabs(); $widget->add_responsive_control( 'ep_background_overlay_margin', [ 'label' => esc_html__('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'separator' => 'before', 'selectors' => [ '{{WRAPPER}}' => '--ep-overlay-margin-top: {{TOP}}{{UNIT}}; --ep-overlay-margin-right: {{RIGHT}}{{UNIT}}; --ep-overlay-margin-bottom: {{BOTTOM}}{{UNIT}}; --ep-overlay-margin-left: {{LEFT}}{{UNIT}};', ], ] ); $widget->add_control( 'ep_background_overlay_zindex', [ 'label' => esc_html__('Z-Index', 'bdthemes-element-pack'), 'type' => Controls_Manager::NUMBER, 'dynamic' => [ 'active' => true, ], 'selectors' => [ '{{WRAPPER}}.bdt-background-overlay-yes > .elementor-widget-container:before' => 'z-index: {{VALUE}};', ] ] ); $widget->add_control( 'ep_background_overlay_position_relative', [ 'label' => esc_html__('Position Relative', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'selectors' => [ '{{WRAPPER}}.bdt-background-overlay-yes > .elementor-widget-container' => 'position: relative;', ] ] ); $widget->add_control( 'ep_background_overlay_widget_zindex', [ 'label' => esc_html__('Widget Z-Index', 'bdthemes-element-pack'), 'type' => Controls_Manager::NUMBER, 'default' => '-1', 'dynamic' => [ 'active' => true, ], 'condition' => ['ep_background_overlay_position_relative' => 'yes'], 'selectors' => [ '{{WRAPPER}}.bdt-background-overlay-yes > .elementor-widget-container' => 'z-index: {{VALUE}};', ] ] ); $widget->end_controls_section(); } public function background_overlay_render($widget) { $settings = $widget->get_settings_for_display(); if (in_array($widget->get_name(), ['column', 'section'])) { return; } if (Plugin::instance()->editor->is_edit_mode()) { return; } $overlay_bg = isset($settings['ep_background_overlay_background']) ? $settings['ep_background_overlay_background'] : ''; $overlay_bg_hover = isset($settings['ep_background_overlay_hover_background']) ? $settings['ep_background_overlay_hover_background'] : ''; $has_background_overlay = (in_array($overlay_bg, ['classic', 'gradient'], true) || in_array($overlay_bg_hover, ['classic', 'gradient'], true)); if ($has_background_overlay) { $widget->add_render_attribute('_wrapper', 'class', 'bdt-background-overlay-yes'); wp_enqueue_script('ep-background-overlay'); } } protected function add_actions() { add_action('elementor/element/common/_section_background/after_section_end', [$this, 'register_controls'], 10, 2); add_action('elementor/element/after_add_attributes', [$this, 'background_overlay_render'], 10, 1); } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Background Overlay', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\ReadingProgress\Widgets; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Box_Shadow; use ElementPack\Base\Module_Base; use ElementPack\Modules\ReadingProgress\Skins; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Reading_Progress extends Module_Base { public function get_name() { return 'bdt-reading-progress'; } public function get_id() { return 'bdt-reading-progress'; } public function get_title() { return BDTEP . esc_html__( 'Reading Progress', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-reading-progress'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'read', 'reading', 'progress', 'scroll' ]; } public function get_style_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'elementor-icons-fa-solid', 'ep-font', 'ep-styles' ]; } else { return [ 'elementor-icons-fa-solid', 'ep-font', 'ep-reading-progress' ]; } } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'progressHorizontal', 'ep-scripts' ]; } else { return [ 'progressHorizontal', 'ep-reading-progress' ]; } } public function register_skins() { $this->add_skin( new Skins\Back_To_Top_With_Progress( $this ) ); $this->add_skin( new Skins\Horizontal_Progress( $this ) ); $this->add_skin( new Skins\Progress_With_Cursor( $this ) ); } public function get_custom_help_url() { return 'https://youtu.be/cODL1E2f9FI'; } protected function register_controls() { $this->start_controls_section( 'reading_progress_layout', [ 'label' => esc_html__( 'Reading Progress', 'bdthemes-dark-mode' ), ] ); $this->add_control( 'progress_position', [ 'label' => __( 'Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'bottom-right', 'options' => [ 'bottom-right' => __( 'Bottom Right', 'bdthemes-element-pack' ), 'bottom-left' => __( 'Bottom Left', 'bdthemes-element-pack' ), 'top-right' => __( 'Top Right', 'bdthemes-element-pack' ), 'top-left' => __( 'Top Left', 'bdthemes-element-pack' ), ], 'condition' => [ '_skin' => [ '', 'bdt-back-to-top-with-progress' ], ], ] ); $this->add_responsive_control( 'reading_progress_font_area_size', [ 'label' => __( 'Primary Circle Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 80, ], 'range' => [ 'px' => [ 'min' => 40, 'max' => 140, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-reading-progress .bdt-reading-progress-circle' => 'height: {{SIZE}}{{UNIT}} !important; width: {{SIZE}}{{UNIT}} !important;', ], 'condition' => [ '_skin' => '', ], ] ); $this->add_responsive_control( 'reading_progress_size', [ 'label' => __( 'Secondary Circle Size', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 90, ], 'range' => [ 'px' => [ 'min' => 50, 'max' => 150, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-reading-progress ' => 'height: {{SIZE}}{{UNIT}} !important; width: {{SIZE}}{{UNIT}} !important;', '{{WRAPPER}} .bdt-progress-with-top .bdt-progress-wrap, {{WRAPPER}} .bdt-progress-with-top .bdt-progress-wrap::before ' => 'height: {{SIZE}}{{UNIT}} ; width: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-progress-with-top .bdt-progress-wrap::before ' => 'line-height: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .bdt-progress-with-cursor .bdt-progress-wrap' => 'height: {{SIZE}}{{UNIT}} !important; width: {{SIZE}}{{UNIT}} !important; ', '{{WRAPPER}} .bdt-progress-with-cursor .bdt-cursor2, .bdt-progress-with-cursor .bdt-cursor3' => 'height: {{SIZE}}{{UNIT}} !important; width: {{SIZE}}{{UNIT}} !important; ', ], 'condition' => [ '_skin' => [ '', 'bdt-back-to-top-with-progress', 'bdt-progress-with-cursor' ], ], ] ); $this->add_responsive_control( 'reading_progress_horizontal_offset', [ 'label' => __( 'Horizontal Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -150, 'max' => 150, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-reading-progress ' => 'transform: translateX({{SIZE}}{{UNIT}});', '{{WRAPPER}} .bdt-progress-with-top .bdt-progress-wrap' => 'transform: translateX({{SIZE}}{{UNIT}});', ], 'condition' => [ '_skin' => [ '', 'bdt-back-to-top-with-progress' ], ], ] ); $this->add_responsive_control( 'reading_progress_vertical_offset', [ 'label' => __( 'Vertical Offset', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => -150, 'max' => 150, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-reading-progress' => 'margin: {{SIZE}}{{UNIT}} 0px;', '{{WRAPPER}} .bdt-progress-with-top .bdt-progress-wrap' => 'margin: {{SIZE}}{{UNIT}} 0px;', ], 'condition' => [ '_skin' => [ '', 'bdt-back-to-top-with-progress' ], ], ] ); $this->add_control( 'horizontal_reading_progress_position', [ 'label' => __( 'Position', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'top', 'options' => [ 'top' => __( 'Top ', 'bdthemes-element-pack' ), 'bottom' => __( 'Bottom', 'bdthemes-element-pack' ), ], 'condition' => [ '_skin' => 'bdt-horizontal-progress', ], ] ); $this->add_responsive_control( 'horizontal_reading_progress_size', [ 'label' => __( 'Height', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px', '%' ], 'range' => [ 'px' => [ 'min' => 1, 'max' => 10, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-horizontal-progress' => 'height: {{SIZE}}{{UNIT}} !important;', ], 'condition' => [ '_skin' => 'bdt-horizontal-progress', ], ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'reading_progress_style_default', [ 'label' => __( 'Additional', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'reading_progress_value_color', [ 'label' => __( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-reading-progress .bdt-reading-progress-border .bdt-reading-progress-circle .bdt-reading-progress-text' => 'color: {{VALUE}}', '{{WRAPPER}} .bdt-progress-with-top .bdt-progress-wrap::before' => 'background-color: {{VALUE}}', ], 'condition' => [ '_skin' => [ '', 'bdt-back-to-top-with-progress' ], ], ] ); $this->add_control( 'reading_progress_bg', [ 'label' => __( 'Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#08AEEC', 'selectors' => [ '{{WRAPPER}} .bdt-horizontal-progress' => 'background-color: {{VALUE}} !important', ], 'condition' => [ '_skin' => [ '', 'bdt-horizontal-progress' ], ], ] ); $this->add_control( 'reading_progress_font_area_bg', [ 'label' => __( 'Secondary Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#54595F', 'selectors' => [ '{{WRAPPER}} .bdt-reading-progress .bdt-reading-progress-border .bdt-reading-progress-circle ' => 'background-color: {{VALUE}}', ], 'condition' => [ '_skin' => '', ], ] ); $this->add_control( 'reading_progress_bg_scroll', [ 'label' => __( 'Active Background', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '#FF0000', 'selectors' => [ '{{WRAPPER}} .bdt-horizontal-progress .inner' => 'background-color: {{VALUE}} !important', '{{WRAPPER}} .bdt-progress-with-top .bdt-progress-wrap svg.bdt-progress-circle path' => 'stroke: {{VALUE}}', '{{WRAPPER}} .bdt-progress-with-cursor .bdt-progress-wrap svg.bdt-progress-circle path' => 'stroke: {{VALUE}}', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'reading_progress_border', 'label' => esc_html__( 'Border', 'bdthemes-element-pack' ), 'selector' => '{{WRAPPER}} .bdt-reading-progress .bdt-reading-progress-circle', 'condition' => [ '_skin' => '', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'cursor_with_progress_reading_bg', 'selector' => '{{WRAPPER}} .bdt-progress-with-cursor .bdt-progress-wrap, {{WRAPPER}} .bdt-progress-with-top .bdt-progress-wrap', 'condition' => [ '_skin' => [ 'bdt-back-to-top-with-progress', 'bdt-progress-with-cursor' ], ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'reading_progress_value', 'selector' => '{{WRAPPER}} .bdt-reading-progress .bdt-reading-progress-border .bdt-reading-progress-circle .bdt-reading-progress-text, {{WRAPPER}} .bdt-progress-with-top .bdt-progress-wrap::before', 'condition' => [ '_skin' => [ '', 'bdt-back-to-top-with-progress' ], ], ] ); $this->end_controls_section(); } public function render() { $settings = $this->get_settings_for_display(); $position = $settings['progress_position']; $this->add_render_attribute( 'reading-progress', 'class', 'bdt-reading-progress' ); $this->add_render_attribute( 'reading-progress', 'class', $position ); $this->add_render_attribute( [ 'reading-progress' => [ 'data-settings' => [ wp_json_encode( array_filter( [ "progress_bg" => $settings['reading_progress_bg'], "scroll_bg" => $settings['reading_progress_bg_scroll'], ] ) ), ], ], ] ); ?> <div <?php $this->print_render_attribute_string( 'reading-progress' ); ?>></div> <?php } } <?php namespace ElementPack\Modules\ReadingProgress; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'reading-progress'; } public function get_widgets() { $widgets = ['Reading_Progress']; return $widgets; } } <?php namespace ElementPack\Modules\ReadingProgress\Skins; use Elementor\Controls_Manager; use Elementor\Group_Control_Box_Shadow; use Elementor\Skin_Base as Elementor_Skin_Base; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class Progress_With_Cursor extends Elementor_Skin_Base { public function get_id() { return 'bdt-progress-with-cursor'; } public function get_title() { return __('Progress With Cursor', 'bdthemes-element-pack'); } public function render() { ?> <div class="bdt-progress-with-cursor"> <div class='bdt-cursor'></div> <div class='bdt-cursor2'> <div class="bdt-progress-wrap"> <svg class="bdt-progress-circle svg-content" width="100%" height="100%" viewBox="-1 -1 102 102"> <path d="M50,1 a49,49 0 0,1 0,98 a49,49 0 0,1 0,-98"/> </svg> </div> </div> <div class='bdt-cursor3'></div> </div> <?php } } <?php namespace ElementPack\Modules\ReadingProgress\Skins; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Skin_Base as Elementor_Skin_Base; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class Horizontal_Progress extends Elementor_Skin_Base { public function get_id() { return 'bdt-horizontal-progress'; } public function get_title() { return __('Horizontal Progress', 'bdthemes-element-pack'); } public function render() { $settings = $this->parent->get_settings(); $position = $settings['horizontal_reading_progress_position']; ?> <div class="bdt-horizontal-progress <?php echo esc_attr($position); ?>" id="bdt-progress"> </div> <?php } } <?php namespace ElementPack\Modules\ReadingProgress\Skins; use Elementor\Controls_Manager; use Elementor\Group_Control_Border; use Elementor\Skin_Base as Elementor_Skin_Base; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Back_To_Top_With_Progress extends Elementor_Skin_Base { public function get_id() { return 'bdt-back-to-top-with-progress'; } public function get_title() { return __( 'Back To Top With Progress', 'bdthemes-element-pack' ); } public function render() { $position_progress = $this->parent->get_settings( 'progress_position' ); ?> <div class="bdt-progress-with-top"> <div class="bdt-progress-wrap <?php echo esc_attr($position_progress); ?>"> <svg class="bdt-progress-circle svg-content" width="100%" height="100%" viewBox="-1 -1 102 102"> <path d="M50,1 a49,49 0 0,1 0,98 a49,49 0 0,1 0,-98" /> </svg> </div> </div> <?php } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Reading Progress', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => false, 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\Notation; use Elementor\Controls_Manager; use Elementor\Repeater; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) { exit; } // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function __construct() { parent::__construct(); $this->add_actions(); } public function get_name() { return 'bdt-notation'; } public function register_section($element) { $element->start_controls_section( 'section_element_pack_notation_controls', [ 'label' => BDTEP_CP . esc_html__('Notation', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $element->end_controls_section(); } public function register_controls($widget, $args) { $widget->add_control( 'ep_notation_active', [ 'label' => esc_html__('Notation Effects', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'render_type' => 'template', 'frontend_available' => true, ] ); $repeater = new Repeater(); $repeater->add_control( 'ep_notation_select_type', [ 'label' => esc_html__('Element Type', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'widget', 'options' => [ 'widget' => esc_html__('Widget', 'bdthemes-element-pack'), 'custom' => esc_html__('Widget > Custom Selector', 'bdthemes-element-pack'), ], ] ); $repeater->add_control( 'ep_notation_custom_selector', [ 'label' => esc_html__('Custom Selector', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'description' => esc_html__('Please use ID or Class to select your element/elements. ( Example - #select-id, .select-class)', 'bdthemes-element-pack'), 'condition' => [ 'ep_notation_select_type' => 'custom', ], ] ); $repeater->add_control( 'ep_notation_type', [ 'label' => esc_html__('Select Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'underline', 'options' => [ 'underline' => esc_html__('Underline', 'bdthemes-element-pack'), 'box' => esc_html__('Box', 'bdthemes-element-pack'), 'circle' => esc_html__('Circle', 'bdthemes-element-pack'), 'highlight' => esc_html__('Highlight', 'bdthemes-element-pack'), 'strike-through' => esc_html__('Strike-through', 'bdthemes-element-pack'), 'crossed-off' => esc_html__('Crossed-off', 'bdthemes-element-pack'), 'bracket' => esc_html__('Bracket', 'bdthemes-element-pack'), ], ] ); $repeater->add_control( 'ep_notation_bracket_on', [ 'label' => esc_html__('Bracket On', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'description' => esc_html__('Value could be a string. Each string being one of these values: left, right, top, bottom. When drawing a bracket, this configures which side(s) of the element to bracket. Default value is left,right', 'bdthemes-element-pack'), 'default' => 'left,right', 'condition' => [ 'ep_notation_type' => 'bracket', ], ] ); $repeater->add_control( 'ep_notation_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, ] ); $repeater->add_control( 'ep_notation_anim_duration', [ 'label' => esc_html__('Animation Duration', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 5000, 'step' => 10, ], ], 'default' => [ 'unit' => 'px', 'size' => 800, ], ] ); $repeater->add_control( 'ep_notation_stroke_width', [ 'label' => esc_html__('Stroke Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'default' => [ 'unit' => 'px', 'size' => 1, ], ] ); $repeater->add_control( 'ep_notation_waypoint_offset', [ 'label' => esc_html__('Waypoint Offset', 'bdthemes-element-pack') . BDTEP_NC, 'description' => esc_html__('Example: bottom-in-view, 90%', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'placeholder' => 'bottom-in-view', 'separator' => 'before' ] ); $widget->add_control( 'ep_notation_list', [ 'label' => esc_html__('Notation Items', 'bdthemes-element-pack'), 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(), 'prevent_empty' => false, 'title_field' => '{{{ ep_notation_select_type }}}', 'frontend_available' => true, 'default' => [ [ 'ep_notation_select_type' => 'widget', ], ], 'condition' => [ 'ep_notation_active' => 'yes', ], 'render_type' => 'template', ] ); } public function enqueue_scripts() { wp_enqueue_script('ep-notation'); } public function should_script_enqueue($widget) { if ('yes' === $widget->get_settings_for_display('ep_notation_active')) { $this->enqueue_scripts(); } } protected function add_actions() { add_action('elementor/element/common/_section_style/after_section_end', [$this, 'register_section']); add_action('elementor/element/common/section_element_pack_notation_controls/before_section_end', [$this, 'register_controls'], 10, 2); // render scripts add_action('elementor/frontend/widget/before_render', [$this, 'should_script_enqueue']); add_action('elementor/preview/enqueue_scripts', [$this, 'enqueue_scripts']); } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Notation', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\LottieImage\Widgets; use Elementor\Modules\DynamicTags\Module as TagsModule; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Border; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Text_Shadow; use Elementor\Group_Control_Css_Filter; use ElementPack\Element_Pack_Loader; if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly class Lottie_Image extends Module_Base { public function get_name() { return 'bdt-lottie-image'; } public function get_title() { return BDTEP . esc_html__( 'Lottie Image', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-lottie-image'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'lottie', 'animation', 'bodymovin', 'transition', 'image', 'svg' ]; } public function get_script_depends() { if ( $this->ep_is_edit_mode() ) { return [ 'lottie', 'ep-scripts' ]; } else { return [ 'lottie', 'ep-lottie-image' ]; } } public function get_custom_help_url() { return 'https://youtu.be/CbODBtLTxWc'; } protected function register_controls() { $this->start_controls_section( 'section_content_layout', [ 'label' => esc_html__( 'Lottie Image', 'bdthemes-element-pack' ), ] ); $this->add_control( 'lottie_json_source', [ 'label' => __( 'Select JSON Source', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'url', 'options' => [ 'url' => __( 'Load From URL', 'bdthemes-element-pack' ), 'local' => __( 'Self Hosted', 'bdthemes-element-pack' ), 'custom' => __( 'Custom JSON Code', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'lottie_json_path', [ 'label' => __( 'Lottie JSON URL', 'bdthemes-element-pack' ), 'description' => sprintf( __( 'Enter your lottie josn file, if you don\'t understand lottie json file so please %1s look here %2s', 'bdthemes-element-pack' ), '<a href="https://lottiefiles.com/featured" target="_blank">', '</a>' ), 'type' => Controls_Manager::TEXT, 'autocomplete' => false, 'show_external' => false, 'label_block' => true, 'show_label' => false, 'default' => BDTEP_ASSETS_URL . 'others/teamwork.json', 'placeholder' => __( 'Enter your json URL', 'bdthemes-element-pack' ), 'condition' => [ 'lottie_json_source' => 'url', ], 'dynamic' => [ 'active' => true, ], ] ); $this->add_control( 'upload_json_file', [ 'label' => __( 'Select JSON File', 'bdthemes-element-pack' ), 'type' => 'json-upload', 'label_block' => true, 'show_label' => true, 'condition' => [ 'lottie_json_source' => 'local', ], 'dynamic' => [ 'active' => true, ], ] ); $this->add_control( 'lottie_json_code', [ 'label' => __( 'Paste JSON Code', 'bdthemes-element-pack' ), 'description' => sprintf( __( 'Enter your lottie josn text, if you don\'t understand lottie json file so please %1s look here %2s', 'bdthemes-element-pack' ), '<a href="https://lottiefiles.com/featured" target="_blank">', '</a>' ), 'type' => Controls_Manager::TEXTAREA, 'label_block' => true, 'show_label' => true, 'dynamic' => [ 'active' => true, ], 'placeholder' => __( 'Enter your json TEXT', 'bdthemes-element-pack' ), 'condition' => [ 'lottie_json_source' => 'custom', ], ] ); $this->add_responsive_control( 'align', [ 'label' => __( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], ], 'selectors' => [ '{{WRAPPER}}' => 'text-align: {{VALUE}};', ], ] ); $this->add_control( 'caption_source', [ 'label' => __( 'Caption', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'none', 'options' => [ 'none' => __( 'None', 'bdthemes-element-pack' ), // 'title_caption' => __( 'Title', 'bdthemes-element-pack' ), 'custom_caption' => __( 'Custom', 'bdthemes-element-pack' ), ], 'frontend_available' => true, ] ); $this->add_control( 'caption', [ 'label' => __( 'Custom Caption', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'default' => '', 'placeholder' => __( 'Enter your image caption', 'bdthemes-element-pack' ), 'condition' => [ 'caption_source' => 'custom_caption' ], 'dynamic' => [ 'active' => true, ], ] ); $this->add_control( 'link_to', [ 'label' => __( 'Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'none', 'options' => [ 'none' => __( 'None', 'bdthemes-element-pack' ), 'custom' => __( 'Custom URL', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'link', [ 'label' => __( 'Link', 'bdthemes-element-pack' ), 'type' => Controls_Manager::URL, 'dynamic' => [ 'active' => true, ], 'placeholder' => __( 'https://your-link.com', 'bdthemes-element-pack' ), 'condition' => [ 'link_to' => 'custom', ], 'show_label' => false, ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_additional', [ 'label' => esc_html__( 'Additional Settings', 'bdthemes-element-pack' ), ] ); $this->add_control( 'play_action', [ 'label' => __( 'Play Action', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'autoplay', 'options' => [ '' => __( 'None', 'bdthemes-element-pack' ), 'autoplay' => __( 'Auto Play', 'bdthemes-element-pack' ), 'hover' => __( 'Play on Hover', 'bdthemes-element-pack' ), 'click' => __( 'Play on Click', 'bdthemes-element-pack' ), 'column' => __( 'Play on Hover Column', 'bdthemes-element-pack' ), 'section' => __( 'Play on Hover Section', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'view_type', [ 'label' => esc_html__( 'Start When', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'options' => [ 'pageload' => esc_html__( 'Page Loaded', 'bdthemes-element-pack' ), 'scroll' => esc_html__( 'When Scroll', 'bdthemes-element-pack' ), ], 'default' => 'pageload', 'separator' => 'before', ] ); $this->add_control( 'loop', [ 'label' => esc_html__( 'Loop', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'lottie_number_of_times', [ 'label' => __( 'Times', 'bdthemes-element-pack' ), 'type' => Controls_Manager::NUMBER, 'render_type' => 'content', 'min' => 0, 'step' => 1, 'frontend_available' => true, 'condition' => [ 'loop' => [ 'yes' ], ] ] ); $this->add_control( 'speed', [ 'label' => esc_html__( 'Play Speed', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0.1, 'max' => 5, 'step' => 0.1, ], ], 'default' => [ 'size' => '1', ], ] ); $this->add_control( 'lottie_start_point', [ 'label' => __( 'Start Point', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'frontend_available' => true, 'render_type' => 'content', 'default' => [ 'size' => '0', 'unit' => '%', ], 'size_units' => [ '%' ], ] ); $this->add_control( 'lottie_end_point', [ 'label' => __( 'End Point', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'frontend_available' => true, 'render_type' => 'content', 'default' => [ 'size' => '100', 'unit' => '%', ], 'size_units' => [ '%' ], ] ); $this->add_control( 'lottie_renderer', [ 'label' => __( 'Renderer', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'svg', 'options' => [ 'svg' => __( 'SVG', 'bdthemes-element-pack' ), 'canvas' => __( 'Canvas', 'bdthemes-element-pack' ), ], 'separator' => 'before', ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'section_style_image', [ 'label' => __( 'Lottie', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_responsive_control( 'width', [ 'label' => __( 'Width', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => '%', ], 'tablet_default' => [ 'unit' => '%', ], 'mobile_default' => [ 'unit' => '%', ], 'size_units' => [ '%', 'px', 'vw' ], 'range' => [ '%' => [ 'min' => 1, 'max' => 100, ], 'px' => [ 'min' => 1, 'max' => 1000, ], 'vw' => [ 'min' => 1, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-image svg' => 'width: {{SIZE}}{{UNIT}} !important;', ], ] ); $this->add_responsive_control( 'space', [ 'label' => __( 'Max Width', 'bdthemes-element-pack' ) . ' (%)', 'type' => Controls_Manager::SLIDER, 'default' => [ 'unit' => '%', ], 'tablet_default' => [ 'unit' => '%', ], 'mobile_default' => [ 'unit' => '%', ], 'size_units' => [ '%' ], 'range' => [ '%' => [ 'min' => 1, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-image svg' => 'max-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'separator_panel_style', [ 'type' => Controls_Manager::DIVIDER, 'style' => 'thick', ] ); $this->start_controls_tabs( 'image_effects' ); $this->start_controls_tab( 'normal', [ 'label' => __( 'Normal', 'bdthemes-element-pack' ), ] ); $this->add_control( 'opacity', [ 'label' => __( 'Opacity', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-image svg' => 'opacity: {{SIZE}};', ], ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters', 'selector' => '{{WRAPPER}} .bdt-lottie-image svg', ] ); $this->end_controls_tab(); $this->start_controls_tab( 'hover', [ 'label' => __( 'Hover', 'bdthemes-element-pack' ), ] ); $this->add_control( 'opacity_hover', [ 'label' => __( 'Opacity', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-image:hover svg' => 'opacity: {{SIZE}};', ], ] ); $this->add_group_control( Group_Control_Css_Filter::get_type(), [ 'name' => 'css_filters_hover', 'selector' => '{{WRAPPER}} .bdt-lottie-image:hover svg', ] ); $this->add_control( 'background_hover_transition', [ 'label' => __( 'Transition Duration', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 3, 'step' => 0.1, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-image svg' => 'transition-duration: {{SIZE}}s', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'image_border', 'selector' => '{{WRAPPER}} .bdt-lottie-image svg', 'separator' => 'before', ] ); $this->add_responsive_control( 'image_border_radius', [ 'label' => __( 'Border Radius', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ '{{WRAPPER}} .bdt-lottie-image svg' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'image_box_shadow', 'exclude' => [ 'box_shadow_position', ], 'selector' => '{{WRAPPER}} .bdt-lottie-image svg', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_caption', [ 'label' => __( 'Caption', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'caption_show' => 'yes', ], ] ); $this->add_control( 'caption_align', [ 'label' => __( 'Alignment', 'bdthemes-element-pack' ), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __( 'Left', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __( 'Center', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __( 'Right', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-right', ], 'justify' => [ 'title' => __( 'Justified', 'bdthemes-element-pack' ), 'icon' => 'eicon-text-align-justify', ], ], 'default' => '', 'selectors' => [ '{{WRAPPER}} .widget-image-caption' => 'text-align: {{VALUE}};', ], ] ); $this->add_control( 'text_color', [ 'label' => __( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'default' => '', 'selectors' => [ '{{WRAPPER}} .widget-image-caption' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'caption_background_color', [ 'label' => __( 'Background Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .widget-image-caption' => 'background-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'caption_padding', [ 'label' => esc_html__( 'Padding', 'bdthemes-element-pack' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .widget-image-caption' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'caption_typography', 'selector' => '{{WRAPPER}} .widget-image-caption', ] ); $this->add_group_control( Group_Control_Text_Shadow::get_type(), [ 'name' => 'caption_text_shadow', 'selector' => '{{WRAPPER}} .widget-image-caption', ] ); $this->add_responsive_control( 'caption_space', [ 'label' => __( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .widget-image-caption' => 'margin-top: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); } private function get_link_url( $settings ) { if ( 'custom' === $settings['link_to'] ) { if ( empty ( $settings['link']['url'] ) ) { return false; } return $settings['link']; } else { return false; } } private function get_caption( $settings ) { if ( 'custom_caption' === $settings['caption_source'] ) { return $settings['caption']; } else if ( 'title_caption' === $settings['caption_source'] ) { return get_the_title( $settings['upload_json_file'] ); } return ''; } protected function render() { $settings = $this->get_settings_for_display(); $json_code = ''; $json_path = ''; $is_json_url = true; if ( $settings['lottie_json_source'] == 'url' ) { $json_path = $settings['lottie_json_path']; } elseif ( $settings['lottie_json_source'] == 'local' ) { $json_path = $settings['upload_json_file']; } elseif ( $settings['lottie_json_source'] == 'custom' ) { $json_code = $settings['lottie_json_code']; $is_json_url = false; } $this->add_render_attribute( 'wrapper', 'class', 'bdt-lottie-image' ); if ( ! empty ( $settings['shape'] ) ) { $this->add_render_attribute( 'wrapper', 'class', 'elementor-image-shape-' . $settings['shape'] ); } $link = $this->get_link_url( $settings ); if ( $link ) { if ( Element_Pack_Loader::elementor()->editor->is_edit_mode() ) { $this->add_render_attribute( 'link', [ 'class' => 'elementor-clickable', ] ); } $this->add_link_attributes( 'link', $link ); } $lottie_start_point = ( ! empty ( $settings['lottie_start_point']['size'] ) ? $settings['lottie_start_point']['size'] : 0 ); $lottie_end_point = ( isset ( $settings['lottie_end_point']['size'] ) ) ? $settings['lottie_end_point']['size'] : 0; $lottie_end_point = ( strlen( $lottie_end_point ) > 0 ) ? $lottie_end_point : 100; $loopSet = ''; if ( isset ( $settings['loop'] ) ) { $loopSet = ( $settings['loop'] ) ? true : false; } if ( ! empty ( $settings['lottie_number_of_times'] ) && strlen( $settings['lottie_number_of_times'] ) > 0 ) { $loopSet = ( $settings['lottie_number_of_times'] ) - 1; } $this->add_render_attribute( [ 'lottie' => [ 'id' => 'bdt-lottie-' . $this->get_id(), 'class' => 'bdt-lottie-container', 'data-settings' => [ wp_json_encode( [ 'loop' => $loopSet, 'is_json_url' => $is_json_url, 'json_path' => $json_path, 'json_code' => $json_code, 'view_type' => $settings['view_type'], 'speed' => ( $settings['speed']['size'] ) ? $settings['speed']['size'] : 1, 'play_action' => $settings['play_action'], 'start_point' => $lottie_start_point, 'end_point' => $lottie_end_point, 'lottie_renderer' => $settings['lottie_renderer'], ] ) ] ] ] ); $caption = $this->get_caption( $settings ); ?> <div <?php $this->print_render_attribute_string( 'wrapper' ); ?>> <?php if ( 'custom_caption' === $settings['caption_source'] ) : ?> <figure class="wp-caption"> <?php endif; ?> <?php if ( $link ) : ?> <a <?php $this->print_render_attribute_string( 'link' ); ?>> <?php endif; ?> <div <?php $this->print_render_attribute_string( 'lottie' ); ?>></div> <?php if ( $link ) : ?> </a> <?php endif; ?> <?php if ( 'custom_caption' === $settings['caption_source'] ) : ?> <figcaption class="widget-image-caption wp-caption-text"> <?php echo wp_kses_post( $caption ); ?> </figcaption> </figure> <?php endif; ?> </div> <?php } } <?php namespace ElementPack\Modules\LottieImage; use ElementPack\Base\Element_Pack_Module_Base; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'lottie-image'; } public function get_widgets() { $widgets = [ 'Lottie_Image', ]; return $widgets; } } <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly return [ 'title' => esc_html__( 'Lottie Image', 'bdthemes-element-pack' ), 'required' => true, 'default_activation' => false, // 'has_style' => true, 'has_script' => true, ]; <?php namespace ElementPack\Modules\GiveTotals\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Typography; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Give_Totals extends Module_Base { public function get_name() { return 'bdt-give-totals'; } public function get_title() { return BDTEP . __('Give Totals', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-give-totals'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['give', 'charity', 'donation', 'donor', 'history', 'wall', 'form', 'goal', 'totals']; } // public function get_style_depends() { // if ($this->ep_is_edit_mode()) { // return ['ep-styles']; // } else { // return ['ep-give-totals']; // } // } public function get_custom_help_url() { return 'https://youtu.be/fZMljNFdvKs'; } protected function register_controls() { $this->start_controls_section( 'give_totals_settings', [ 'label' => __('Give Totals', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'forms', [ 'label' => __('Forms', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT2, 'options' => element_pack_give_forms_options(), 'multiple' => true, 'label_block' => true ] ); $this->add_control( 'total_goal', [ 'label' => __('Goal Amount', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'default' => '1000' ] ); $this->add_control( 'message', [ 'label' => __('Message:', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXTAREA, 'default' => __('Hey! We\'ve raised {total} of the {total_goal} we are trying to raise for this campaign!', 'bdthemes-element-pack'), ] ); $this->add_control( 'link', [ 'label' => __('Link', 'bdthemes-element-pack'), 'type' => Controls_Manager::URL, 'show_external' => false, 'default' => [ 'url' => 'https://example.org', 'is_external' => false, 'nofollow' => false, ], ] ); $this->add_control( 'link_text', [ 'label' => __('Link Text', 'bdthemes-element-pack'), 'type' => Controls_Manager::TEXT, 'default' => __('Donate Now', 'bdthemes-element-pack'), ] ); $this->add_control( 'show_progress', [ 'label' => __('Show Progress', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] ); $this->end_controls_section(); //Style $this->start_controls_section( 'section_title_style', [ 'label' => esc_html__('Raised Title', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_progress' => 'yes' ] ] ); $this->add_control( 'title_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-totals .raised' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'title_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-give-totals .raised' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography', 'selector' => '{{WRAPPER}} .bdt-give-totals .raised', ] ); $this->end_controls_section(); $this->start_controls_section( 'progress_bar_style', [ 'label' => esc_html__('Progress Bar', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_progress' => 'yes' ] ] ); $this->add_control( 'progress_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-totals .give-progress-bar>span' => 'background-color: {{VALUE}} !important;', ], ] ); $this->add_control( 'progress_bg_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-totals .give-progress-bar' => 'background-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'progress_border_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-give-totals .give-progress-bar, {{WRAPPER}} .bdt-give-totals .give-progress-bar>span' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'progress_height', [ 'label' => __('Height', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-give-totals .give-progress-bar' => 'height: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'progress_spacing', [ 'label' => __('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .bdt-give-totals .give-progress-bar' => 'margin-top: {{SIZE}}{{UNIT}} !important;', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_message_style', [ 'label' => esc_html__('Message', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'message!' => '' ] ] ); $this->add_control( 'message_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-give-totals .give-totals-shortcode-wrap' => 'color: {{VALUE}} !important;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'message_typography', 'selector' => '{{WRAPPER}} .bdt-give-totals .give-totals-shortcode-wrap', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_link_style', [ 'label' => esc_html__('Link', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'link_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} a.give-totals-text-link' => 'color: {{VALUE}} !important;', ], ] ); $this->add_control( 'link_hover_color', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} a.give-totals-text-link:hover' => 'color: {{VALUE}} !important;', ], ] ); $this->add_control( 'link_margin', [ 'label' => __('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} a.give-totals-text-link' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'link_typography', 'selector' => '{{WRAPPER}} a.give-totals-text-link', ] ); $this->end_controls_section(); } private function get_shortcode() { $settings = $this->get_settings_for_display(); if (!$settings['forms']) { return '<div class="bdt-alert bdt-alert-warning">' . __('Please select a Give Forms From Setting!', 'bdthemes-element-pack') . '</div>'; } $attributes = [ 'ids' => $settings['forms'], 'total_goal' => $settings['total_goal'], 'message' => esc_html($settings['message']), 'link' => esc_url($settings['link']['url']), 'link_text' => esc_html($settings['link_text']), 'progress_bar' => $settings['show_progress'], ]; $this->add_render_attribute('shortcode', $attributes); $shortcode = []; $shortcode[] = sprintf('[give_totals %s]', $this->get_render_attribute_string('shortcode')); return implode("", $shortcode); } public function render() { $this->add_render_attribute('give_wrapper', 'class', 'bdt-give-totals'); ?> <div <?php $this->print_render_attribute_string('give_wrapper'); ?>> <?php echo do_shortcode($this->get_shortcode()); ?> </div> <?php } public function render_plain_content() { echo wp_kses_post($this->get_shortcode()); } } <?php namespace ElementPack\Modules\GiveTotals; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'give-totals'; } public function get_widgets() { $widgets = ['Give_Totals']; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('Give Totals', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, // 'has_style' => true, ]; <?php namespace ElementPack\Modules\EddDownloadHistory\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } class EDD_Download_History extends Module_Base { public function get_name() { return 'bdt-easy-digital-download-history'; } public function get_title() { return BDTEP . esc_html__('EDD History', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-edd-download-history'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['easy', 'digital', 'history', 'software', 'eshop', 'estore']; } public function get_custom_help_url() { return 'https://youtu.be/taM7whXxmNY'; } protected function register_controls() { $this->start_controls_section( 'section_content_table', [ 'label' => __('Table', 'bdthemes-element-pack'), ] ); $this->add_control( 'header_align', [ 'label' => __('Header Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-right', ], ], 'default' => 'center', 'selectors' => [ '{{WRAPPER}} #edd_user_history th' => 'text-align: {{VALUE}};', ], ] ); $this->add_control( 'body_align', [ 'label' => __('Body Alignment', 'bdthemes-element-pack'), 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => [ 'title' => __('Left', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-left', ], 'center' => [ 'title' => __('Center', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-center', ], 'right' => [ 'title' => __('Right', 'bdthemes-element-pack'), 'icon' => 'eicon-text-align-right', ], ], 'default' => 'center', 'selectors' => [ '{{WRAPPER}} #edd_user_history td' => 'text-align: {{VALUE}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_table', [ 'label' => __('Table', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'table_border_style', [ 'label' => __('Border Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'none' => __('None', 'bdthemes-element-pack'), 'solid' => __('Solid', 'bdthemes-element-pack'), 'double' => __('Double', 'bdthemes-element-pack'), 'dotted' => __('Dotted', 'bdthemes-element-pack'), 'dashed' => __('Dashed', 'bdthemes-element-pack'), 'groove' => __('Groove', 'bdthemes-element-pack'), ], 'selectors' => [ '{{WRAPPER}} #edd_user_history' => 'border-style: {{VALUE}};', ], ] ); $this->add_control( 'table_border_width', [ 'label' => __('Border Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'min' => 0, 'max' => 20, 'size' => 1, ], 'selectors' => [ '{{WRAPPER}} #edd_user_history' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'table_border_color', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#ccc', 'selectors' => [ '{{WRAPPER}} #edd_user_history' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_header', [ 'label' => __('Header', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'header_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#dfe3e6', 'selectors' => [ '{{WRAPPER}} #edd_user_history th' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'header_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#333', 'selectors' => [ '{{WRAPPER}} #edd_user_history th' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'header_border_style', [ 'label' => __('Border Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'none' => __('None', 'bdthemes-element-pack'), 'solid' => __('Solid', 'bdthemes-element-pack'), 'double' => __('Double', 'bdthemes-element-pack'), 'dotted' => __('Dotted', 'bdthemes-element-pack'), 'dashed' => __('Dashed', 'bdthemes-element-pack'), 'groove' => __('Groove', 'bdthemes-element-pack'), ], 'selectors' => [ '{{WRAPPER}} #edd_user_history th' => 'border-style: {{VALUE}};', ], ] ); $this->add_control( 'header_border_width', [ 'label' => __('Border Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'min' => 0, 'max' => 20, 'size' => 1, ], 'selectors' => [ '{{WRAPPER}} #edd_user_history th' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'header_border_color', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#ccc', 'selectors' => [ '{{WRAPPER}} #edd_user_history th' => 'border-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'header_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'default' => [ 'top' => 1, 'bottom' => 1, 'left' => 1, 'right' => 1, 'unit' => 'em' ], 'selectors' => [ '{{WRAPPER}} #edd_user_history th' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_body', [ 'label' => __('Body', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'cell_border_style', [ 'label' => __('Border Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => [ 'none' => __('None', 'bdthemes-element-pack'), 'solid' => __('Solid', 'bdthemes-element-pack'), 'double' => __('Double', 'bdthemes-element-pack'), 'dotted' => __('Dotted', 'bdthemes-element-pack'), 'dashed' => __('Dashed', 'bdthemes-element-pack'), 'groove' => __('Groove', 'bdthemes-element-pack'), ], 'selectors' => [ '{{WRAPPER}} #edd_user_history td' => 'border-style: {{VALUE}};', ], ] ); $this->add_control( 'cell_border_width', [ 'label' => __('Border Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'min' => 0, 'max' => 20, 'size' => 1, ], 'selectors' => [ '{{WRAPPER}} #edd_user_history td' => 'border-width: {{SIZE}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'cell_padding', [ 'label' => __('Cell Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'default' => [ 'top' => 0.5, 'bottom' => 0.5, 'left' => 1, 'right' => 1, 'unit' => 'em' ], 'selectors' => [ '{{WRAPPER}} #edd_user_history td' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'separator' => 'after', ] ); $this->start_controls_tabs('tabs_body_style'); $this->start_controls_tab( 'tab_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'normal_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#fff', 'selectors' => [ '{{WRAPPER}} #edd_user_history tr:nth-child(odd) td' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'normal_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_user_history tr:nth-child(odd) td' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'normal_border_color', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#ccc', 'selectors' => [ '{{WRAPPER}} #edd_user_history tr:nth-child(odd) td' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_stripe', [ 'label' => __('Stripe', 'bdthemes-element-pack'), ] ); $this->add_control( 'stripe_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#f7f7f7', 'selectors' => [ '{{WRAPPER}} #edd_user_history tr:nth-child(even) td' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'stripe_color', [ 'label' => __('Text Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #edd_user_history tr:nth-child(even) td' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'stripe_border_color', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'default' => '#ccc', 'selectors' => [ '{{WRAPPER}} #edd_user_history tr:nth-child(even) td' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); } protected function render() { echo do_shortcode('[download_history]'); } } <?php namespace ElementPack\Modules\EddDownloadHistory; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'easy-digital-download-history'; } public function get_widgets() { $widgets = [ 'EDD_Download_History', ]; return $widgets; } } <?php if (!defined('ABSPATH')) exit; // Exit if accessed directly return [ 'title' => esc_html__('EDD Download History', 'bdthemes-element-pack'), 'required' => true, 'default_activation' => true, ]; <?php namespace ElementPack\Modules\EventsCalendarGrid\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Image_Size; use Elementor\Group_Control_Typography; use Elementor\Group_Control_Box_Shadow; use Elementor\Group_Control_Border; use Elementor\Utils; use ElementPack\Modules\EventsCalendarGrid\Skins; if (!defined('ABSPATH')) exit; // Exit if accessed directly /** * Class Post Slider */ class Events_Calendar_Grid extends Module_Base { public $_query = null; public function get_name() { return 'bdt-event-grid'; } public function get_title() { return BDTEP . esc_html__('Events Calendar Grid', 'bdthemes-element-pack'); } public function get_icon() { return 'bdt-wi-events-calendar-grid'; } public function get_categories() { return ['element-pack']; } public function get_keywords() { return ['events', 'gallery', 'calendar', 'grid', 'the']; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-styles']; } else { return ['ep-events-calendar-grid', 'ep-font']; } } public function register_skins() { $this->add_skin(new Skins\Skin_Annal($this)); $this->add_skin(new Skins\Skin_Acara($this)); } public function get_custom_help_url() { return 'https://youtu.be/QeqrcDx1Vus'; } public function on_import($element) { if (!get_post_type_object($element['settings']['posts_post_type'])) { $element['settings']['posts_post_type'] = 'post'; } return $element; } // public function on_export($element) { // $element = Group_Control_Posts::on_export_remove_setting_from_element($element, 'posts'); // return $element; // } public function get_query() { return $this->_query; } public function register_controls() { // Layout Section $this->start_controls_section( 'section_content_layout', [ 'label' => __('Layout', 'bdthemes-element-pack'), ] ); $this->add_responsive_control( 'columns', [ 'label' => esc_html__('Columns', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => '3', 'tablet_default' => '2', 'mobile_default' => '1', 'options' => [ '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', ], ] ); $this->add_control( 'column_gap', [ 'label' => esc_html__('Column Gap', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'medium', 'options' => [ 'small' => esc_html__('Small', 'bdthemes-element-pack'), 'medium' => esc_html__('Medium', 'bdthemes-element-pack'), 'large' => esc_html__('Large', 'bdthemes-element-pack'), 'collapse' => esc_html__('Collapse', 'bdthemes-element-pack'), ], ] ); $this->add_responsive_control( 'row_gap', [ 'label' => esc_html__('Row Gap', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item, .bdt-event-calendar .bdt-event-item-inner' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'show_image', [ 'label' => __('Show Image', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_title', [ 'label' => __('Show Title', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_date', [ 'label' => __('Show Date', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_excerpt', [ 'label' => __('Show Excerpt', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'excerpt_length', [ 'label' => __('Excerpt Length', 'bdthemes-element-pack'), 'type' => Controls_Manager::NUMBER, 'default' => 15, 'condition' => [ 'show_excerpt' => 'yes' ] ] ); $this->add_control( 'show_meta', [ 'label' => __('Show Meta', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ '_skin!' => 'acara', ], ] ); $this->add_control( 'show_meta_cost', [ 'label' => __('Show Cost', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_meta_website', [ 'label' => __('Show Website', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_meta_location', [ 'label' => __('Show Location', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_meta_more_btn', [ 'label' => __('Show More Button', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ '_skin' => 'annal', ], ] ); $this->add_control( 'anchor_link', [ 'label' => __('Anchor Link', 'bdthemes-element-pack'), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'match_height', [ 'label' => __('Item Match Height', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_image', [ 'label' => __('Image', 'bdthemes-element-pack'), ] ); $this->add_group_control( Group_Control_Image_Size::get_type(), [ 'name' => 'image', 'label' => esc_html__('Image Size', 'bdthemes-element-pack'), 'exclude' => ['custom'], 'default' => 'medium', ] ); $this->add_responsive_control( 'image_width', [ 'label' => __('Image Width', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 100, 'unit' => '%', ], 'tablet_default' => [ 'unit' => '%', ], 'mobile_default' => [ 'unit' => '%', ], 'size_units' => ['%'], 'range' => [ '%' => [ 'min' => 5, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-image' => 'width: {{SIZE}}{{UNIT}};margin-left: auto;margin-right: auto;', ], 'condition' => [ 'show_image' => 'yes', '_skin!' => 'acara', ], ] ); $this->add_responsive_control( 'image_ratio', [ 'label' => __('Image Ratio', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0.1, 'max' => 2, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-image' => 'padding-bottom: calc( {{SIZE}} * 100% ); top: 0; left: 0; right: 0; bottom: 0;', '{{WRAPPER}} .bdt-event-calendar .bdt-event-image:after' => 'content: "{{SIZE}}"; position: absolute; color: transparent;', '{{WRAPPER}} .bdt-event-calendar .bdt-event-image img' => 'height: 100%; width: auto; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); font-size: {{SIZE}}; object-fit: cover;', ], 'condition' => [ 'show_image' => 'yes', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_content_query', [ 'label' => __('Query', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'source', [ 'label' => _x('Source', 'Posts Query Control', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => [ '' => esc_html__('Show All', 'bdthemes-element-pack'), 'upcoming_events' => esc_html__('Upcoming Events', 'bdthemes-element-pack'), 'by_name' => esc_html__('Manual Selection', 'bdthemes-element-pack'), ], 'label_block' => true, ] ); $this->add_control( 'event_categories', [ 'label' => esc_html__('Categories', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT2, 'options' => element_pack_get_terms('tribe_events_cat'), 'default' => [], 'label_block' => true, 'multiple' => true, 'condition' => [ 'source' => 'by_name', ], ] ); $this->add_control( 'start_date', [ 'label' => esc_html__('Start Date', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => [ '' => esc_html__('Any Time', 'bdthemes-element-pack'), 'now' => esc_html__('Now', 'bdthemes-element-pack'), 'today' => esc_html__('Today', 'bdthemes-element-pack'), 'last month' => esc_html__('Last Month', 'bdthemes-element-pack'), 'custom' => esc_html__('Custom', 'bdthemes-element-pack'), ], 'label_block' => true, ] ); $this->add_control( 'custom_start_date', [ 'label' => esc_html__('Custom Start Date', 'bdthemes-element-pack'), 'type' => Controls_Manager::DATE_TIME, 'condition' => [ 'start_date' => 'custom' ] ] ); $this->add_control( 'end_date', [ 'label' => esc_html__('End Date', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'options' => [ '' => esc_html__('Any Time', 'bdthemes-element-pack'), 'now' => esc_html__('Now', 'bdthemes-element-pack'), 'today' => esc_html__('Today', 'bdthemes-element-pack'), 'next month' => esc_html__('Last Month', 'bdthemes-element-pack'), 'custom' => esc_html__('Custom', 'bdthemes-element-pack'), ], 'label_block' => true, ] ); $this->add_control( 'custom_end_date', [ 'label' => esc_html__('Custom End Date', 'bdthemes-element-pack'), 'type' => Controls_Manager::DATE_TIME, 'condition' => [ 'end_date' => 'custom' ] ] ); $this->add_control( 'limit', [ 'label' => esc_html__('Limit', 'bdthemes-element-pack'), 'type' => Controls_Manager::NUMBER, 'default' => 6, ] ); $this->add_control( 'orderby', [ 'label' => esc_html__('Order by', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'event_date', 'options' => [ 'event_date' => esc_html__('Event Date', 'bdthemes-element-pack'), 'title' => esc_html__('Title', 'bdthemes-element-pack'), 'category' => esc_html__('Category', 'bdthemes-element-pack'), 'rand' => esc_html__('Random', 'bdthemes-element-pack'), ], ] ); $this->add_control( 'order', [ 'label' => esc_html__('Order', 'bdthemes-element-pack'), 'type' => Controls_Manager::SELECT, 'default' => 'DESC', 'options' => [ 'DESC' => esc_html__('Descending', 'bdthemes-element-pack'), 'ASC' => esc_html__('Ascending', 'bdthemes-element-pack'), ], ] ); $this->end_controls_section(); // Style Section $this->start_controls_section( 'section_style_item', [ 'label' => __('Items', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'item_content_background', [ 'label' => __('Content Background', 'bdthemes-element-pack') . BDTEP_NC, 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .skin-annal .bdt-event-content' => 'background-color: {{VALUE}};', ], 'condition' => [ '_skin' => ['annal'], ], ] ); $this->add_responsive_control( 'content_padding', [ 'label' => __('Content Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}', ], 'condition' => [ '_skin!' => ['annal'], ], ] ); $this->add_control( 'item_hover_before_style_background', [ 'label' => __('Hover Style', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner:before' => 'background-color: {{VALUE}};', ], 'condition' => [ '_skin!' => ['annal', 'acara'], ], ] ); $this->add_responsive_control( 'item_hover_before_style_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner:before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], 'condition' => [ '_skin!' => ['annal', 'acara'], ], ] ); $this->start_controls_tabs('tabs_item_style'); $this->start_controls_tab( 'tab_item_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'item_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner' => 'background-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_shadow', 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner', ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'item_border', 'label' => __('Border', 'bdthemes-element-pack'), 'placeholder' => '1px', 'default' => '1px', 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner', ] ); $this->add_responsive_control( 'item_border_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_item_hover', [ 'label' => __('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'item_hover_background', [ 'label' => __('Background', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner:hover' => 'background-color: {{VALUE}};', ], ] ); $this->add_control( 'item_hover_border_color', [ 'label' => __('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'condition' => [ 'item_border_border!' => '', ], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner:hover' => 'border-color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'item_hover_shadow', 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner:hover', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_image', [ 'label' => esc_html__('Image', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_image' => ['yes'], ], ] ); $this->add_responsive_control( 'image_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-image' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}', ], ] ); $this->add_responsive_control( 'image_margin', [ 'label' => __('Margin', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-image' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}', ], ] ); $this->add_control( 'image_border_radius', [ 'label' => __('Image Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_control( 'image_opacity', [ 'label' => __('Opacity (%)', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-image img' => 'opacity: {{SIZE}};', ], ] ); $this->add_control( 'image_hover_opacity', [ 'label' => __('Hover Opacity (%)', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 1, ], 'range' => [ 'px' => [ 'max' => 1, 'min' => 0.10, 'step' => 0.01, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner:hover .bdt-event-image img' => 'opacity: {{SIZE}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_title', [ 'label' => esc_html__('Title', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_title' => ['yes'], ], ] ); $this->add_control( 'title_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-title' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'title_hover_color', [ 'label' => esc_html__('Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-title:hover' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'title_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-event-title-wrap', ] ); $this->add_control( 'title_separator_color', [ 'label' => esc_html__('Separator Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-intro .bdt-event-title-wrap, {{WRAPPER}} .bdt-event-calendar .bdt-event-intro' => 'border-color: {{VALUE}};', ], 'condition' => [ '_skin!' => 'annal', ], ] ); $this->add_responsive_control( 'title_spacing', [ 'label' => esc_html__('Spacing', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-intro, {{WRAPPER}} .skin-annal .bdt-event-title-wrap' => 'margin-bottom: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_date', [ 'label' => esc_html__('Date', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_date' => ['yes'], ], ] ); $this->add_control( 'day_color', [ 'label' => esc_html__('Day Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-date a .bdt-event-day' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'day_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-event-date a .bdt-event-day', ] ); $this->add_control( 'date_color', [ 'label' => esc_html__('Month Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-date a' => 'color: {{VALUE}};', ], 'condition' => [ '_skin!' => ['annal'], ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'date_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-event-date', 'condition' => [ '_skin!' => ['annal'], ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_excerpt', [ 'label' => esc_html__('Excerpt', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_excerpt' => ['yes'], ], ] ); $this->add_control( 'excerpt_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-excerpt' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'excerpt_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-event-excerpt', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_meta', [ 'label' => esc_html__('Meta', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_meta' => ['yes'], '_skin!' => 'acara', ], ] ); $this->add_control( 'meta_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-meta .bdt-event-price a' => 'color: {{VALUE}};', ], 'condition' => [ 'show_meta_cost' => ['yes'], ], ] ); $this->add_control( 'meta_icon_color', [ 'label' => esc_html__('Icon Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-meta .bdt-address-website-icon a, {{WRAPPER}} .skin-annal .bdt-event-meta .bdt-more-icon a' => 'color: {{VALUE}};', ], 'condition' => [ 'show_meta_more_btn' => ['yes'], ], ] ); $this->add_control( 'meta_icon_border_color', [ 'label' => esc_html__('Icon Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .skin-annal .bdt-event-meta .bdt-more-icon a' => 'border-color: {{VALUE}};', ], 'condition' => [ '_skin!' => [''], ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'meta_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-event-meta a', ] ); $this->add_responsive_control( 'meta_padding', [ 'label' => __('Meta Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-meta' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}', ], ] ); $this->add_control( 'meta_border_top_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-meta' => 'border-color: {{VALUE}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_meta_price', [ 'label' => esc_html__('Meta Price', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_meta_cost' => ['yes'], '_skin' => 'acara', ], ] ); $this->start_controls_tabs('tabs_meta_price_style'); $this->start_controls_tab( 'tab_meta_price_normal', [ 'label' => __('Normal', 'bdthemes-element-pack'), ] ); $this->add_control( 'meta_price_icon_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-price a svg *' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'meta_price_icon_background_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-price a' => 'background: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'meta_price_border', 'label' => __('Border', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-event-price a', ] ); $this->add_control( 'meta_price_border_radius', [ 'label' => __('Border Radius', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%'], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-price a' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', ], ] ); $this->add_responsive_control( 'meta_price_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-price a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}', ], ] ); $this->add_responsive_control( 'meta_price_icon_size', [ 'label' => __('Icon Size', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ] ], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-price a svg' => 'width: {{SIZE}}{{UNIT}};' ], ] ); $this->end_controls_tab(); $this->start_controls_tab( 'tab_meta_price_hover', [ 'label' => __('Hover', 'bdthemes-element-pack'), ] ); $this->add_control( 'meta_price_hover_color', [ 'label' => esc_html__('Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-price a .bdt-price-amount' => 'color: {{VALUE}};', '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner:hover .bdt-event-price a svg *' => 'fill: {{VALUE}};', ], ] ); $this->add_control( 'meta_price_hover_background_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner:hover .bdt-event-price a' => 'background: {{VALUE}};', ], ] ); $this->add_control( 'price_border_hover_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner:hover .bdt-event-price a' => 'border-color: {{VALUE}};', ], ] ); $this->add_responsive_control( 'meta_price_padding_right', [ 'label' => __('Match Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 100, ] ], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-event-item-inner:hover .bdt-event-price a' => 'padding-right: {{SIZE}}{{UNIT}};' ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'meta_price_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-event-price a .bdt-price-amount', ] ); $this->end_controls_tab(); $this->end_controls_tabs(); $this->end_controls_section(); $this->start_controls_section( 'section_style_address_website', [ 'label' => esc_html__('Address', 'bdthemes-element-pack'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ '_skin' => ['annal', 'acara'], ], ] ); $this->add_control( 'address_website_icon_color', [ 'label' => esc_html__('Icon Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-address-website-icon a' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'address_website_icon_hover_color', [ 'label' => esc_html__('Icon Hover Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-address-website-icon a:hover' => 'color: {{VALUE}};', ], 'condition' => [ '_skin' => ['acara'], ], ] ); $this->add_control( 'address_website_icon_background_color', [ 'label' => esc_html__('Background Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-address-website-icon a' => 'background-color: {{VALUE}};', ], 'condition' => [ '_skin!' => ['acara'], ], ] ); $this->add_control( 'address_website_icon_border_color', [ 'label' => esc_html__('Border Color', 'bdthemes-element-pack'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-address-website-icon a' => 'border-color: {{VALUE}};', ], 'condition' => [ '_skin!' => ['acara'], ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'address_website_typography', 'label' => esc_html__('Typography', 'bdthemes-element-pack'), 'selector' => '{{WRAPPER}} .bdt-event-calendar .bdt-address-website-icon a', ] ); $this->add_responsive_control( 'address_website_padding', [ 'label' => __('Padding', 'bdthemes-element-pack'), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', '%', 'em'], 'selectors' => [ '{{WRAPPER}} .bdt-event-calendar .bdt-address-website-icon a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}', ], 'condition' => [ '_skin!' => ['acara'], ], ] ); $this->end_controls_section(); } public function render() { $settings = $this->get_settings_for_display(); global $post; $start_date = ('custom' == $settings['start_date']) ? $settings['custom_start_date'] : $settings['start_date']; $end_date = ('custom' == $settings['end_date']) ? $settings['custom_end_date'] : $settings['end_date']; $query_args = array_filter([ 'start_date' => $start_date, 'end_date' => $end_date, 'orderby' => $settings['orderby'], 'order' => $settings['order'], 'eventDisplay' => ('custom' == $settings['start_date'] or 'custom' == $settings['end_date']) ? 'custom' : 'all', 'posts_per_page' => $settings['limit'], //'tag' => 'donor-program', // or whatever the tag name is ]); if('upcoming_events' === $settings['source']){ /** @var Tribe__Context $context */ $context = tribe('context'); $display = $context->get('event_display'); $date_pivot_key = 'past' === $display ? 'starts_before' : 'starts_after'; $query_args[$date_pivot_key] = 'now'; } if ('by_name' === $settings['source'] and !empty($settings['event_categories'])) { // $query_args['tax_query'] = [ // 'taxonomy' => 'tribe_events_cat', // 'field' => 'slug', // 'terms' => $settings['event_categories'] // ]; $query_args['event_category'] = $settings['event_categories']; } $query_args = tribe_get_events($query_args); $this->render_header(); if(!empty($query_args)){ foreach ($query_args as $post) { $this->render_loop_item($post); } } else { echo '<div class="bdt-alert bdt-alert-warning">'.esc_html__('No events!', 'bdthemes-element-pack').'</div>'; } $this->render_footer(); wp_reset_postdata(); } public function render_image() { $settings = $this->get_settings_for_display(); if (!$this->get_settings('show_image')) { return; } $settings['image'] = [ 'id' => get_post_thumbnail_id(), ]; $image_html = Group_Control_Image_Size::get_attachment_image_html($settings, 'image'); $placeholder_image_src = Utils::get_placeholder_image_src(); if (!$image_html) { $image_html = '<img src="' . esc_url($placeholder_image_src) . '" alt="' . get_the_title() . '">'; } ?> <div class="bdt-event-image bdt-background-cover"> <a href="<?php echo ($settings['anchor_link'] == 'yes') ? esc_url(the_permalink()) : 'javascript:void(0);'; ?>" title="<?php echo esc_html(get_the_title()); ?>"> <img src="<?php echo esc_url(wp_get_attachment_image_url(get_post_thumbnail_id(), $settings['image_size'])); ?>" alt="<?php echo esc_html(get_the_title()); ?>"> </a> </div> <?php } public function render_title() { $settings = $this->get_settings_for_display(); if (!$this->get_settings('show_title')) { return; } ?> <h3 class="bdt-event-title-wrap"> <a href="<?php echo ($settings['anchor_link'] == 'yes') ? esc_url( get_permalink()) : 'javascript:void(0);'; ?>" class="bdt-event-title"> <?php the_title() ?> </a> </h3> <?php } public function render_date() { if (!$this->get_settings('show_date')) { return; } $start_datetime = tribe_get_start_date(); $end_datetime = tribe_get_end_date(); $event_day = tribe_get_start_date(null, false, 'j'); $event_month = tribe_get_start_date(null, false, 'M'); ?> <span class="bdt-event-date"> <a href="javascript:void(0);" title="<?php esc_html_e('Start Date:', 'bdthemes-element-pack'); echo esc_html($start_datetime); ?> - <?php esc_html_e('End Date:', 'bdthemes-element-pack'); echo esc_html($end_datetime); ?>"> <span class="bdt-event-day"> <?php echo esc_html(str_pad($event_day, 2, '0', STR_PAD_LEFT)); ?> </span> <span> <?php echo esc_html($event_month); ?> </span> </a> </span> <?php } public function render_excerpt($post) { if (!$this->get_settings('show_excerpt')) { return; } ?> <div class="bdt-event-excerpt"> <?php if (!$post->post_excerpt) { echo wp_kses_post(strip_shortcodes(wp_trim_words($post->post_content, $this->get_settings('excerpt_length')))); } else { echo wp_kses_post(strip_shortcodes(wp_trim_words($post->post_excerpt, $this->get_settings('excerpt_length')))); } ?> </div> <?php } public function render_meta() { $settings = $this->get_settings_for_display(); if (!$this->get_settings('show_meta')) { return; } $cost = ($settings['show_meta_cost']) ? tribe_get_formatted_cost() : ''; $address = ($settings['show_meta_location']) ? tribe_address_exists() : ''; $website = ($settings['show_meta_website']) ? tribe_get_event_website_url() : ''; ?> <?php if (!empty($cost) or $address or !empty($website)) : ?> <div class="bdt-event-meta bdt-grid"> <?php if (!empty($cost)) : ?> <div class="bdt-width-auto bdt-padding-remove"> <div class="bdt-event-price"> <a href="javascript:void(0);"><?php esc_html_e('Cost:', 'bdthemes-element-pack'); ?></a> <a href="javascript:void(0);"><?php echo esc_html($cost); ?></a> </div> </div> <?php endif; ?> <?php if (!empty($website) or $address) : ?> <div class="bdt-width-expand bdt-text-right"> <div class="bdt-address-website-icon"> <?php if (!empty($website)) : ?> <a href="<?php echo esc_url($website); ?>" target="_blank" class="ep-icon-earth" aria-hidden="true"></a> <?php endif; ?> <?php if ($address) : ?> <a href="javascript:void(0);" bdt-tooltip="<?php echo esc_html(tribe_get_full_address()); ?>" class="ep-icon-location" aria-hidden="true"></a> <?php endif; ?> </div> </div> <?php endif; ?> </div> <?php endif; ?> <?php } public function render_header($skin_name = 'default') { $settings = $this->get_settings_for_display(); $id = $this->get_id(); $desktop_cols = isset($settings['columns']) ? $settings['columns'] : 3; $tablet_cols = isset($settings['columns_tablet']) ? $settings['columns_tablet'] : 2; $mobile_cols = isset($settings['columns_mobile']) ? $settings['columns_mobile'] : 1; $this->add_render_attribute('event-grid', 'id', $id); $this->add_render_attribute('event-grid', 'class', ['bdt-event-grid', 'bdt-event-calendar', 'skin-' . $skin_name]); if ('yes' == $settings['match_height']) { $this->add_render_attribute('event-grid', 'bdt-height-match', 'target: > div > div > div > .bdt-event-content'); } ?> <div <?php $this->print_render_attribute_string('event-grid'); ?>> <div class="bdt-grid bdt-grid-<?php echo esc_attr($settings['column_gap']); ?> bdt-child-width-1-<?php echo esc_attr($mobile_cols); ?> bdt-child-width-1-<?php echo esc_attr($tablet_cols); ?>@s bdt-child-width-1-<?php echo esc_attr($desktop_cols); ?>@l" bdt-grid> <?php } public function render_footer() { $settings = $this->get_settings_for_display(); ?> </div> </div> <?php } public function render_loop_item($post) { $settings = $this->get_settings_for_display(); ?> <div class="bdt-event-grid-item"> <div class="bdt-event-item-inner"> <?php $this->render_image(); ?> <div class="bdt-event-content"> <div class="bdt-event-intro"> <?php $this->render_date(); ?> <?php $this->render_title(); ?> </div> <?php $this->render_excerpt($post); ?> </div> <?php $this->render_meta(); ?> </div> </div> <?php } } <?php namespace ElementPack\Modules\EventsCalendarGrid; use ElementPack\Base\Element_Pack_Module_Base; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function get_name() { return 'event-grid'; } public function get_widgets() { $widgets = ['Events_Calendar_Grid']; return $widgets; } } <?php namespace ElementPack\Modules\EventsCalendarGrid\Skins; use Elementor\Skin_Base as Elementor_Skin_Base; use Elementor\Group_Control_Image_Size; use Elementor\Utils; if (!defined('ABSPATH')) exit; // Exit if accessed directly class Skin_Acara extends Elementor_Skin_Base { public function get_id() { return 'acara'; } public function get_title() { return __('Acara', 'bdthemes-element-pack'); } public function render() { $settings = $this->parent->get_settings_for_display(); global $post; $start_date = ('custom' == $settings['start_date']) ? $settings['custom_start_date'] : $settings['start_date']; $end_date = ('custom' == $settings['end_date']) ? $settings['custom_end_date'] : $settings['end_date']; $query_args = array_filter([ 'start_date' => $start_date, 'end_date' => $end_date, 'orderby' => $settings['orderby'], 'order' => $settings['order'], 'eventDisplay' => ('custom' == $settings['start_date'] or 'custom' == $settings['end_date']) ? 'custom' : 'all', 'posts_per_page' => $settings['limit'], //'tag' => 'donor-program', // or whatever the tag name is ]); if ('by_name' === $settings['source'] and !empty($settings['event_categories'])) { // $query_args['tax_query'] = [ // 'taxonomy' => 'tribe_events_cat', // 'field' => 'slug', // 'terms' => $settings['event_categories'] // ]; $query_args['event_category'] = $settings['event_categories']; } $query_args = tribe_get_events($query_args); $skin_name = 'acara'; $this->parent->render_header($skin_name); foreach ($query_args as $post) { $this->render_loop_item($post); } $this->parent->render_footer(); wp_reset_postdata(); } public function render_date() { if (!$this->parent->get_settings('show_date')) { return; } $start_datetime = tribe_get_start_date(); $end_datetime = tribe_get_end_date(); $event_day = tribe_get_start_date(null, false, 'j'); $event_month = tribe_get_start_date(null, false, 'M'); ?> <span class="bdt-event-date"> <a href="javascript:void(0);" title="<?php esc_html_e('Start Date:', 'bdthemes-element-pack'); echo esc_html($start_datetime); ?> - <?php esc_html_e('End Date:', 'bdthemes-element-pack'); echo esc_html($end_datetime); ?>"> <span class="bdt-event-day"> <?php echo esc_html(str_pad($event_day, 2, '0', STR_PAD_LEFT)); ?> </span> <span> <?php echo esc_html($event_month); ?> </span> </a> </span> <?php } public function render_price() { $settings = $this->parent->get_settings_for_display(); $cost = ($settings['show_meta_cost']) ? tribe_get_formatted_cost() : ''; ?> <?php if (!empty($cost)) : ?> <div class="bdt-event-price"> <a class="bdt-flex bdt-flex-middle" href="javascript:void(0);"> <span> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" width="20px" height="20px" viewBox="0 0 481.08 481.08" style="" xml:space="preserve"> <g> <g> <path d="M470.52,159.601l-35.977-35.688c-10.657,10.656-23.604,15.988-38.828,15.988c-15.229,0-28.168-5.332-38.824-15.988 c-10.664-10.66-15.988-23.601-15.988-38.83c0-15.23,5.331-28.171,15.988-38.832l-35.693-35.688 c-7.05-7.04-15.66-10.562-25.838-10.562c-10.184,0-18.794,3.523-25.837,10.562L10.566,269.233C3.521,276.279,0,284.896,0,295.07 c0,10.182,3.521,18.791,10.566,25.838l35.688,35.977c10.66-10.656,23.604-15.988,38.831-15.988 c15.226,0,28.167,5.325,38.826,15.988c10.657,10.657,15.987,23.6,15.987,38.828s-5.327,28.164-15.987,38.828l35.976,35.974 c7.044,7.043,15.658,10.564,25.841,10.564c10.184,0,18.798-3.521,25.837-10.564L470.52,211.275 c7.043-7.042,10.561-15.653,10.561-25.837C481.08,175.255,477.562,166.645,470.52,159.601z M393.145,216.701L216.702,393.139 c-3.422,3.433-7.705,5.144-12.847,5.144c-5.137,0-9.419-1.711-12.845-5.144L87.653,289.793c-3.617-3.621-5.424-7.902-5.424-12.847 c0-4.949,1.807-9.236,5.424-12.854L264.095,87.651c3.429-3.427,7.714-5.142,12.854-5.142c5.134,0,9.418,1.715,12.847,5.142 l103.35,103.353c3.621,3.619,5.428,7.902,5.428,12.85C398.572,208.801,396.766,213.083,393.145,216.701z" /> <path d="M276.955,113.639l90.223,90.218L203.87,367.165l-90.218-90.218L276.955,113.639z" /> </g> </g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> <g></g> </svg> </span> <span class="bdt-price-amount"><?php echo esc_html($cost); ?></span> </a> </div> <?php endif; ?> <?php } public function render_website_address() { $settings = $this->parent->get_settings_for_display(); $address = ($settings['show_meta_location']) ? tribe_address_exists() : ''; $website = ($settings['show_meta_website']) ? tribe_get_event_website_url() : ''; ?> <?php if (!empty($website) or $address) : ?> <div class="bdt-address-website-icon"> <?php if (!empty($website)) : ?> <a href="<?php echo esc_url($website); ?>" target="_blank" class="ep-icon-earth" aria-hidden="true"></a> <?php endif; ?> <?php if ($address) : ?> <a href="javascript:void(0);" bdt-tooltip="<?php echo esc_html(tribe_get_full_address()); ?>" class="ep-icon-location" aria-hidden="true"></a> <?php endif; ?> </div> <?php endif; ?> <?php } public function render_loop_item($post) { $settings = $this->parent->get_settings_for_display(); ?> <div class="bdt-event-item"> <div class="bdt-event-item-inner"> <div class="bdt-event-image-wrap"> <?php $this->parent->render_image(); ?> <?php $this->render_date(); ?> <?php $this->render_price(); ?> </div> <div class="bdt-event-content"> <div class="bdt-event-intro bdt-flex bdt-flex-between bdt-flex-middle"> <?php $this->parent->render_title(); ?> <?php $this->render_website_address(); ?> </div> <?php $this->parent->render_excerpt($post); ?> </div> </div> </div> <?php } }