/** * @param RunkitFunction $function * * @return boolean */ public function redefineFunction(\Runkit\RunkitFunction $function) { if (!function_exists('runkit_function_redefine')) { return false; } return runkit_function_redefine($function->getName(), (string) $function->getArguments(), $function->getCode()->get()); }
function logFunc($name) { // copy function to fuinction_o runkit_function_copy($name, $name . "_o"); // runkit_function_redefine($name,"","logger_simple(); if(!empty(getargs())) return {$name}_o(getargs()); else return {$name}_o();"); // redefine function and internally call the original function (function_o) runkit_function_redefine($name, "", "return logger();"); }
function _wpe_replace_function($name, $args) { if (function_exists($name)) { // is possible that it doesn't! Then don't emit errors. $call_args = preg_replace("#=[^,]+#", "", $args); runkit_function_copy($name, "_wpe_old_{$name}"); runkit_function_redefine($name, $args, "return _wpe_new_{$name}({$call_args});"); } }
public static function timeInit() { if (!isset(self::$_timeStart)) { runkit_function_copy('time', 'time_original'); runkit_function_redefine('time', '', 'return CMTest_TH::time();'); } self::$_timeStart = time_original(); self::$timeDelta = 0; }
/** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock */ protected function runkitOverride($func, $args, $body, $mock = null) { $this->skipTestIfNoRunkit(); if (array_key_exists($func, self::$override_functions)) { throw new \RuntimeException("Function '{$func}' is marked as mocked already"); } self::$override_functions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); }
/** * Testcase Constructor for successful request. * * @return void */ public function setUpSuccess() { if (extension_loaded('curl') === FALSE) { $this->markTestSkipped('Extension curl is required.'); } if (extension_loaded('runkit') === FALSE) { $this->markTestSkipped('Extension runkit is required.'); } runkit_function_redefine('curl_errno', '', self::CURL_RETURN_ERRNO); runkit_function_redefine('curl_error', '', self::CURL_RETURN_ERRMSG); runkit_function_redefine('curl_getinfo', '', self::CURL_RETURN_INFO); $this->class = new CurlResponse('Result', 'handle'); $this->reflection = new ReflectionClass('Lunr\\Network\\CurlResponse'); }
public function __construct($functionName) { $this->functionName = $functionName; if (function_exists($functionName)) { if (!function_exists('runkit_function_redefine')) { throw new Exception("PECL runkit extension is required to override functions. see http://www.php.net/manual/en/runkit.installation.php for more info."); } $rf = new ReflectionFunction($functionName); $rp = $rf->getParameters(); $aParams = array(); foreach ($rp as $param) { $aParams[] = '$' . $param; } $fnc_code = '$mock = PHPMockFunction::getMock(\'' . $functionName . '\');' . "\n" . 'return $mock->invoke(func_get_args());'; runkit_function_redefine(strtolower($functionName), '', $fnc_code); } else { eval("function {$functionName}() {\n return PHPMockFunction::getMock('{$functionName}')->invoke(func_get_args());\n }"); } }
/** * Test for mysql related functions, using runkit_function_redefine * * @return void * * @group medium */ public function testMysqlDBI() { if (!PMA_HAS_RUNKIT) { $this->markTestSkipped("Cannot redefine function"); } //FOR UT, we just test the right mysql client API is called runkit_function_redefine('mysql_pconnect', '', 'return "mysql_pconnect";'); runkit_function_redefine('mysql_connect', '', 'return "mysql_connect";'); runkit_function_redefine('mysql_query', '', 'return "mysql_query";'); runkit_function_redefine('mysql_fetch_array', '', 'return "mysql_fetch_array";'); runkit_function_redefine('mysql_data_seek', '', 'return "mysql_data_seek";'); runkit_function_redefine('mysql_get_host_info', '', 'return "mysql_get_host_info";'); runkit_function_redefine('mysql_get_proto_info', '', 'return "mysql_get_proto_info";'); runkit_function_redefine('mysql_field_flags', '', 'return "mysql_field_flags";'); runkit_function_redefine('mysql_field_name', '', 'return "mysql_field_name";'); runkit_function_redefine('mysql_field_len', '', 'return "mysql_field_len";'); runkit_function_redefine('mysql_num_fields', '', 'return "mysql_num_fields";'); runkit_function_redefine('mysql_affected_rows', '', 'return "mysql_affected_rows";'); //test for fieldFlags $result = array("table1", "table2"); $ret = $this->object->numFields($result); $this->assertEquals('mysql_num_fields', $ret); //test for fetchRow $result = array("table1", "table2"); $ret = $this->object->fetchRow($result); $this->assertEquals('mysql_fetch_array', $ret); //test for fetchRow $result = array("table1", "table2"); $ret = $this->object->fetchAssoc($result); $this->assertEquals('mysql_fetch_array', $ret); //test for affectedRows $link = "PMA_link"; $get_from_cache = false; $ret = $this->object->affectedRows($link, $get_from_cache); $this->assertEquals("mysql_affected_rows", $ret); //test for connect $user = '******'; $password = '******'; $is_controluser = false; $server = array('port' => 8080, 'socket' => 123, 'host' => 'locahost'); $auxiliary_connection = true; //test for connect $ret = $this->object->connect($user, $password, $is_controluser, $server, $auxiliary_connection); $this->assertEquals('mysql_connect', $ret); $GLOBALS['cfg']['PersistentConnections'] = true; $ret = $this->object->connect($user, $password, $is_controluser, $server, $auxiliary_connection); $this->assertEquals('mysql_pconnect', $ret); //test for realQuery $query = 'select * from DBI'; $link = $ret; $options = 0; $ret = $this->object->realQuery($query, $link, $options); $this->assertEquals('mysql_query', $ret); //test for fetchArray $result = $ret; $ret = $this->object->fetchArray($result); $this->assertEquals('mysql_fetch_array', $ret); //test for dataSeek $result = $ret; $offset = 12; $ret = $this->object->dataSeek($result, $offset); $this->assertEquals('mysql_data_seek', $ret); //test for getHostInfo $ret = $this->object->getHostInfo($ret); $this->assertEquals('mysql_get_host_info', $ret); //test for getProtoInfo $ret = $this->object->getProtoInfo($ret); $this->assertEquals('mysql_get_proto_info', $ret); //test for fieldLen $ret = $this->object->fieldLen($ret, $offset); $this->assertEquals('mysql_field_len', $ret); //test for fieldName $ret = $this->object->fieldName($ret, $offset); $this->assertEquals('mysql_field_name', $ret); //test for fieldFlags $ret = $this->object->fieldFlags($ret, $offset); $this->assertEquals('mysql_field_flags', $ret); }
/** * Process content of CSV file * * @since 0.1 **/ public function generate_data() { // Check if the user clicked on the Save, Load, or Delete Settings buttons ## if (!isset($_POST['_wpnonce-q-eud-export-user-page_export']) || isset($_POST['load_export']) || isset($_POST['save_export']) || isset($_POST['delete_export'])) { return false; } // check admin referer ## check_admin_referer('q-eud-export-user-page_export', '_wpnonce-q-eud-export-user-page_export'); // build argument array ## $args = array('fields' => isset($_POST['user_fields']) && '1' == $_POST['user_fields'] ? 'all' : array('ID'), 'role' => sanitize_text_field($_POST['role'])); // did they request a specific program ? ## if (isset($_POST['program']) && $_POST['program'] != '') { $args['meta_key'] = 'member_of_club'; $args['meta_value'] = (int) $_POST['program']; $args['meta_compare'] = '='; } // is there a range limit in place for the export ? ## if (isset($_POST['limit_total']) && $_POST['limit_total'] != '') { // let's just make sure they are integer values ## $limit_offset = isset($_POST['limit_offset']) ? (int) $_POST['limit_offset'] : 0; $limit_total = (int) $_POST['limit_total']; if (is_int($limit_offset) && is_int($limit_total)) { $args['offset'] = $limit_offset; $args['number'] = $limit_total; // number - Limit the total number of users returned ## // test it ## #wp_die( $this->pr( $args ) ); } } // pre_user query ## add_action('pre_user_query', array($this, 'pre_user_query')); $users = get_users($args); remove_action('pre_user_query', array($this, 'pre_user_query')); // test args ## #wp_die( $this->pr ( $users ) ); // no users found, so chuck an error into the args array and exit the export ## if (!$users) { wp_redirect(add_query_arg('error', 'empty', wp_get_referer())); exit; } // get sitename and clean it up ## $sitename = sanitize_key(get_bloginfo('name')); if (!empty($sitename)) { $sitename .= '.'; } // export method ? ## $export_method = 'excel'; // default to Excel export ## if (isset($_POST['format']) && $_POST['format'] != '') { $export_method = sanitize_text_field($_POST['format']); } // set export filename structure ## $filename = $sitename . 'users.' . date('Y-m-d-H-i-s'); switch ($export_method) { case 'csv': // to csv ## header('Content-Description: File Transfer'); header('Content-Disposition: attachment; filename=' . $filename . '.csv'); header('Content-Type: text/csv; charset=' . get_option('blog_charset'), true); // set a csv check flag $is_csv = true; // nothing here $doc_begin = ''; //preformat $pre = ''; // how to seperate data ## $seperator = ','; // comma for csv ## // line break ## $breaker = "\n"; // nothing here $doc_end = ''; break; case 'excel': // to xls ## header('Content-Description: File Transfer'); header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename={$filename}.xls"); header("Pragma: no-cache"); header("Expires: 0"); // set a csv check flag $is_csv = false; //grab the template file (for tidy formatting) include 'xml-template.php'; // open xml $doc_begin = $xml_doc_begin; //preformat $pre = $xml_pre; // how to seperate data ## $seperator = $xml_seperator; // line break ## $breaker = $xml_breaker; // close xml $doc_end = $xml_doc_end; break; } // check for selected usermeta fields ## $usermeta = isset($_POST['usermeta']) ? $_POST['usermeta'] : ''; #$this->pr( $usermeta ); $usermeta_fields = array(); if ($usermeta && is_array($usermeta)) { foreach ($usermeta as $field) { $usermeta_fields[] = sanitize_text_field($field); } } #$this->pr( $usermeta_fields ); #exit; // check for selected x profile fields ## $bp_fields = isset($_POST['bp_fields']) ? $_POST['bp_fields'] : ''; $bp_fields_passed = array(); if ($bp_fields && is_array($bp_fields)) { foreach ($bp_fields as $field) { // reverse tidy ## $field = str_replace('__', ' ', sanitize_text_field($field)); // add to array ## $bp_fields_passed[] = $field; } } // cwjordan: check for x profile fields we want update time for ## $bp_fields_update = isset($_POST['bp_fields_update_time']) ? $_POST['bp_fields_update_time'] : ''; $bp_fields_update_passed = array(); if ($bp_fields_update && is_array($bp_fields_update)) { foreach ($bp_fields_update as $field) { // reverse tidy ## $field = str_replace('__', ' ', sanitize_text_field($field)); // add to array ## $bp_fields_update_passed[] = $field . " Update Date"; } } // global wpdb object ## global $wpdb; // debug ## if (self::debug) { self::log('generate_data(): merging array'); } // compile final fields list ## $fields = array_merge(self::get_user_fields(), self::get_special_fields(), $usermeta_fields, $bp_fields_passed, $bp_fields_update_passed); // test field array ## #self::pr( $fields ); // build the document headers ## $headers = array(); foreach ($fields as $key => $field) { // rename programs field ## if ($field == 'member_of_club') { $field = 'Program'; } // grab fields to exclude from exports ## if (in_array($field, $this->get_exclude_fields())) { // ditch 'em ## unset($fields[$key]); } else { if ($is_csv) { $headers[] = '"' . $field . '"'; } else { $headers[] = $field; #echo '<script>console.log("Echoing header cell: '.$field.'")</script>'; } } } // quick check ## #if ( self::debug ) self::log( 'All Fields: '. var_dump( $fields ) ); #if ( self::debug ) self::log( '$bp_fields_passed: '. var_dump( $bp_fields_passed ) ); // no more buffering while spitting back the export data ## ob_end_flush(); // get the value in bytes allocated for Memory via php.ini ## // @link http://wordpress.org/support/topic/how-to-exporting-a-lot-of-data-out-of-memory-issue $memory_limit = $this->return_bytes(ini_get('memory_limit')) * 0.75; // we need to disable caching while exporting because we export so much data that it could blow the memory cache // if we can't override the cache here, we'll have to clear it later... if (function_exists('override_function')) { override_function('wp_cache_add', '$key, $data, $group="", $expire=0', ''); override_function('wp_cache_set', '$key, $data, $group="", $expire=0', ''); override_function('wp_cache_replace', '$key, $data, $group="", $expire=0', ''); override_function('wp_cache_add_non_persistent_groups', '$key, $data, $group="", $expire=0', ''); } elseif (function_exists('runkit_function_redefine')) { runkit_function_redefine('wp_cache_add', '$key, $data, $group="", $expire=0', ''); runkit_function_redefine('wp_cache_set', '$key, $data, $group="", $expire=0', ''); runkit_function_redefine('wp_cache_replace', '$key, $data, $group="", $expire=0', ''); runkit_function_redefine('wp_cache_add_non_persistent_groups', '$key, $data, $group="", $expire=0', ''); } // open doc wrapper.. ## echo $doc_begin; // echo headers ## echo $pre . implode($seperator, $headers) . $breaker; #wp_die( self::pr( $users ) ); // build row values for each user ## foreach ($users as $user) { #wp_die( self::pr( $user ) ); // check if we're hitting any Memory limits, if so flush them out ## // per http://wordpress.org/support/topic/how-to-exporting-a-lot-of-data-out-of-memory-issue?replies=2 if (memory_get_usage(true) > $memory_limit) { wp_cache_flush(); } // open up a new empty array ## $data = array(); #wp_die( self::pr( $GLOBALS['bp'] ) ); // BP loaded ? ## if (function_exists('bp_is_active') && bp_is_active('xprofile')) { // grab all user data ## $bp_data = BP_XProfile_ProfileData::get_all_for_user($user->ID); } // single query method - get all user_meta data ## $get_user_meta = (array) get_user_meta($user->ID); #wp_die( self::pr( $get_user_meta ) ); // Filter out empty meta data ## #$get_user_meta = array_filter( array_map( function( $a ) { # return $a[0]; #}, $get_user_meta ) ); // loop over each field ## foreach ($fields as $field) { // check if this is a BP field ## if (isset($bp_data) && isset($bp_data[$field]) && in_array($field, $bp_fields_passed)) { // old way from single BP query ## $value = $bp_data[$field]; if (is_array($value)) { $value = maybe_unserialize($value['field_data']); // suggested by @grexican ## #$value = $value['field_data']; /** * cwjordan * after unserializing it we then * need to implode it so * that we have something readable? * Going to use :: as a separator * because that's what Buddypress Members Import * expects, but we might want to make that * configurable. */ if (is_array($value)) { $value = implode("::", $value); } } $value = self::sanitize($value); // check if this is a BP field we want the updated date for ## } elseif (in_array($field, $bp_fields_update_passed)) { global $bp; $real_field = str_replace(" Update Date", "", $field); $field_id = xprofile_get_field_id_from_name($real_field); $value = $wpdb->get_var($wpdb->prepare("\n SELECT last_updated \n FROM {$bp->profile->table_name_data} \n WHERE user_id = %d AND field_id = %d\n ", $user->ID, $field_id)); // include the user's role in the export ## } elseif (isset($_POST['roles']) && '1' == $_POST['roles'] && $field == 'roles') { // add "Role" as $value ## $value = isset($user->roles[0]) ? implode($user->roles, '|') : ''; // empty value if no role found - or flat array of user roles ## // include the user's BP group in the export ## } elseif (isset($_POST['groups']) && '1' == $_POST['groups'] && $field == 'groups') { if (function_exists('groups_get_user_groups')) { // check if user is a member of any groups ## $group_ids = groups_get_user_groups($user->ID); #self::pr( $group_ids ); #wp_die( pr( 'loaded group data.' )); if (!$group_ids || $group_ids == '') { $value = ''; } else { // new empty array ## $groups = array(); // loop over all groups ## foreach ($group_ids["groups"] as $group_id) { $groups[] = groups_get_group(array('group_id' => $group_id))->name . (end($group_ids["groups"]) == $group_id ? '' : ''); } // implode it ## $value = implode($groups, '|'); } } else { $value = ''; } } elseif ($field == 'bp_latest_update') { // https://bpdevel.wordpress.com/2014/02/21/user-last_activity-data-and-buddypress-2-0/ ## $value = bp_get_user_last_activity($user->ID); // user or usermeta field ## } else { // the user_meta key isset ## if (isset($get_user_meta[$field])) { // take from the bulk get_user_meta call - this returns an array in all cases, so we take the first key ## $value = $get_user_meta[$field][0]; // standard WP_User value ## } else { // use the magically assigned value from WP_Users $value = $user->{$field}; } // the $value might be serialized ## $value = self::unserialize($value); // the value is an array ## if (is_array($value)) { // recursive implode it ## $value = self::recursive_implode($value); } } // correct program value to Program Name ## if ($field == 'member_of_club') { $value = get_the_title($value); } // wrap values in quotes and add to array ## if ($is_csv) { $data[] = '"' . str_replace('"', '""', self::special_characters($value)) . '"'; // just add to array ## } else { $data[] = self::special_characters($value); } } // echo row data ## echo $pre . implode($seperator, $data) . $breaker; } // close doc wrapper.. echo $doc_end; // stop PHP, so file can export correctly ## exit; }
/** * Process content of CSV file * * @since 0.1 **/ public function generate_data() { // This function runs before we display the admin page, so we need to skip // the rest of this function if the user didn't click on the // Save, Load, or Delete Settings buttons if (!isset($_POST['_wpnonce-q-eud-export-user-page_export']) || isset($_POST['load_export']) || isset($_POST['save_export']) || isset($_POST['delete_export'])) { return false; } // check admin referer ## check_admin_referer('q-eud-export-user-page_export', '_wpnonce-q-eud-export-user-page_export'); // build argument array ## $args = array('fields' => 'all', 'role' => sanitize_text_field($_POST['role'])); // did they request a specific program ? ## if (isset($_POST['program']) && $_POST['program'] != '') { $args['meta_key'] = 'member_of_club'; $args['meta_value'] = (int) $_POST['program']; $args['meta_compare'] = '='; } // is there a range limit in place for the export ? ## if (isset($_POST['limit_offset']) && $_POST['limit_offset'] != '' && isset($_POST['limit_total']) && $_POST['limit_total'] != '') { // let's just make sure they are integer values ## $limit_offset = (int) $_POST['limit_offset']; $limit_total = (int) $_POST['limit_total']; if (is_int($limit_offset) && is_int($limit_total)) { $args['offset'] = $limit_offset; /* cwjordan codex says: * number - Limit the total number of users returned * so don't subtract the offset, like as not * that will give us a negative number for $args['number'] * e.g. offset of 1000 total of 100 */ $args['number'] = $limit_total; // $args['number'] = $limit_total - $limit_offset; #wp_die(pr($args)); } } /* pre_user query */ add_action('pre_user_query', array($this, 'pre_user_query')); $users = get_users($args); remove_action('pre_user_query', array($this, 'pre_user_query')); /* no users found, so chuck an error into the args array and exit the export */ if (!$users) { $referer = add_query_arg('error', 'empty', wp_get_referer()); wp_redirect($referer); exit; } /* get sitename and clean it up */ $sitename = sanitize_key(get_bloginfo('name')); if (!empty($sitename)) { $sitename .= '.'; } // export method ? ## $export_method = 'excel'; // default to Excel export ## if (isset($_POST['format']) && $_POST['format'] != '') { $export_method = sanitize_text_field($_POST['format']); } // set export filename structure ## $filename = $sitename . 'users.' . date('Y-m-d-H-i-s'); switch ($export_method) { case "csv": // to csv ## header('Content-Description: File Transfer'); header('Content-Disposition: attachment; filename=' . $filename . '.csv'); header('Content-Type: text/csv; charset=' . get_option('blog_charset'), true); // set a csv check flag $is_csv = true; // nothing here $doc_begin = ''; //preformat $pre = ''; // how to seperate data ## $seperator = ','; // comma for csv ## // line break ## $breaker = "\n"; // nothing here $doc_end = ''; break; case 'excel': // to xls ## header('Content-Description: File Transfer'); header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename={$filename}.xls"); header("Pragma: no-cache"); header("Expires: 0"); // set a csv check flag $is_csv = false; //grab the template file (for tidy formatting) include 'xml-template.php'; // open xml $doc_begin = $xml_doc_begin; //preformat $pre = $xml_pre; // how to seperate data ## $seperator = $xml_seperator; // line break ## $breaker = $xml_breaker; // close xml $doc_end = $xml_doc_end; break; } // function to exclude data ## $exclude_data = apply_filters('q_eud_exclude_data', array()); // check for selected usermeta fields ## $usermeta = isset($_POST['usermeta']) ? $_POST['usermeta'] : ''; $usermeta_fields = array(); if ($usermeta && is_array($usermeta)) { foreach ($usermeta as $field) { $usermeta_fields[] = sanitize_text_field($field); } } #pr($usermeta_fields); #exit; // check for selected x profile fields ## $bp_fields = isset($_POST['bp_fields']) ? $_POST['bp_fields'] : ''; $bp_fields_passed = array(); if ($bp_fields && is_array($bp_fields)) { foreach ($bp_fields as $field) { // reverse tidy ## $field = str_replace('__', ' ', sanitize_text_field($field)); // add to array ## $bp_fields_passed[] = $field; } } // cwjordan: check for x profile fields we want update time for ## $bp_fields_update = isset($_POST['bp_fields_update_time']) ? $_POST['bp_fields_update_time'] : ''; $bp_fields_update_passed = array(); if ($bp_fields_update && is_array($bp_fields_update)) { foreach ($bp_fields_update as $field) { // reverse tidy ## $field = str_replace('__', ' ', sanitize_text_field($field)); // add to array ## $bp_fields_update_passed[] = $field . " Update Date"; } } // global wpdb object ## global $wpdb; // exportable user data ## $data_keys = array('ID', 'user_login', 'user_nicename', 'user_email', 'user_url', 'user_registered', 'display_name'); // compile final fields list ## $fields = array_merge($data_keys, $usermeta_fields, $bp_fields_passed, $bp_fields_update_passed); // build the document headers ## $headers = array(); foreach ($fields as $key => $field) { // rename programs field ## if ($field == 'member_of_club') { $field = 'Program'; } if (in_array($field, $exclude_data)) { unset($fields[$key]); } else { if ($is_csv) { $headers[] = '"' . $field . '"'; } else { $headers[] = $field; #echo '<script>console.log("Echoing header cell: '.$field.'")</script>'; } } } // no more buffering while spitting back the export data ## ob_end_flush(); // get the value in bytes allocated for Memory via php.ini ## // @link http://wordpress.org/support/topic/how-to-exporting-a-lot-of-data-out-of-memory-issue?replies=2 $memory_limit = $this->return_bytes(ini_get('memory_limit')) * 0.75; // we need to disable caching while exporting because we export so much data that it could blow the memory cache // if we can't override the cache here, we'll have to clear it later... if (function_exists('override_function')) { override_function('wp_cache_add', '$key, $data, $group="", $expire=0', ''); override_function('wp_cache_set', '$key, $data, $group="", $expire=0', ''); override_function('wp_cache_replace', '$key, $data, $group="", $expire=0', ''); override_function('wp_cache_add_non_persistent_groups', '$key, $data, $group="", $expire=0', ''); } elseif (function_exists('runkit_function_redefine')) { runkit_function_redefine('wp_cache_add', '$key, $data, $group="", $expire=0', ''); runkit_function_redefine('wp_cache_set', '$key, $data, $group="", $expire=0', ''); runkit_function_redefine('wp_cache_replace', '$key, $data, $group="", $expire=0', ''); runkit_function_redefine('wp_cache_add_non_persistent_groups', '$key, $data, $group="", $expire=0', ''); } // open doc wrapper.. ## echo $doc_begin; // echo headers ## echo $pre . implode($seperator, $headers) . $breaker; // build row values for each user ## foreach ($users as $user) { // check if we're hitting any Memory limits, if so flush them out ## // per http://wordpress.org/support/topic/how-to-exporting-a-lot-of-data-out-of-memory-issue?replies=2 if (memory_get_usage(true) > $memory_limit) { wp_cache_flush(); } // open up a new empty array ## $data = array(); // BP loaded ? ## if (function_exists('bp_is_active')) { $bp_data = BP_XProfile_ProfileData::get_all_for_user($user->ID); } // loop over each field ## foreach ($fields as $field) { // check if this is a BP field ## if (isset($bp_data) && isset($bp_data[$field]) && in_array($field, $bp_fields_passed)) { $value = $bp_data[$field]; if (is_array($value)) { $value = maybe_unserialize($value['field_data']); // suggested by @grexican ## #$value = $value['field_data']; /** * cwjordan * after unserializing it we then * need to implode it so * that we have something readable? * Going to use :: as a separator * because that's what Buddypress Members Import * expects, but we might want to make that * configurable. */ if (is_array($value)) { $value = implode("::", $value); } } $value = $this->sanitize($value); // check if this is a BP field we want the updated date for ## } elseif (in_array($field, $bp_fields_update_passed)) { global $bp; $real_field = str_replace(" Update Date", "", $field); $field_id = xprofile_get_field_id_from_name($real_field); $value = $wpdb->get_var($wpdb->prepare("\n SELECT last_updated \n FROM {$bp->profile->table_name_data} \n WHERE user_id = %d AND field_id = %d\n ", $user->ID, $field_id)); // include the user's role in the export ## } elseif (isset($_POST['q_eud_role']) && $_POST['q_eud_role'] != '' && $field == 'role') { // add "Role" as $value ## $value = $user->roles[0] ? $user->roles[0] : ''; // empty value if no role found - note: we only take the first role assigned to the user ## // user data or usermeta ## } else { $value = isset($user->{$field}) ? $user->{$field} : ''; #$value = is_array( $value ) ? serialize( $value ) : $value; // maybe serialize the value ## $value = is_array($value) ? implode(", ", $value) : $value; // maybe serialize the value - suggested by @nicmare ## } // correct program value to Program Name ## if ($field == 'member_of_club') { $value = get_the_title($value); } if ($is_csv) { $data[] = '"' . str_replace('"', '""', $value) . '"'; } else { $data[] = $value; } } // echo row data ## echo $pre . implode($seperator, $data) . $breaker; } // close doc wrapper.. echo $doc_end; // stop PHP, so file can export correctly ## exit; }
/** * Overrides native PHP functions. * @return void */ protected function overrideNativeFuncs() { if (Daemon::supported(Daemon::SUPPORT_RUNKIT_INTERNAL_MODIFY)) { function define($k, $v) { if (defined($k)) { runkit_constant_redefine($k, $v); } else { runkit_constant_add($k, $v); } } $this->override('define'); function header() { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return call_user_func_array([Daemon::$context, 'header'], func_get_args()); } $this->override('header'); function is_uploaded_file() { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return call_user_func_array([Daemon::$context, 'isUploadedFile'], func_get_args()); } $this->override('is_uploaded_file'); function move_uploaded_file() { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return call_user_func_array([Daemon::$context, 'moveUploadedFile'], func_get_args()); } $this->override('move_uploaded_file'); function headers_sent(&$file = null, &$line = null) { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return Daemon::$context->headers_sent($file, $line); } //$this->override('headers_sent'); function headers_list() { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return Daemon::$context->headers_list(); } $this->override('headers_list'); function setcookie() { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return call_user_func_array([Daemon::$context, 'setcookie'], func_get_args()); } $this->override('setcookie'); /** * @param callable $cb */ function register_shutdown_function($cb) { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return Daemon::$context->registerShutdownFunction($cb); } $this->override('register_shutdown_function'); runkit_function_copy('create_function', 'create_function_native'); runkit_function_redefine('create_function', '$arg,$body', 'return \\PHPDaemon\\Core\\Daemon::$process->createFunction($arg,$body);'); } }
/** * Test for mysqli related functions, using runkit_function_redefine * * @return void * * @group medium */ public function testMysqliDBI() { if (!PMA_HAS_RUNKIT) { $this->markTestSkipped("Cannot redefine function"); } //FOR UT, we just test the right mysql client API is called runkit_function_redefine('mysqli_real_connect', '', 'return "mysqli_real_connect";'); runkit_function_redefine('mysqli_init', '', 'return "mysqli_init";'); runkit_function_redefine('mysqli_options', '', 'return "mysqli_options";'); runkit_function_redefine('mysqli_query', '', 'return "mysqli_query";'); runkit_function_redefine('mysqli_multi_query', '', 'return "mysqli_multi_query";'); runkit_function_redefine('mysqli_fetch_array', '', 'return "mysqli_fetch_array";'); runkit_function_redefine('mysqli_data_seek', '', 'return "mysqli_data_seek";'); runkit_function_redefine('mysqli_more_results', '', 'return "mysqli_more_results";'); runkit_function_redefine('mysqli_next_result', '', 'return "mysqli_next_result";'); runkit_function_redefine('mysqli_get_host_info', '', 'return "mysqli_get_host_info";'); runkit_function_redefine('mysqli_get_proto_info', '', 'return "mysqli_get_proto_info";'); runkit_function_redefine('mysqli_get_client_info', '', 'return "mysqli_get_client_info";'); $user = '******'; $password = '******'; $is_controluser = false; $server = array('port' => 8080, 'socket' => 123, 'host' => 'locahost'); $auxiliary_connection = true; //test for connect $ret = $this->object->connect($user, $password, $is_controluser, $server, $auxiliary_connection); $this->assertEquals('mysqli_init', $ret); //test for realQuery $query = 'select * from DBI'; $link = $ret; $options = 0; $ret = $this->object->realQuery($query, $link, $options); $this->assertEquals('mysqli_query', $ret); //test for realMultiQuery $ret = $this->object->realMultiQuery($link, $query); $this->assertEquals('mysqli_multi_query', $ret); //test for fetchArray $result = $ret; $ret = $this->object->fetchArray($result); $this->assertEquals('mysqli_fetch_array', $ret); //test for fetchAssoc $result = $ret; $ret = $this->object->fetchAssoc($result); $this->assertEquals('mysqli_fetch_array', $ret); //test for fetchRow $result = $ret; $ret = $this->object->fetchRow($result); $this->assertEquals('mysqli_fetch_array', $ret); //test for dataSeek $result = $ret; $offset = 10; $ret = $this->object->dataSeek($result, $offset); $this->assertEquals('mysqli_data_seek', $ret); //test for moreResults $link = $ret; $ret = $this->object->moreResults($link); $this->assertEquals('mysqli_more_results', $ret); //test for nextResult $link = $ret; $ret = $this->object->nextResult($link); $this->assertEquals('mysqli_next_result', $ret); //test for getHostInfo $link = $ret; $ret = $this->object->getHostInfo($link); $this->assertEquals('mysqli_get_host_info', $ret); //test for getProtoInfo $link = $ret; $ret = $this->object->getProtoInfo($link); $this->assertEquals('mysqli_get_proto_info', $ret); //test for getClientInfo $ret = $this->object->getClientInfo(); $this->assertEquals('mysqli_get_client_info', $ret); }
/** * Test that parse() returns an ast array on success. * * @requires extension runkit * @covers Lunr\Shadow\GetoptCliParser::parse */ public function testParseReturnsAstOnSuccess() { runkit_function_redefine('getopt', '', self::PARSE_WORKS); $value = $this->class->parse(); $this->assertInternalType('array', $value); $this->assertEquals(array('a' => array(), 'b' => array('arg')), $value); }
/** * Creates runkit function to be used for mocking, taking care of callback to this object. * * Also temporary renames the original function if there is. */ protected function createFunction() { if (function_exists($this->function_name)) { $this->restore_name = 'restore_' . $this->function_name . '_' . $this->id . '_' . uniqid(); runkit_function_copy($this->function_name, $this->restore_name); runkit_function_redefine($this->function_name, '', $this->getCallback()); } else { runkit_function_add($this->function_name, '', $this->getCallback()); } $this->active = true; }
/** * Overrides native PHP functions. * @return void */ public function overrideNativeFuncs() { if (Daemon::supported(Daemon::SUPPORT_RUNKIT_INTERNAL_MODIFY)) { runkit_function_rename('header', 'header_native'); function header() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return call_user_func_array(array(Daemon::$req, 'header'), func_get_args()); } } runkit_function_rename('is_uploaded_file', 'is_uploaded_file_native'); function is_uploaded_file() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return call_user_func_array(array(Daemon::$req, 'isUploadedFile'), func_get_args()); } } runkit_function_rename('move_uploaded_file', 'move_uploaded_file_native'); function move_uploaded_file() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return call_user_func_array(array(Daemon::$req, 'moveUploadedFile'), func_get_args()); } } runkit_function_rename('headers_sent', 'headers_sent_native'); function headers_sent() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return Daemon::$req->headers_sent(); } } runkit_function_rename('headers_list', 'headers_list_native'); function headers_list() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return Daemon::$req->headers_list(); } } runkit_function_rename('setcookie', 'setcookie_native'); function setcookie() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return call_user_func_array(array(Daemon::$req, 'setcookie'), func_get_args()); } } register_shutdown_function(array($this, 'shutdown')); runkit_function_rename('register_shutdown_function', 'register_shutdown_function_native'); function register_shutdown_function($cb) { if (Daemon::$req) { return Daemon::$req->registerShutdownFunction($cb); } } runkit_function_copy('create_function', 'create_function_native'); runkit_function_redefine('create_function', '$arg,$body', 'return __create_function($arg,$body);'); function __create_function($arg, $body) { static $cache = array(); static $maxCacheSize = 64; static $sorter; if ($sorter === NULL) { $sorter = function ($a, $b) { if ($a->hits == $b->hits) { return 0; } return $a->hits < $b->hits ? 1 : -1; }; } $crc = crc32($arg . "" . $body); if (isset($cache[$crc])) { ++$cache[$crc][1]; return $cache[$crc][0]; } if (sizeof($cache) >= $maxCacheSize) { uasort($cache, $sorter); array_pop($cache); } $cache[$crc] = array($cb = eval('return function(' . $arg . '){' . $body . '};'), 0); return $cb; } } }
/** * Mock a PHP function. * * @param String $name Function name * @param String $mock Replacement code for the function * * @return void */ protected function mock_function($name, $mock) { if (function_exists($name . self::FUNCTION_ID) === FALSE) { runkit_function_copy($name, $name . self::FUNCTION_ID); } runkit_function_redefine($name, '', $mock); }
private function replaceFunction() { $funcName = $this->funcName; $this->parameters = $this->getParameters($funcName); runkit_function_copy($funcName, $this->tmpFuncName); $result = runkit_function_redefine($this->funcName, $this->parameters, "return MockFunctionContainer::call('{$funcName}', func_get_args());"); if (!$result) { throw new Exception("Error trying to replace function"); } MockFunctionContainer::register($funcName, $this); }
protected function _reverseFunctionCallRedirection() { $newOrigFuncName = $this->_functionName . $this->_origFuncSuffix; $spyFuncName = $this->_functionName . $this->_spyFuncSuffix; runkit_function_remove($spyFuncName); //keep memory address of function to prevent seg fault runkit_function_redefine($this->_functionName, '', '$args = func_get_args(); return call_user_func_array("' . $newOrigFuncName . '", $args);'); }