Hello,

I am developing a site with ci 2.1.4

I used this tutorial to create an event in google calendar:

When I change a date on the view, it calls the controller function with ajax, used to work fine, now the page just hangs and I can’t figure out what is going on.

My controller function (Calling create_event from the zend_helper):

public function set_paid()
 {
  $this->load->helper('zend');

  some code here....

  $title = $firstName . ' ' . $lastName . ' - order has been paid.';
  $where = 'Clarens';

  $content = "Order Details:\n";
  foreach($dets as $item)
  {
   $qty = $item['qty'];
   $coffee = $item['coffee'];
   $weight = $item['weight'];
   $content .= "Qty: $qty\nCoffee: $coffee\nWeight: $weight\n\n";
  }

  $content .= "Postage type: $postageType\n\n";
  $content .= "\nPostal Address:\n";

  foreach($addr as $item)
  {
   $addrLine1 = $item->addr_line_1;
   $addrLine2 = $item->addr_line_2;
   $suburb = $item->suburb;
   $city = $item->city;
   $postCode = $item->post_code;
   $province = $item->province;

   $content .= "$addrLine1\n";
   if($addrLine2)
    $content .= "$addrLine2\n";
   $content .= "$suburb\n$city $postCode\n$province"; 
  }

  create_event($title, $where, $content);

My zend helper:

<?php
//Changed this today, copied the Zend folder to application/libraries
/*if(ENVIRONMENT != 'production')
{
 ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.str_replace("/", "\\", BASEPATH)."contrib\\");
 require_once ('Zend/Loader.php');
}
else
{
 ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.BASEPATH."/contrib/");
 require_once 'Zend/Loader.php';
}*/
require_once (APPPATH . 'libraries/Zend/Loader.php');

function create_event($title, $where, $content)
{
 Zend_Loader::loadClass('Zend_Gdata_Calendar');
  Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  // Parameters for ClientAuth authentication

 $user = "user";
 $pass = "pass";

 $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
 $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
 $service = new Zend_Gdata_Calendar($client);

 // Create a new entry using the calendar service's magic factory method
 $event= $service->newEventEntry();

 // Populate the event with the desired information
 // Note that each attribute is crated as an instance of a matching class
 $event->title = $service->newTitle($title);
 $event->where = array($service->newWhere($where));
 $event->content = $service->newContent($content);

 // Set the date using RFC 3339 format.
 $date = new DateTime('NOW');
 $duration = new DateInterval('PT15M');

 $calendar_date = $date->format('Y-m-d');
 $date->add($duration);
 $startTime = $date->format('H:i');

 $endDate = $date;
 $endDate->add($duration);
 $endTime = $endDate->format('H:i');

 $tzOffset = "+02";

 $when = $service->newWhen();

 $when->startTime = "{$calendar_date}T{$startTime}:00.000{$tzOffset}:00";
 $when->endTime = "{$calendar_date}T{$endTime}:00.000{$tzOffset}:00";
 $reminder = $service->newReminder();
 $reminder->method = "sms";
 $reminder->minutes = "5";

 // Apply the reminder to an existing event's when property
 $when->reminders = array($reminder);
 $event->when = array($when);

 // Upload the event to the calendar server
 // A copy of the event as it is recorded on the server is returned
 $newEvent = $service->insertEvent($event);
} 

I have the zend installed in two places now:
application/libraries/zend
system/contrib/zend

Do I have to use Zend to connect to google? I searched google but can’t find a solution.

I also found this spark: Click Here but it uses ZF2 and I have no clue what to do with it. Also installing sparks and getting the loader to work with HMVC broke the whole site.

Can someone please help me? I don’t get any errors in firebug, I can’t understand what is going on.

Dani AI

Generated

Nice catch on CSRF. In CI 2.1.x an AJAX POST without the CSRF token is rejected, which can look like the request just hangs. Instead of whitelisting the route, send the token with every AJAX call. One simple pattern is to expose the token name/hash to the page and append them to each POST:

<script>
  var csrfName = '<?php echo $this->security->get_csrf_token_name(); ?>';
  var csrfHash = '<?php echo $this->security->get_csrf_hash(); ?>';

  // example usage
  $.post('/cart/admin/orders/set_paid', $.extend({/* your data */}, (function(d){ d[csrfName]=csrfHash; return d; })()), function(resp){
    // if you keep csrf_regenerate = TRUE, read the refreshed token from the CSRF cookie for subsequent requests
  });
</script>

CI’s guide shows where the token comes from and how forms include it automatically. If you prefer to exclude endpoints, do it sparingly because it reduces protection. See the CSRF section of the CodeIgniter user guide for details. CodeIgniter 2.x CSRF docs. For token helpers and URI whitelisting examples, the CI3 docs are also a helpful reference. CodeIgniter 3 CSRF docs.

On the Google side, your helper uses Zend_Gdata with ClientLogin. Both are legacy: ClientLogin was shut down on April 20, 2015, and the GData-based Calendar API (v1/v2) was turned off on November 17, 2014. Migrate to Calendar API v3 with OAuth 2 using Google’s PHP client library; the v3 docs include a PHP events.insert example with proper RFC3339 times and supported reminders (email/popup; SMS was removed in 2019). ClientLogin shutdown, Calendar v3 migration notice, Events.insert (PHP example), SMS removal announcement. ’ suggestion to use the official PHP client is the right path today. googleapis/google-api-php-client.

Recommended Answers

All 3 Replies

Thanks pritaeas, but I see this library is still in development so rather not use it.

Ok, found the problem, it was the csrf.

I added my function in the config file:
$config['csrf_exclude_uris'] = array('cart/admin/orders/set_paid');

And everything is back to normal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.