prepared mysql statements and arrays - php

Solved the problem earlier today. In the 2nd line of code global $db,$tags; was overwriting if($this->has_lead_type_selected($person['ID'],$tags)) which caused the global $tags to overwrite $tags at the lower portion of code. So the global var was empty because it was before the actual $tags var was given a function.
When an HTML form is submitted to our REST API it sends data called 'lead_type' which are simply tags to identify the lead being sent.
User select these tags from a tag cloud. If a form is submitted to the API with one of these tags (lead_type) and any of our users profiles match those tags (they selected in their tag cloud). The user is sent a SMS to notify them.
Everything posts to the database tables, the API works but everyone of the users still gets a SMS even if they don't have matching tags. If I comment out the line (i'll show the rest of the code below) an SMS is sent to everyone. If I leave it uncommented no SMS is sent to anyone.
if($this->has_lead_type_selected($person['ID'],$tags))
Here is how the code flows.
}
private function has_lead_type_selected($user_id,$tags){
global $db,$tags;
$lead_types = explode(',',$tags);
$user_lead_types = $db
->where('user_id',$user_id)
->where('lead_type_id', $lead_types, 'IN')
->get('user_lead_types');
return sizeof($user_lead_types) > 0;
}
//Get lead types from API post and create $tags
$lead_types = $this->request['leadData']['lead_types'];
$strTags = array();
if(!empty($lead_types))
$strTags = explode(',',$lead_types);
$tags = '';
$lead_types_objects = $db->where('lead_type', $strTags,'IN')->get('lead_types');
foreach($lead_types_objects as $l)
{
if($tags=='')
$tags = $l['id'];
else
$tags.=',' .$l['id'];
}
We then send them a SMS if the form tags matched the users cloud tags.
global $sid,$token;
$client = new Twilio\Rest\Client($sid, $token);
$content_data = [
"leadname" => $posted_name,
"leadzipcode" => $posted_zipcode,
"leadphone" => $posted_phone,
"leademail" => $posted_email,
"leadtags" => $lead_types
];
//Replace Content
foreach($content_data as $index => $value){
$lead_sms_template = str_replace("|".$index."|", $value, $lead_sms_template);
}
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $person) {
try{
//commented temporarily -- uncommented below to try to solve issue of texting everyone still
if($this->has_lead_type_selected($person['ID'],$tags))
{
$number = $person['phone_no'];
$name = $person['first_name']. ' '. $person['last_name'];
Thank you for the help.

In the 2nd line of code global $db,$tags; was overwriting if($this->has_lead_type_selected($person['ID'],$tags)) which caused the global $tags to overwrite $tags at the lower portion of code. So the global var was empty because it was before the actual $tags var was given a function.

Related

OpenVBX block caller id patterns

We are getting automated calls coming to twillio. We do have openvbx installed. I am trying to figure out how I can block calls with incoming DID that have 111, 110, or 101 in the beginning. I know how to input static numbers in openvbx and I can successfully block them.
Any help would be appreciated.
Hi Twilio Customer Support here.
Have you thought about using the Twilio verb? It will allow you to build a list of numbers you do not wish to receive calls from:
https://www.twilio.com/docs/api/twiml/reject
https://www.twilio.com/docs/howto/reject
Regards,
Tom
I guess there is no support for openvbx on here and from twilio. To post a question here on stackoverflow and tag it openvbx it said I needed to have a certain number of points. I am new to this so my account has no points.
Anyways I solved my own question. Here is the explanation to other users looking for a similar solution.
You need to edit plugins/standard/applets/start/twiml.php in your openvbx installation and make it the code below. I hope there is more support out there for openvbx.
<?php
$ci =& get_instance();
$list = AppletInstance::getValue('list');
$direction = isset($_REQUEST['Direction']) ? $_REQUEST['Direction'] : 'inbound';
// block calls
$caller = normalize_phone_to_E164(isset($_REQUEST['From'])? $ci->input->get_post('From') : '');
$response = new TwimlResponse;
// Update this list of numbers
$block_list = array('+112345678910');
//pattern analysis to block DIDs
$patterns = array('111','110','101','+111','+110','+101');
$position = 0;
foreach ($patterns as $pattern) {
if( $position = strpos(' ' . $caller, $pattern)) {
if( $position == 1 ) $response->reject(array('reason' => 'busy'));
}
}
if (in_array($caller, $block_list)) {
$response->reject(array('reason' => 'busy'));
}else{
$next = AppletInstance::getDropZoneUrl('next');
if (!empty($next)) {
$response->redirect($next);
}
}
$response->respond();
?>

Session issue in PHP

I am trying to build a simple shopping cart using PHP and no Database.
Based on the user input, I search for an Item using ebay API(returns XML Data).
I am able to get its price,id,and other details.
I am then creating an array called ITEMS which contains all the data returned by ebay.
(I know I have to create a cart only for items that are selected).
The Issue is I am able to access the cart contents from the session within the search function but not from other functions.I am new to PHP could someone help me fix this.
As of now I am only trying to access the cart contents from buy.
function search(){
$xml = new SimpleXMLElement($xmlstr);
print "<table border=1>";
$loop = $xml->categories[0]->category->items->product;
foreach ( $loop as $dummy) {
$id = $dummy->attributes();
$link = $dummy->productOffersURL;
$name = $dummy->name;
$price = $dummy->minPrice;
$image = $dummy->images->image->sourceURL;
}
array_push($ITEMS, $item);
}
}
I changed the for eeach loop and it worked:
The code is :
$id = (String) $dummy->attributes();
$link = (String)$dummy->productOffersURL;
$name = (String)$dummy->name;
$price = (String)$dummy->minPrice;
$id = (String) $dummy->attributes();
$link = (String)$dummy->productOffersURL;
$name = (String)$dummy->name;
$price = (String)$dummy->minPrice;
You shouldn't call session start with the # (error supression) modifier. It could be triggering and error and you wouldn't know.
Check if your php.ini configuration is set to autostart the session.
session.auto_start = 0
If it's set to 1, you could be overwriting its contents by manually calling session start every time.

Rate limit. Twitter API

I'm working on a small and simple code which basically does some tweets filtering. The problem is that I'm hitting the request limit of Twitter API and I would like to know if there is a workaround or if what I want to do just cannot be done.
First, I type a twitter username to retrieve the ID's of people this user follows.
$user_id = $_GET["username"];
$url_post = "http://api.twitter.com/1/friends/ids.json?cursor=-1&screen_name=" . urlencode($user_id);
$following = file_get_contents($url_post, true);
$json = json_decode($following);
$ids = $json->ids;
Twitter API responds with a list of ID's.
Here comes the problem. The next step is to make a request to find out username, profile picture and description for each one of those ID's.
$following = array();
foreach ($ids as $value)
{
$build_url = 'http://api.twitter.com/1/users/lookup.json?user_id=' . $value . '';
$following[] = $build_url;
}
foreach ($following as $url)
{
$data_names = file_get_contents($url, true); //getting the file content
$json_names = json_decode($data_names);
foreach ($json_names as $tweet) {
$name = $tweet->name;
$description = $tweet->description;
echo '<p>';
echo $name . '<br>';
echo $description;
echo '</p>';
}
}
If the user follows 50 people it works. But if he follows, let's say, 600 hundred, that would be 600 hundred request (for username, description and profile pic) to Twitter API which exceeds the limit.
Is there any way to workaround this o it just cannot be done?
Thank you!
You can and should request users/lookup API endPoint with 100 userIds at a time, instead of doing one request per twitter ID. cf. https://dev.twitter.com/docs/api/1.1/get/users/lookup
You have to replace your forEach loop (foreach ($following as $url)) by a recursive function.
At the end of the function, check the number of hits remaining before calling it again (cf. this link to see how to know the time remining until you get rate limited).
If there is no hit left, sleep 15 minutes before calling the function again, otherwise do the call again.
There is plenty of information on how to do this, use Google and search existing stackOverflow questions.

active users script, user count not working properly

i have written a script to output active users on my site....
part of this is counting unique ips in the log, as the array i use to split the lines / data unloads active users from the array list after 5 minutes.....
however the "3 online users now" count is not working properly.....
it kinda works.... when someone views a page, it says there is 1 user
lets say i view a page.... 1 visitor
then user 2 views a page .... 2 visitors
but if i then view another page, it displays 3 users.....
even though i use the same ip for both page requests....
here is my code
$data = file_get_contents('active-log.txt');
$break = "\r\n";
$lines = explode($break, $data);
foreach ($lines as $key => $value) {
$active_ip[] = $lines[$key][1];
}
$active_ip_count = array_unique($active_ip);
$active_users = (count($active_ip_count));
$active_users is the variable i use to display how many unique visitors are online at one time
thanks in advance for anyone that can help me thanks
....
EDIT
.....
here is a sample of the log saved....
1328469393|157.55.39.84|g-book
1328469398|157.55.39.84|downloads
1328469400|157.55.39.84|badger
1328469404|157.55.39.84|home
1328469408|157.55.39.84|boneyard-dogs
the first part is timestamp (to remove the line from array, if timestamp is older than 5 minutes... this works fine)
the second part is ip
third part is page viewed and the new line is created with \r\n
$lines[$key][1] is the variable for each ip in each line....
as im not exacly a php expert, when writing scripts, i test them heavily while developing, and each time i add a new line of script , i echo the data, to check its what i hope, to make sure i make no mistakes......
here is a section of code that i didnt paste as i didnt think it was necessary....
foreach($lines as $k=>$v) {
$lines[$k] = explode("|", $v); }
// echo $lines[0][0]; // now this is first array of first line .... line 2 / url would be - $lines[1][2]
this is in my code, straight after the line "$lines = explode($break, $data);" in my code
Have you looked at the output of var_dump($active_ip) after the foreach loop ends? With this setup, I'm pretty sure $lines[$key][1] is simply the first character of the line you're dealing with, so that's not going to work well for a number of reasons. What does active-log.txt look like? Does it only contain IP addresses or user names, too? If it only contains IP addresses, consider using something like this:
<?php
$data = file('active-log.txt');
$no_duplicate_ips = array_unique($data);
$active_users = (count($no_duplicate_ips));
?>
Edit:
Right, that makes sense then. Try this:
$data = file_get_contents('active-log.txt');
$break = "\r\n"; //Note that it's generally a good idea to use PHP_EOL throughout your code, for greater cross-platform compatibility
$lines = explode($break, $data);
$exploded_data = array();
$active_ips = array();
foreach($lines as $v) {
$exploded_data = explode("|", $v);
//Now check whether the timestamp is not > 5 min
if(TIMESTAMP CHECK HERE) {
//OK, this one is not too old
$active_ips[] = $exploded_data[1];
}
}
$active_ip_count = array_unique($active_ip);
$active_users = (count($active_ip_count));

problem calculating the position using php

I have a function below which gets name from a site. Below is the partial code, not complete. The values are passed thru a for loop using php.
function funct($$name,$page)
{
$url="http://testserver.com/client?list=$name&page=$page";
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,url);
$result=curl_exec($ch);
$dom = new DOMDocument();
#$dom->loadHTML($result);
$xpath=new DOMXPath($dom);
$elements = $xpath->evaluate("//div");
foreach ($elements as $element)
{
$name = $element->getElementsByTagName("name")->item(0)->nodeValue;
$position=$position +1;
echo $name.$position;
}
}
The code works fine but when i get a name i need to add a position and for each name it will be incremented by 1 to make it contentious. But when the values for the pages are passed, for an example when i move from page 1 to page 2. the count starts again from first, next page... same problem.
How can i make it continues on every page?
Either make $position a global variable (global $position;) or pass it to the function: function funct($name, $page, &$position). (What's with the variable variable $$name in your function signature?)
Use $_SESSION. It's designed specifically to maintain state.

Categories