cron jobs doesn't work with some php function - php

when i set some cron jobs for loading a php file cron doesn't work and it seams some php function have problem with cron job.
i know my cron command is true because i tested my cron job working true with simple php code that put date to a text file so my cron command is true i tested all ways of command like : wget, crul , cd, php , /user/local/bin/php and another but i don't know why my php code doesn't work and too i test that codes working very well when i loading php files with my browser.
my php file code:
<?php
header('Content-Type: text/html; charset=utf-8');
include ('simple_html_dom.php');
$mycache_url = 'http://example3.com';
$mybziran_url = 'http://example2.com';
function addhttp($url)
{
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
global $mybziran_url;
$url = ltrim($url, '/');
$url = $mybziran_url . '/' . $url;
}
return $url;
}
function urlencodeproblem($badurl)
{
$badurl = urlencode($badurl);
$badchar = array('%3A', '%2F');
$truechar = array(':', '/');
$badurl = str_replace($badchar, $truechar, $badurl);
return $badurl;
}
$url_html = #file_get_html($mycache_url);
$bziran_url = '';
$bziran_title = '';
foreach (#$url_html->find('a') as $elements) {
$bziran_url[] = urlencodeproblem($elements->href);
$bziran_title[] = $elements->innertext;
}
$myi = count($bziran_url);
for ($i = 0; $i < $myi; ++$i) {
$post_title = $bziran_title[$i];
$post_url = $bziran_url[$i];
$html = #file_get_html($post_url);
foreach ($html->find('div.price') as $myhtml_price_adelete) {
echo '######' . $myhtml_price_adelete->innertext . '######';
}
$bad_title_my = '';
foreach ($html->find('h1 a') as $myhtml_price_adelete) {
$bad_title_my .= $myhtml_price_adelete->innertext;
}
if (empty($bad_title_my)) {
echo $post_url;
echo 'prob';
} else {
$kalame = urlencode($post_title);
$A2_html = 'ok';
foreach ($html->find('a') as $myhtml_a_code) {
$e_ahref = addhttp($myhtml_a_code->href);
$myhtml_a_code->href = $e_ahref;
$myhtml_a_code->target = '_blank';
}
$html->save();
foreach ($html->find('img') as $myhtml_img_code) {
if (strpos($myhtml_img_code->src, 'base64') === false) {
$e_imgsrc = addhttp($myhtml_img_code->src);
$myhtml_img_code->src = $e_imgsrc;
}
}
$html->save();
$mymeta_keyword = '';
foreach ($html->find('meta[name=keywords]') as $myhtml_keyword) {
$mymeta_keyword[] = $myhtml_keyword->content;
}
foreach ($html->find('p') as $mytagdelete) {
if (strpos($mytagdelete->innertext, 'tag :') !== false) {
$mytagdelete->outertext = '';
}
}
$html->save();
foreach ($html->find('h1 a') as $myadelete) {
$myadelete->outertext = $myadelete->innertext;
}
$html->save();
$a3_href = '';
$a2_href = $html->find("img[alt=buy]");
foreach ($a2_href as $a2_href) {
$a2_href->outertext =
'<br><p align="center"><img alt="pay-download" src="http://exam.com/tmp_files/01-pay-download.png"></p>';
}
$html->save();
echo '<br>buy : ' . $a3_href . '<br>';
$myhtmlcode3 = '';
foreach ($html->find('div.prod') as $myhtmlcode) {
$myhtmlcode3 .= $myhtmlcode->outertext;
}
$html->save();
echo '<br>*** title ***' . $post_title . '<br>';
echo $post_url . '<br>';
$i_t = mt_rand(1, 34);
$mysaier_mahsolat = '<br>
<a target="_blank" href="http://ayta.ir/index.php?page=' . $i_t . '"> click </a>
<br>';
echo $mysaier_mahsolat;
echo $A2_html . $myhtmlcode3 . '<br>';
echo 'keyword :' . $mymeta_keyword[0];
}
}
?>
<?
$crontest = date("Y-m-d - h:i:s") . "\n" . file_get_contents(dirname(__file__) .
DIRECTORY_SEPARATOR . "cron.txt");
echo $crontest;
file_put_contents("cron.txt", $crontest);
?>

