PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/includes/functions/extra_functions/easypopulate_functions.php

https://bitbucket.org/pickupman/zencart-easy-populate
PHP | 412 lines | 321 code | 28 blank | 63 comment | 29 complexity | 2d5cb4193e3ad7ea95fc550c703cabee MD5 | raw file
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // |zen-cart Open Source E-commerce |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2004 The zen-cart developers |
  7. // | |
  8. // | http://www.zen-cart.com/index.php |
  9. // | |
  10. // | Portions Copyright (c) 2003 osCommerce |
  11. // +----------------------------------------------------------------------+
  12. // | This source file is subject to version 2.0 of the GPL license, |
  13. // | that is bundled with this package in the file LICENSE, and is |
  14. // | available through the world-wide-web at the following url: |
  15. // | http://www.zen-cart.com/license/2_0.txt. |
  16. // | If you did not receive a copy of the zen-cart license and are unable |
  17. // | to obtain it through the world-wide-web, please send a note to |
  18. // | license@zen-cart.com so we can mail you a copy immediately. |
  19. // +----------------------------------------------------------------------+
  20. // $Id:easypopulate_functions.php,v1.2.5.4 2005/09/26 langer $
  21. //
  22. function ep_get_uploaded_file($filename) {
  23. if (isset($_FILES[$filename])) {
  24. //global $_FILES;
  25. $uploaded_file = array('name' => $_FILES[$filename]['name'],
  26. 'type' => $_FILES[$filename]['type'],
  27. 'size' => $_FILES[$filename]['size'],
  28. 'tmp_name' => $_FILES[$filename]['tmp_name']);
  29. } elseif (isset($_POST[$filename])) {
  30. $uploaded_file = array('name' => $_POST[$filename],
  31. );
  32. } elseif (isset($GLOBALS['HTTP_POST_FILES'][$filename])) {
  33. global $HTTP_POST_FILES;
  34. $uploaded_file = array('name' => $HTTP_POST_FILES[$filename]['name'],
  35. 'type' => $HTTP_POST_FILES[$filename]['type'],
  36. 'size' => $HTTP_POST_FILES[$filename]['size'],
  37. 'tmp_name' => $HTTP_POST_FILES[$filename]['tmp_name']);
  38. } elseif (isset($GLOBALS['HTTP_POST_VARS'][$filename])) {
  39. global $HTTP_POST_VARS;
  40. $uploaded_file = array('name' => $HTTP_POST_VARS[$filename],
  41. );
  42. } else {
  43. $uploaded_file = array('name' => $GLOBALS[$filename . '_name'],
  44. 'type' => $GLOBALS[$filename . '_type'],
  45. 'size' => $GLOBALS[$filename . '_size'],
  46. 'tmp_name' => $GLOBALS[$filename]);
  47. }
  48. return $uploaded_file;
  49. }
  50. // the $filename parameter is an array with the following elements:
  51. // name, type, size, tmp_name
  52. function ep_copy_uploaded_file($filename, $target) {
  53. if (substr($target, -1) != '/') $target .= '/';
  54. $target .= $filename['name'];
  55. move_uploaded_file($filename['tmp_name'], $target);
  56. }
  57. function ep_get_tax_class_rate($tax_class_id) {
  58. $tax_multiplier = 0;
  59. $tax_query = mysql_query("select SUM(tax_rate) as tax_rate from " . TABLE_TAX_RATES . " WHERE tax_class_id = '" . zen_db_input($tax_class_id) . "' GROUP BY tax_priority");
  60. if (mysql_num_rows($tax_query)) {
  61. while ($tax = mysql_fetch_array($tax_query)) {
  62. $tax_multiplier += $tax['tax_rate'];
  63. }
  64. }
  65. return $tax_multiplier;
  66. }
  67. function ep_get_tax_title_class_id($tax_class_title) {
  68. $classes_query = mysql_query("select tax_class_id from " . TABLE_TAX_CLASS . " WHERE tax_class_title = '" . zen_db_input($tax_class_title) . "'" );
  69. $tax_class_array = mysql_fetch_array($classes_query);
  70. $tax_class_id = $tax_class_array['tax_class_id'];
  71. return $tax_class_id ;
  72. }
  73. function print_el($item2) {
  74. //$output_display = " | " . substr(strip_tags($item2), 0, 10);
  75. $output_display = substr(strip_tags($item2), 0, 10) . " | ";
  76. return $output_display;
  77. }
  78. function print_el1($item2) {
  79. $output_display = sprintf("| %'.4s ", substr(strip_tags($item2), 0, 80));
  80. return $output_display;
  81. }
  82. function smart_tags($string,$tags,$crsub,$doit) {
  83. if ($doit == true) {
  84. foreach ($tags as $tag => $new) {
  85. $tag = '/('.$tag.')/';
  86. $string = preg_replace($tag,$new,$string);
  87. }
  88. }
  89. // we remove problem characters here anyway as they are not wanted..
  90. $string = preg_replace("/(\r\n|\n|\r)/", "", $string);
  91. // $crsub is redundant - may add it again later though..
  92. return $string;
  93. }
  94. function ep_field_name_exists($tbl,$fld) {
  95. if (zen_not_null(zen_field_type($tbl,$fld))) {
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101. function ep_remove_product($product_model) {
  102. global $db, $ep_debug_logging, $ep_debug_logging_all, $ep_stack_sql_error;
  103. $sql = "select products_id
  104. from " . TABLE_PRODUCTS . "
  105. where products_model = '" . zen_db_input($product_model) . "'";
  106. $products = $db->Execute($sql);
  107. if (mysql_errno()) {
  108. $ep_stack_sql_error = true;
  109. if ($ep_debug_logging == true) {
  110. // langer - will add time & date..
  111. $string = "MySQL error ".mysql_errno().": ".mysql_error()."\nWhen executing:\n$sql\n";
  112. write_debug_log($string);
  113. }
  114. } elseif ($ep_debug_logging_all == true) {
  115. $string = "MySQL PASSED\nWhen executing:\n$sql\n";
  116. write_debug_log($string);
  117. }
  118. while (!$products->EOF) {
  119. zen_remove_product($products->fields['products_id']);
  120. $products->MoveNext();
  121. }
  122. return;
  123. }
  124. function ep_purge_dross() {
  125. $dross = ep_get_dross();
  126. foreach ($dross as $products_id => $langer) {
  127. zen_remove_product($products_id);
  128. }
  129. }
  130. function ep_get_dross() {
  131. global $db;
  132. $target_tables = array(TABLE_PRODUCTS_DESCRIPTION,
  133. TABLE_SPECIALS,
  134. TABLE_PRODUCTS_TO_CATEGORIES,
  135. TABLE_PRODUCTS_ATTRIBUTES,
  136. TABLE_FEATURED,
  137. TABLE_CUSTOMERS_BASKET,
  138. TABLE_CUSTOMERS_BASKET_ATTRIBUTES,
  139. TABLE_PRODUCTS_DISCOUNT_QUANTITY);
  140. // can add others I guess, though this probably catches all possible data debris...
  141. // reviews uses reviews_id, but if it is in reviews, it is probably detected above anyway
  142. // This array needs to work with all versions - could break EP on older versions I think.. with each additional table, test on older versions
  143. $dross = array();
  144. foreach ($target_tables as $table) {
  145. //lets check the tables for deleted products
  146. $sql = "select distinct t.products_id from " . $table . " as t left join " . TABLE_PRODUCTS . " as p on t.products_id = p.products_id where p.products_id is NULL";
  147. $products = $db->Execute($sql);
  148. while (!$products->EOF) {
  149. $dross[$products->fields['products_id']] = 'dross';
  150. $products->MoveNext();
  151. }
  152. }
  153. // our array has product_id => "dross", so duplicate products simply over-write same in array
  154. //print_r($dross);
  155. return $dross;
  156. }
  157. function ep_update_cat_ids() {
  158. // reset products master categories ID
  159. global $db;
  160. $sql = "select products_id from " . TABLE_PRODUCTS;
  161. $check_products = $db->Execute($sql);
  162. while (!$check_products->EOF) {
  163. $sql = "select products_id, categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id='" . $check_products->fields['products_id'] . "'";
  164. $check_category = $db->Execute($sql);
  165. $sql = "update " . TABLE_PRODUCTS . " set master_categories_id='" . $check_category->fields['categories_id'] . "' where products_id='" . $check_products->fields['products_id'] . "'";
  166. $update_viewed = $db->Execute($sql);
  167. $check_products->MoveNext();
  168. }
  169. }
  170. function ep_update_prices() {
  171. global $db;
  172. // reset products_price_sorter for searches etc.
  173. $sql = "select products_id from " . TABLE_PRODUCTS . " where products_price != products_price_sorter";
  174. $update_prices = $db->Execute($sql);
  175. while (!$update_prices->EOF) {
  176. zen_update_products_price_sorter($update_prices->fields['products_id']);
  177. $update_prices->MoveNext();
  178. }
  179. }
  180. function ep_update_attributes_sort_order() {
  181. global $db;
  182. $all_products_attributes= $db->Execute("select p.products_id, pa.products_attributes_id from " .
  183. TABLE_PRODUCTS . " p, " .
  184. TABLE_PRODUCTS_ATTRIBUTES . " pa " . "
  185. where p.products_id= pa.products_id"
  186. );
  187. while (!$all_products_attributes->EOF) {
  188. $count++;
  189. //$product_id_updated .= ' - ' . $all_products_attributes->fields['products_id'] . ':' . $all_products_attributes->fields['products_attributes_id'];
  190. zen_update_attributes_products_option_values_sort_order($all_products_attributes->fields['products_id']);
  191. $all_products_attributes->MoveNext();
  192. }
  193. }
  194. function ep_datoriser($date_time) {
  195. global $ep_date_format; // d-m-y etc..
  196. global $ep_raw_time; // user's prefered time (eg for specials to start) if no time in upload
  197. $raw_date_exist = preg_match("/^([0-2]0[0-9]{2}-[0-1][0-9]-[0-3][0-9])( [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$)?/", $date_time);
  198. if (!$raw_date_exist) {
  199. // not raw... we can only assume it is an excel date..
  200. // separate dates from times
  201. $exist_time = preg_match("/^2?0?[0-9]?[0-9][\.\/-][0-3]?[0-9][\.\/-]2?0?[0-9]?[0-9] ([0-2]?[0-9]:[0-5][0-9]).*$/", $date_time, $excel_time); // no seconds..
  202. $exist_date = preg_match("/^(2?0?[0-9]?[0-9][\.\/-][0-3]?[0-9][\.\/-]2?0?[0-9]?[0-9]).*$/", $date_time, $excel_date);
  203. //echo $excel_time[1] . '<br >';
  204. //echo $excel_date[1] . '<br ><br />';
  205. // if (!zen_not_null($exist_date)) // we fail to get a date! error msg rqd, and/or substitute action??
  206. // check for which of 3 possible date separators we have..
  207. // this sucks, I know... but it works for now
  208. if (zen_not_null(strpos($excel_date[1], '-'))) $separator = '-';
  209. if (zen_not_null(strpos($excel_date[1], '.'))) $separator = '.';
  210. if (zen_not_null(strpos($excel_date[1], '/'))) $separator = '/';
  211. //echo 'separator is: ' . $separator . '<br />';
  212. $format_bits = explode('-', $ep_date_format);
  213. $date_bits = explode($separator, $excel_date[1]);
  214. foreach ($format_bits as $key => $bit) {
  215. $$bit = $date_bits[$key]; // $y = 05 or 2005, $m = 09 or 9, $d = 03 or 3 for eg. Can only work if d,m,y order from excel is same as config
  216. $$bit = strlen($$bit) < 2 ? '0' . $$bit : $$bit; // 4 is now 04 for eg. - expand this as a rudimentary check - should never occur on $y var
  217. $$bit = strlen($$bit) > 2 ? substr($$bit,-2, 2) : $$bit; // 2005 is now 05 - expand this as a rudimentary check - should only occur on $y var
  218. //echo $$bit . '<br />';
  219. // another rudimentary check could be for $m vals > 12 = error too!
  220. }
  221. // create default raw time... if user left space off, put it on..
  222. if (substr($ep_raw_time,0, 1) != ' ') $ep_raw_time = ' ' . $ep_raw_time;
  223. // is it really a raw time? if not, make it midnight..
  224. $exist_raw_time = preg_match("/ [0-2][0-9]:[0-5][0-9]:[0-5][0-9]/", $ep_raw_time); // true if is raw time
  225. $ep_raw_time = zen_not_null($exist_raw_time) ? $ep_raw_time : ' 00:00:00';
  226. // if time supplied from excel, use it instead..
  227. $ep_raw_time = zen_not_null($exist_time) ? ' ' . $excel_time[1] : $ep_raw_time;
  228. //echo '<br />'.$ep_raw_time . '<br />';
  229. $raw_date = '20' . $y . '-' . $m . '-' . $d . $ep_raw_time; // needs updating at the end of the century ;-)
  230. //echo $raw_date . '<br /><br />';
  231. } else {
  232. // the date is raw, so return it
  233. $raw_date = $date_time;
  234. //echo $date . ' is raw...<br />';
  235. }
  236. return $raw_date;
  237. }
  238. function write_debug_log($string) {
  239. global $ep_debug_log_path;
  240. $logFile = $ep_debug_log_path . 'ep_debug_log.txt';
  241. $fp = fopen($logFile,'ab');
  242. fwrite($fp, $string);
  243. fclose($fp);
  244. return;
  245. }
  246. function ep_query($query) {
  247. global $ep_debug_logging, $ep_debug_logging_all, $ep_stack_sql_error;
  248. $result = mysql_query($query);
  249. if (mysql_errno()) {
  250. $ep_stack_sql_error = true;
  251. if ($ep_debug_logging == true) {
  252. // langer - will add time & date..
  253. $string = "MySQL error ".mysql_errno().": ".mysql_error()."\nWhen executing:\n$query\n";
  254. write_debug_log($string);
  255. }
  256. } elseif ($ep_debug_logging_all == true) {
  257. $string = "MySQL PASSED\nWhen executing:\n$query\n";
  258. write_debug_log($string);
  259. }
  260. return $result;
  261. }
  262. function install_easypopulate() {
  263. global $db;
  264. $db->Execute("INSERT INTO " . TABLE_CONFIGURATION_GROUP . " VALUES ('', 'Easy Populate', 'Config options for Easy Populate', '1', '1')");
  265. $group_id = mysql_insert_id();
  266. $db->Execute("UPDATE " . TABLE_CONFIGURATION_GROUP . " SET sort_order = " . $group_id . " WHERE configuration_group_id = " . $group_id);
  267. $db->Execute("INSERT INTO " . TABLE_CONFIGURATION . " VALUES
  268. ('', 'Uploads Directory', 'EASYPOPULATE_CONFIG_TEMP_DIR', 'tempEP/', 'Name of directory for your uploads (default: tempEP/).', " . $group_id . ", '0', NULL, now(), NULL, NULL),
  269. ('', 'Upload File Date Format', 'EASYPOPULATE_CONFIG_FILE_DATE_FORMAT', 'm-d-y', 'Choose order of date values that corresponds to your uploads file, usually generated by MS Excel. Raw dates in your uploads file (Eg 2005-09-26 09:00:00) are not affected, and will upload as they are.', " . $group_id . ", '1', NULL, now(), NULL, 'zen_cfg_select_option(array(\"m-d-y\", \"d-m-y\", \"y-m-d\"),'),
  270. ('', 'Default Raw Time', 'EASYPOPULATE_CONFIG_DEFAULT_RAW_TIME', '09:00:00', 'If no time value stipulated in upload file, use this value. Useful for ensuring specials begin after a specific time of the day (default: 09:00:00)', " . $group_id . ", '2', NULL, now(), NULL, NULL),
  271. ('', 'Split File On # Records', 'EASYPOPULATE_CONFIG_SPLIT_MAX', '300', 'Default number of records for split-file uploads. Used to avoid timeouts on large uploads (default: 300).', " . $group_id . ", '3', NULL, now(), NULL, NULL),
  272. ('', 'Maximum Category Depth', 'EASYPOPULATE_CONFIG_MAX_CATEGORY_LEVELS', '7', 'Maximum depth of categories required for your store. Is the number of category columns in downloaded file (default: 7).', " . $group_id . ", '4', NULL, now(), NULL, NULL),
  273. ('', 'Upload/Download Prices Include Tax', 'EASYPOPULATE_CONFIG_PRICE_INC_TAX', 'false', 'Choose to include or exclude tax, depending on how you manage prices outside of Zen Cart.', " . $group_id . ", '5', NULL, now(), NULL, 'zen_cfg_select_option(array(\"true\", \"false\"),'),
  274. ('', 'Make Zero Qty Products Inactive', 'EASYPOPULATE_CONFIG_ZERO_QTY_INACTIVE', 'false', 'When uploading, make the status Inactive for products with zero qty (default: false).', " . $group_id . ", '6', NULL, now(), NULL, 'zen_cfg_select_option(array(\"true\", \"false\"),'),
  275. ('', 'Smart Tags Replacement of Newlines', 'EASYPOPULATE_CONFIG_SMART_TAGS', 'true', 'Allows your description fields in your uploads file to have carriage returns and/or new-lines converted to HTML line-breaks on uploading, thus preserving some rudimentary formatting (default: true).', " . $group_id . ", '7', NULL, now(), NULL, 'zen_cfg_select_option(array(\"true\", \"false\"),'),
  276. ('', 'Advanced Smart Tags', 'EASYPOPULATE_CONFIG_ADV_SMART_TAGS', 'false', 'Allow the use of complex regular expressions to format descriptions, making headings bold, add bullets, etc. Configuration is in ADMIN/easypopulate.php (default: false).', " . $group_id . ", '8', NULL, now(), NULL, 'zen_cfg_select_option(array(\"true\", \"false\"),'),
  277. ('', 'Debug Logging', 'EASYPOPULATE_CONFIG_DEBUG_LOGGING', 'true', 'Allow Easy Populate to generate an error log on errors only (default: true)', " . $group_id . ", '9', NULL, now(), NULL, 'zen_cfg_select_option(array(\"true\", \"false\"),'),
  278. ('', 'Custom Products Fields', 'EASYPOPULATE_CONFIG_CUSTOM_FIELDS', '', 'Enter a comma seperated list of fields to be automatically added to import/export file(ie: products_length, products_width). Please make sure field exists in PRODUCTS table.', " . $group_id . ", '10', NULL, now(), NULL, NULL)
  279. ");
  280. //Check if we are using Version > 1.5 to install admin profiles
  281. $version_check = $db->Execute("SELECT project_version_minor FROM ".TABLE_PROJECT_VERSION . " WHERE project_version_key='Zen-Cart Main'", 1);
  282. if ($version_check->RecordCount() > 0) {
  283. $version = substr($version_check->fields['project_version_minor'], 0, 3);
  284. if ($version == '5.0') {
  285. //We are on version 1.5.x check admin profiles admin profiles
  286. //Get last sort order id
  287. $last_id = $db->Execute("SELECT sort_order FROM " . TABLE_ADMIN_PAGES . " WHERE menu_key='tools' ORDER BY sort_order DESC", 1);
  288. $db->Execute("INSERT INTO " . TABLE_ADMIN_PAGES . " (page_key, language_key, main_page, menu_key, display_on_menu, sort_order, page_params) VALUES ('toolsEasyPopulate', 'BOX_TOOLS_EASYPOPULATE', 'FILENAME_EASYPOPULATE', 'tools', 'Y', '" . ((int)$last_id->fields['sort_order'] + 1) ."', '')");
  289. $db->Execute("INSERT INTO " . TABLE_ADMIN_PAGES . " (page_key, language_key, main_page, menu_key, display_on_menu, sort_order, page_params) VALUES ('configEasyPopulate', 'BOX_TOOLS_EASYPOPULATE', 'FILENAME_CONFIGURATION', 'configuration', 'Y' , '" . (int)$group_id . "', 'gID=" . (int)$group_id . "'" );
  290. }
  291. }
  292. }
  293. function remove_easypopulate() {
  294. global $db, $ep_keys;
  295. $sql = "SELECT
  296. configuration_group_id
  297. FROM
  298. " . TABLE_CONFIGURATION_GROUP . "
  299. WHERE
  300. configuration_group_title = 'Easy Populate'";
  301. $result = ep_query($sql);
  302. if (mysql_num_rows($result)) {
  303. // we have at least 1 EP group - let's delete it
  304. $ep_groups = mysql_fetch_array($result);
  305. foreach ($ep_groups as $ep_group) {
  306. $db->Execute("delete from " . TABLE_CONFIGURATION_GROUP . "
  307. where configuration_group_id = '" . (int)$ep_group . "'");
  308. }
  309. }
  310. // now delete any EP keys found in config
  311. foreach ($ep_keys as $ep_key) {
  312. @$db->Execute("delete from " . TABLE_CONFIGURATION . "
  313. where configuration_key = '" . $ep_key . "'");
  314. }
  315. }
  316. function ep_chmod_check($tempdir) {
  317. global $messageStack;
  318. if (!@file_exists(DIR_FS_CATALOG . $tempdir . ".")) {
  319. // directory does not exist, or may be unwritable
  320. @chmod(DIR_FS_CATALOG . $tempdir, 0700); // attempt to make writable - supress error as dir may not exist..
  321. if (!@file_exists(DIR_FS_CATALOG . $tempdir . ".")) {
  322. // still can't see it... let's try chmod 777
  323. @chmod(DIR_FS_CATALOG . $tempdir, 0777); // attempt to make chmod 777 - supress error as dir may not exist..
  324. if (!@file_exists(DIR_FS_CATALOG . $tempdir . ".")) {
  325. // still can't see it, so it is probably not there, or is windows server..
  326. $messageStack->add(sprintf(EASYPOPULATE_MSGSTACK_TEMP_FOLDER_MISSING, $tempdir, DIR_FS_CATALOG), 'warning');
  327. $chmod_check = false;
  328. } else {
  329. // succeeded only with chmod 777 - add msg to ensure index.html is included to prevent file browsing
  330. $messageStack->add(EASYPOPULATE_MSGSTACK_TEMP_FOLDER_PERMISSIONS_SUCCESS_777, 'success');
  331. $chmod_check = true;
  332. }
  333. } else {
  334. // we successfully changed to writable @ chmod 700
  335. $messageStack->add(EASYPOPULATE_MSGSTACK_TEMP_FOLDER_PERMISSIONS_SUCCESS, 'success');
  336. $chmod_check = true;
  337. }
  338. } else {
  339. $chmod_check = true;
  340. }
  341. return $chmod_check;
  342. }
  343. /**
  344. * The following functions are for testing purposes only
  345. */
  346. // available zen functions of use..
  347. /*
  348. function zen_get_category_name($category_id, $language_id)
  349. function zen_get_category_description($category_id, $language_id)
  350. function zen_get_products_name($product_id, $language_id = 0)
  351. function zen_get_products_description($product_id, $language_id)
  352. function zen_get_products_model($products_id)
  353. */
  354. function register_globals_vars_check () {
  355. echo phpversion();
  356. echo '<br>register_globals = ', ini_get('register_globals'), '<br>';
  357. print "_GET: "; print_r($_GET); echo '<br />';
  358. print "_POST: "; print_r($_POST); echo '<br />';
  359. print "_FILES: "; print_r($_FILES); echo '<br />';
  360. print "_COOKIE: "; print_r($_COOKIE); echo '<br />';
  361. print "GLOBALS: "; print_r($GLOBALS); echo '<br />';
  362. print "_REQUEST: "; print_r($_REQUEST); echo '<br /><br />';
  363. global $HTTP_POST_FILES;
  364. print "HTTP_POST_FILES: "; print_r($HTTP_POST_FILES); echo '<br />';
  365. }