Difference between revisions of "Developers - Porting modules from osC"

From Zen Cart(tm) Wiki
Jump to: navigation, search
(Initial text on converting osC code to Zen code.)
 
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
{{Template:menuBarDevelopers}}
 
= Overview =
 
= Overview =
  
Many contributions for osCommerce have been ported to Zen Cart. There are a few general rules for converting osCommerce code to Zen Cart code.
+
Many contributions for osCommerce have been ported to Zen Cart™. There are a few general rules for converting osCommerce code to Zen Cart™ v1.2.x or v1.3.x code.
 +
If you are attempting to convert to v1.5.x code, you will need to follow the additional instructions for converting addons to the v1.5 format.
 +
Writing for v2.x.x requires a complete rewrite of the addon using the much simpler new OOP architecture in the v2 framework.  
  
  
Line 77: Line 80:
  
 
<pre>$db->Insert_ID()</pre>
 
<pre>$db->Insert_ID()</pre>
 +
 +
 +
== Escaping or sanitizing data ==
 +
 +
Don't use direct calls to mysql_xxxxx() functions. Instead, use the Zen Cart database functions like BindVars() and prepare_input(). See [[Developers_-_Database]] for details.
  
  
Line 96: Line 104:
 
by
 
by
  
<pre>zen_session_id</pre>
+
<pre>zen_session_id()</pre>
 +
 
 +
 
 +
== session_register ==
 +
 
 +
The use of tep_session_register() function calls is entirely deprecated. PHP 5.3 and 5.4 no longer support session_register() and session_unregister() and session_is_registered() function calls, and will trigger fatal errors if used.
 +
 
 +
Zen Cart&trade; does not use registered globals (there is no <em style="font-family:Courier;font-style:normal;font-weight:600;">zen_session_is_registered</em>, etc). Instead, Zen Cart uses the <em style="font-family:Courier;font-style:normal;font-weight:600;">$_SESSION</em> superglobal.
  
  
 
== Registered globals ==
 
== Registered globals ==
 
Zen Cart does not use registered globals (there is no <em style="font-family:Courier;font-style:normal;font-weight:600;">zen_session_is_registered</em>, etc). Instead, Zen Cart uses the <em style="font-family:Courier;font-style:normal;font-weight:600;">$_SESSION</em> superglobal.
 
  
 
References to registered globals <em style="font-family:Courier;font-style:normal;font-weight:600;">$var</em> should be replaced by <em style="font-family:Courier;font-style:normal;font-weight:600;">$_SESSION['var']</em>.
 
References to registered globals <em style="font-family:Courier;font-style:normal;font-weight:600;">$var</em> should be replaced by <em style="font-family:Courier;font-style:normal;font-weight:600;">$_SESSION['var']</em>.
Line 107: Line 120:
  
 
== $HTTP_x_VARS ==
 
== $HTTP_x_VARS ==
 +
 +
These variables are a result of the deprecated register_long_arrays setting in VERY old versions of PHP. As of PHP 5.3 they are deprecated, and as of PHP 5.4 they will trigger a fatal error. Thus they must be converted. A simple search-and-replace is usually sufficient.
  
 
Replace  
 
Replace  
Line 117: Line 132:
  
 
Also, remove any <em style="font-family:Courier;font-style:normal;font-weight:600;">global $HTTP_x_VARS;</em> lines inside functions; the new versions are superglobals and do not need to be declared global.
 
Also, remove any <em style="font-family:Courier;font-style:normal;font-weight:600;">global $HTTP_x_VARS;</em> lines inside functions; the new versions are superglobals and do not need to be declared global.
 +
 +
 +
Article with references to PHP 5.4 deprecation: http://mcglockenshire.com/blog/posts/waiting-for-php-5-4-death-to-prehistoric-cruft.html

Latest revision as of 15:43, 10 October 2014

Overview

Many contributions for osCommerce have been ported to Zen Cart™. There are a few general rules for converting osCommerce code to Zen Cart™ v1.2.x or v1.3.x code. If you are attempting to convert to v1.5.x code, you will need to follow the additional instructions for converting addons to the v1.5 format. Writing for v2.x.x requires a complete rewrite of the addon using the much simpler new OOP architecture in the v2 framework.


tep_db_query and tep_db_fetch_array

Replace code like

$my_var_query = tep_db_query("query stuff");
$my_var = tep_db_fetch_array($my_var_query);

by

$my_var = $db->Execute("query stuff");


tep_db_query and tep_db_fetch_array in if

Replace code like

$my_var_query = tep_db_query("query stuff");
if ($my_var = tep_db_fetch_array($my_var_query))  {

by

$my_var = $db->Execute("query stuff");
if (!$my_var->EOF)  {


tep_db_query and tep_db_fetch_array in while

Replace code like

$my_var_query = tep_db_query("query stuff");
while ($my_var = tep_db_fetch_array($my_var_query)) {
do_stuff;
}

by

$my_var = $db->Execute("query stuff");
while (!$my_var->EOF) {
do_stuff;
$my_var->MoveNext();
}


Referencing query fields

In situations like the following:

$my_var_query = tep_db_query("query stuff");
$my_var = tep_db_fetch_array($my_var_query);
$i = $my_var['db_key'];

replace the line

$i = $my_var['db_key'];

by

$i = $my_var->fields['db_key'];


tep_db_num_rows

Replace code like

tep_db_num_rows($my_var)

by

$my_var->RecordCount()


tep_db_insert_id

Replace code like

tep_db_insert_id()

by

$db->Insert_ID()


Escaping or sanitizing data

Don't use direct calls to mysql_xxxxx() functions. Instead, use the Zen Cart database functions like BindVars() and prepare_input(). See Developers_-_Database for details.


Add global $db

Add global $db; to all functions that now reference $db as a result of these changes.


Remaining tep_ functions

Replace tep_ prefix in remaining functions with zen_.


$osCsid

Replace references to

$osCsid

by

zen_session_id()


session_register

The use of tep_session_register() function calls is entirely deprecated. PHP 5.3 and 5.4 no longer support session_register() and session_unregister() and session_is_registered() function calls, and will trigger fatal errors if used.

Zen Cart™ does not use registered globals (there is no zen_session_is_registered, etc). Instead, Zen Cart uses the $_SESSION superglobal.


Registered globals

References to registered globals $var should be replaced by $_SESSION['var'].


$HTTP_x_VARS

These variables are a result of the deprecated register_long_arrays setting in VERY old versions of PHP. As of PHP 5.3 they are deprecated, and as of PHP 5.4 they will trigger a fatal error. Thus they must be converted. A simple search-and-replace is usually sufficient.

Replace

$HTTP_x_VARS

by

$_x

For example: $HTTP_POST_VARS becomes $_POST.

Also, remove any global $HTTP_x_VARS; lines inside functions; the new versions are superglobals and do not need to be declared global.


Article with references to PHP 5.4 deprecation: http://mcglockenshire.com/blog/posts/waiting-for-php-5-4-death-to-prehistoric-cruft.html