Related

Show existing content of file when editing it PHP

I open a .ini, then write some add some values to its parameters and then i saved it back.
My writing the content works perfectly fine, but when i open the file the previous content of that file is cleared.[Maybe due to my write mode].
What change should i do in my code to let user see what the previous content was when he edits a file.
file_get_contents($path.$filename);
/*Get All The datas from that file*/
$this->data['params'] = $this->parameter_m->get();
/*Getting the parameters to display on view*/
$this->data['parameters'] = parse_ini_file($path.$filename,true);
while (current($this->data['parameters']) )
{
$param_set = current($this->data['parameters']);
$param_type = key($this->data['parameters']);
foreach ($param_set as $key =>$value)
{
$this->data['parameters'][$param_type][$key] = $this->input->post($key);
}
next($this->data['parameters']);
}
$this->load->helper('file');
$this->load->library('ini');
$file = $path.$filename;
$ini = new INI($file);
$ini->read($file);
$ini->write($file, $this->data['parameters']);
My Write Function
function write($file = NULL, $data_file = array(), $sections = TRUE) {
$this->data_file = (!empty($data_file)) ? $data_file : $this->data_file;
$this->file = ($file) ? $file : $this->file;
$this->sections = $sections;
$content = NULL;
if ($this->sections) {
foreach ($this->data_file as $section => $data_file) {
$content .= '[' . $section . ']' . PHP_EOL;
foreach ($data_file as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[] = ' . (is_numeric($v) ? $v : $v ) . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . ' = ' . PHP_EOL;
} else {
$content .= $key . ' = ' . (is_numeric($val) ? $val : $val ) . PHP_EOL;
}
}
$content .= PHP_EOL;
}
} else {
foreach ($this->data_file as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . ' = ' . PHP_EOL;
} else {
$content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
}
}
}
return (($handle = fopen($this->file, 'w+')) && fwrite($handle, trim($content)) && fclose($handle)) ? TRUE : FALSE;
}

how can i make a span element in this line of php

I what to put a span element for $term['nodes']
I have tried to put after bracket and between but nothing works for me
if (isset($term['nodes'])) {
$term['name'] = $term['name'] . ' (' . $term['nodes'] . ')';
}
here is the all functin
function bootstrap_taxonomy_menu_block($variables) {
$tree = $variables['items'];
$config = $variables['config'];
$num_items = count($tree);
$i = 0;
$output = '<ul class="nav nav-pills nav-stacked">';
foreach ($tree as $tid => $term) {
$i++;
// Add classes.
$attributes = array();
if ($i == 1) {
$attributes['class'][] = '';
}
if ($i == $num_items) {
$attributes['class'][] = '';
}
if ($term['active_trail'] == '1') {
$attributes['class'][] = 'active-trail';
}
if ($term['active_trail'] == '2') {
$attributes['class'][] = 'active';
}
// Alter link text if we have to display the nodes attached.
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
}
// Set alias option to true so we don't have to query for the alias every
// time, as this is cached anyway.
$output .= '<li' . drupal_attributes($attributes) . '>' . l($term['name'], $term['path'], $options = array('alias' => TRUE));
if (!empty($term['children'])) {
$output .= theme('taxonomy_menu_block__' . $config['delta'], (array('items' => $term['children'], 'config' => $config)));
}
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
i what this for the bootstrap cdn class , i have move the function on template.php , of drupal theme , but the span element is in plain text in browser
Try this:
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
echo $term['name']; // To see the output
}

foreach loop does not work with include

Would someone of you know why I'm not able to use a (long)piece of code within a foreach loop?
The code in the foreach loop is only executed once.
This code at topictweets.php works fine on its own but I want to repeat it for each forum.
The foreach loop works fine without the include. I also tried to have the code from topic tweets.php plainly in the foreach loop, this didn't work either of course.
The code it includes is used to get topics of a forum from the database and find related tweets, and save those in the database.
Is there some other way to do this?
foreach ($forumlist as $x => $fID) {
echo 'id:'.$fID.'<br>';
include 'topictweets.php';
/////////
////////
}
online version: http://oudhollandsedrop.nl/webendata/FeedForum/fetchtweets.php
bunch of code in topic tweets.php
<?php
//?/ VVVV ---- SELECT TOPICS FOR CURRENT FORUM ----- VVVV ////
echo $fID;
$sql = "SELECT Topics_TopicID
FROM Topics_crosstable
WHERE Forums_ForumID = '$fID'";
$result = mysql_query($sql);
if (!$result) {
//echo 'The topiclist could not be displayed, please try again later.';
} else {
if (mysql_num_rows($result) == 0) {
// echo 'This topic doesn′t exist.';
} else {
while ($row = mysql_fetch_assoc($result)) {
//display post data
// echo $row['Topics_TopicID'];
// echo': ';
$topic = "SELECT Name
FROM Topics
WHERE TopicID = " . mysql_real_escape_string($row['Topics_TopicID']);
$topicname = mysql_query($topic);
if (!$topicname) {
// echo 'The topic could not be displayed, please try again later.';
} else {
if (mysql_num_rows($topicname) == 0) {
// echo 'This topic doesn′t exist.';
} else {
while ($row = mysql_fetch_assoc($topicname)) {
//display post data
// echo $row['Name'];
// echo'<br>';
$topiclist[] = $row['Name'];
}
}
}
}
}
}
foreach ($topiclist as $key => $value) {
$terms .= "" . $value . ",";
}
//echo'<p>';
//echo rtrim($terms, ",");
//echo'<p>';
//echo'<p>';
//echo $terms;
//$terms="vintage";
//Twitter account information
$username = "Username";
$password = "Password";
while (true) {
//$terms="vintage";
//echo "search terms: " . substr_replace($terms, "", -1) . "\n";
$url = "https://stream.twitter.com/1/statuses/filter.json";
$cred = sprintf('Authorization: Basic %s', base64_encode("$username:$password"));
$param = "track=" . urlencode(substr_replace($terms, "", -1));
$opts = array(
'http' => array(
'method' => 'POST',
'header' => $cred,
'content' => $param,
'Content-type' => 'application/x-www-form-urlencoded'),
'ssl' => array('verify_peer' => false)
);
$ctx = stream_context_create($opts);
$handle = fopen($url, 'r', false, $ctx);
//var_dump($handle);
$content = "";
$flag = true;
while ($flag) {
$buffer = fread($handle, 100);
//$buffer = stream_get_line($handle, 1024, "\n");
$a = explode("\n", $buffer, 2);
$content = $content . $a[0];
#var_dump($a);
if (count($a) > 1) {
#echo $content;
#echo "\n";
$r = json_decode($content, true);
#var_dump($r);
// echo '<p>';
// echo "text: " . $r["text"];
// echo '<br>';
// echo "\nrceated_at: " . $r["created_at"];
// echo '<br>';
// echo "\nuser screen name: " . $r["user"]["screen_name"];
// echo '<br>';
// echo "\nuser id: " . $r["user"]["id"];
// echo '<br>';
// echo "\nid : " . $r["id"];
// echo '<br>';
// echo "\nin_reply_to_status_id: " . $r["in_reply_to_status_id"];
// echo '<p>';
// echo "\n\n";
$created_at = $r["created_at"];
$created_at = strtotime($created_at);
$mysqldate = date('Y-m-d H:i:s', $created_at);
//
// echo'<p>';
foreach ($topiclist as $key => $value) {
// echo'getshere!';
//$whichterm = $r["text"];
$whichterm = '"' . $r["text"] . '"';
//echo $whichterm;
if (stripos($whichterm, $value) !== false) {
// echo 'true:' . $value . '';
//find topicid
$whattopic = "SELECT TopicID
FROM Topics
WHERE Name = '$value'";
//var_dump($whattopic);
$tID = mysql_query($whattopic);
//var_dump($tID);
if (!$tID) {
// echo 'topic id not found.';
} else {
if (mysql_num_rows($tID) == 0) {
// echo 'This topic doesn′t exist.';
} else {
while ($rec = mysql_fetch_assoc($tID)) {
$inserttweets = "INSERT INTO
Tweets(Topics_TopicID, AddDate, Tweetcontent)
VALUES('" . mysql_real_escape_string($rec['TopicID']) . "',
'" . mysql_real_escape_string($mysqldate) . "',
'" . mysql_real_escape_string($r["text"]) . "')";
//WHERE TopicID = " . mysql_real_escape_string($row['Topics_TopicID'])
}
}
$addtweet = mysql_query($inserttweets);
if (!$addtweet) {
//something went wrong, display the error
//echo 'Something went wrong while adding tweet.';
//echo mysql_error(); //debugging purposes, uncomment when needed
} else {
echo 'Succesfully added tweet';
}
}
}
}
die();
$content = $a[1];
}
}
fclose($handle);
}
?>
"Pasting" a bunch of code inside a loop isn't a great practice. In fact, what you're looking for is a function or the use of a defined class. So, if you can, define a function in your topictweets.php that will contain your code and use it in your loop:
include 'topictweets.php';
foreach ($forumlist as $x => $fID) {
echo 'id:'.$fID.'<br>';
processYourForums($fID);
/////////
////////
}
try include_once()
however, why not have a loop within topictweets.php?
you can do the query, etc.. in this page, but then loop through it in the include
This should work fine:
include 'topictweets.php';
foreach ($forumlist as $x => $fID) {
echo 'id:'.$fID.'<br>';
}
You only need to include once.

How to iterate this in a foreach construct

This one only covers the first record in the array -- $form[items][0][description]. How could I iterate this to be able to echo succeeding ones i.e
$form[items][1][description];
$form[items][2][description];
$form[items][3][description];
and so on and so forth?
$array = $form[items][0][description];
function get_line($array, $line) {
preg_match('/' . preg_quote($line) . ': ([^\n]+)/', $array['#value'], $match);
return $match[1];
}
$anchortext = get_line($array, 'Anchor Text');
$url = get_line($array, 'URL');
echo '' . $anchortext . '';
?>
This should do the trick
foreach ($form['items'] as $item) {
echo $item['description'] . "<br>";
}
I could help you more if I saw the body of your get_line function, but here's the gist of it
foreach ($form['items'] as $item) {
$anchor_text = get_line($item['description'], 'Anchor Text');
$url = get_line($item['description'], 'URL');
echo "{$anchor_text}";
}
You can use a for loop to iterate over this array.
for($i=0; $i< count($form['items']); $i++)
{
$anchortext = get_line($form['items'][$i]['description'], 'Anchor Text');
$url = get_line($form['items'][$i]['description'], 'URL');
echo '' . $anchortext . '';
}

Faster Rackspace cloud upload

I have used to the Rackspace API to upload files to the RackSpace cloud. But this method seems to be a little on the slow side. Is there a better or faster way to upload a file to the cloud(curl, http adapters, etc)?
I am currently uploading with PHP and using the provided API.
Here is my solution how to make it fast:
I'm uploading only missing files using simple PHP script below. Thanks to it I do it in just one click and in just a few seconds.
PHP source code:
function UploadMissingFilesToRackFileCDN($file_paths_to_upload, $b_force_upload = false)
{
include_once("cloudfiles.php");
// Connect to Rackspace
$username = cloudfile_username; // username
echo "Connecting to CDN..." . date("H:i:s") . "<br>"; ob_flush();
$key = cloudfile_api_key; // api key
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);
echo " Connected!" . date("H:i:s") . "<br>"; ob_flush();
// Get the container we want to use
$container_name = 'vladonai';//'test_container';
echo "Obtaining container $container_name..." . date("H:i:s") . "<br>"; ob_flush();
$container = $conn->get_container($container_name);
echo " The container is obtained." . date("H:i:s") . "<br>"; ob_flush();
if (!$b_force_upload)
{
echo "Receiving container objects list..." . date("H:i:s") . "<br>"; ob_flush();
$existing_object_names = $container->list_objects();
$existing_files_count = count($existing_object_names);
echo " Objects list obtained: $existing_files_count." . date("H:i:s") . "<br>"; ob_flush();
$existing_object_names_text .= "\r\n";
foreach ($existing_object_names as $obj_name)
{
$existing_object_names_text .= $obj_name . "\r\n";
}
}
// upload files to Rackspace
$uploaded_file_n = 0;
$skipped_file_n = 0;
$errors_count = 0;
foreach ($file_paths_to_upload as $localfile_path => $file_info)
{
$filename = basename($localfile_path);
if (!file_exists($localfile_path))
{
echo "<font color=red>Error! File $localfile_path doesn't exists!</font>" . date("H:i:s") . "<br>"; ob_flush();
$errors_count ++;
} else
if (is_dir($localfile_path))
{
//simply skip it
} else
if (strpos($existing_object_names_text, "\r\n" . $filename . "\r\n") !== false)
{
//file is already uploaded to CDN (at least file name is present there). Would be good to have date/size checked, but CDN api has no such feature
//echo "<font color=gray>Skipped file $localfile_path - it already exists!</font><br>"; ob_flush();
$skipped_file_n ++;
} else
{
echo "<font color=green>Uploading file $localfile_path (file #$uploaded_file_n)..." . date("H:i:s") . "</font><br>"; ob_flush();
try
{
$object = $container->create_object($filename);
$object->load_from_filename($localfile_path);
$uploaded_file_n ++;
}
catch (Exception $e)
{
echo "<font color=red>Error! Caught exception: ", $e->getMessage(), " on uploading file <strong>$localfile_path</strong>!</font>" . date("H:i:s") . "<br>"; ob_flush();
$errors_count ++;
}
}
// if ($uploaded_file_n >= 10)
// break;
}
echo "Done! $uploaded_file_n files uploaded. Disconnecting :)" . date("H:i:s") . "<br>"; ob_flush();
echo "Skipped files: $skipped_file_n<br>"; ob_flush();
if ($errors_count > 0)
echo "<font color=red>Erorrs: $errors_count</font><br>"; ob_flush();
}
function UploadChangedImagesToRackFileCDN($b_force_upload = false)
{
$exclude = array
(
'.',
'..',
'*.html',
'*.htm',
'*.php',
'*.csv',
'*.log',
'*.txt',
'*.cfg',
//'*sub/forum/files/*',
);
$files_array_images = get_dirlist("/var/www/html/vladonai.com/images/", '*', $exclude, false);
$files_array = array_merge(get_dirlist("/var/www/html/vladonai.com/js/", '*', $exclude, false), $files_array_images);
UploadMissingFilesToRackFileCDN($files_array, $b_force_upload);
}
function get_dirlist($path, $match = '*', $exclude = array( '.', '..' ), $b_short_path = true)
{
$result = array();
if (($handle = opendir($path)))
{
while (false !== ($fname = readdir($handle)))
{
$skip = false;
if (!empty($exclude))
{
if (!is_array($exclude))
{
$skip = fnmatch($exclude, $fname) || fnmatch($exclude, $path . $fname);
} else
{
foreach ($exclude as $ex)
{
if (fnmatch($ex, $fname) || fnmatch($ex, $path . $fname))
$skip = true;
}
}
}
if (!$skip && (empty($match) || fnmatch($match, $fname)))
{
$file_full_path_and_name = $path . $fname;
//echo "$file_full_path_and_name<br>";
$b_dir = is_dir($file_full_path_and_name);
$b_link = is_link($file_full_path_and_name);
$file_size = ($b_dir || $b_link) ? 0 : filesize($file_full_path_and_name);
$file_mod_time = ($b_dir || $b_link) ? 0 : filemtime($file_full_path_and_name);
$new_result_element = array();
if ($b_short_path)
$file_name = str_replace("/var/www/html/vladonai.com/", "", $file_full_path_and_name);//'[' . str_replace("/var/www/html/vladonai.com/", "", $file_full_path_and_name) . ']';
else
$file_name = $file_full_path_and_name;
$result[$file_name] = array();
$result[$file_name]['size'] = $file_size;
$result[$file_name]['modtime'] = $file_mod_time;
if ($b_dir && !$b_link)
{
//recursively enumerate files in sub-directories
$result = array_merge(get_dirlist($file_full_path_and_name . "/", $match, $exclude, $b_short_path), $result);
}
}
}
closedir($handle);
}
return $result;
}

Categories