PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/models/init_model.php

https://github.com/ibnoe/Microweber
PHP | 404 lines | 209 code | 75 blank | 120 comment | 60 complexity | 0ac873e00f8ee4a4b8cac35d8b5021de MD5 | raw file
  1. <?php
  2. define ( 'FIRECMS_DB_VERSION', 2 );
  3. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  4. //l setcookie ( string $name [, string $value [, int $expire=0 [, string $path [, string $domain [, bool $secure=false [, bool $httponly=false ]]]]]] )
  5. //setcookie ( 'fckEditor_filesurl', base64_encode ( $media_url ), time () + 36000, '/' );
  6. $media_url = base_url ();
  7. $media_url = $media_url . '/' . USERFILES_DIRNAME . '/media/';
  8. $media_url = reduce_double_slashes ( $media_url );
  9. /*if ($_COOKIE ['fckEditor_filespath'] == false) {
  10. setcookie ( 'fckEditor_filespath', base64_encode ( MEDIAFILES ), time () + 36000, '/' );
  11. }
  12. if ($_COOKIE ['fckEditor_filesurl'] == false) {
  13. setcookie ( 'fckEditor_filesurl', base64_encode ( $media_url ), time () + 36000, '/' );
  14. }
  15. if ($_COOKIE ['site_url'] != SITEURL) {
  16. setcookie ( 'site_url', SITEURL, time () + 36000, '/' );
  17. }
  18. if ($_COOKIE ['root_path'] != ROOTPATH) {
  19. setcookie ( 'root_path', ROOTPATH, time () + 36000, '/' );
  20. }
  21. if ($_COOKIE ['index_path'] != FCPATH) {
  22. setcookie ( 'index_path', FCPATH, time () + 36000, '/' );
  23. }
  24. $tbpath = static_url () . 'js/tiny_mce/plugins/tinybrowser/';
  25. if ($_COOKIE ['tiny_browser_path'] != $tbpath) {
  26. setcookie ( 'tiny_browser_path', $tbpath, time () + 36000, '/' );
  27. }*/
  28. class Init_model extends Model {
  29. function __construct() {
  30. parent::Model ();
  31. //$this->db_setup ();
  32. }
  33. function db_setup($plugin_dir = false) {
  34. if (is_dir ( $plugin_dir ) == true) {
  35. $dir = $plugin_dir . '/db/';
  36. $dir2 = $plugin_dir . '/db/tmp/';
  37. } else {
  38. $dir = APPPATH . '/models/db/';
  39. $dir2 = CACHEDIR_ROOT . '/models/db/tmp/';
  40. }
  41. $dir2 = CACHEDIR_ROOT . '/db_tmp/';
  42. if (is_dir ( $dir ) == false) {
  43. @mkdir_recursive ( $dir );
  44. }
  45. if (is_dir ( $dir2 ) == false) {
  46. @mkdir_recursive ( $dir2 );
  47. }
  48. /*if (is_writable ( $dir ) == false) {
  49. @chmod ( $dir, 0777 );
  50. }*/
  51. /*if (is_writable ( $dir2 ) == false) {
  52. @chmod ( $dir2, 0777 );
  53. }*/
  54. $cache_file = $dir2.'index.php';
  55. if(is_file($cache_file) == true){
  56. return true;
  57. }
  58. if (is_dir ( $dir ) == true) {
  59. touch($cache_file);
  60. $handle = (@opendir ( $dir ));
  61. //require_once ($dir . 'options.php');
  62. while ( false !== ($file = readdir ( $handle )) ) {
  63. if (stristr ( $file, 'disabled' ) == false) {
  64. if (stristr ( $file, 'php' ) == TRUE) {
  65. $checksum = @md5_file ( $dir . $file );
  66. $src_filename = @md5 ( $file );
  67. $inc = false;
  68. $dir_and_file = reduce_double_slashes ( $dir2 . $src_filename );
  69. if (file_exists ( $dir_and_file ) == false) {
  70. $inc = true;
  71. @touch ( $dir_and_file );
  72. $fp = @fopen ( $dir_and_file, 'w' );
  73. @fwrite ( $fp, $checksum );
  74. @fclose ( $fp );
  75. } else {
  76. $content = file_get_contents ( $dir2 . $src_filename );
  77. //var_dump($checksum , $content);
  78. if ($checksum == $content) {
  79. $inc = false;
  80. } else {
  81. $inc = true;
  82. $fp = @fopen ( $dir2 . $src_filename, 'w' );
  83. @fwrite ( $fp, $checksum );
  84. @fclose ( $fp );
  85. }
  86. }
  87. if ($inc == true) {
  88. //print "inc $dir.$file ";
  89. require_once ($dir . $file);
  90. }
  91. }
  92. }
  93. }
  94. @closedir ( $handle );
  95. }
  96. //$result = $db->describeTable(TABLE_PREFIX."options");
  97. /* $q = "select option_value from ".TABLE_PREFIX."options where option_key='firecms_db_version' ";
  98. $query = CI::db()->query($q);
  99. $query = $query->row_array();
  100. $query = (array_values($query));
  101. $curent_db_version = $query[0];
  102. $firecms_db_version = FIRECMS_DB_VERSION;
  103. //var_dump($curent_db_version);
  104. if($firecms_db_version != $curent_db_version){
  105. $q = "select option_value from ".TABLE_PREFIX."options where option_key='firecms_db_version' ";
  106. $query = CI::db()->query($q);
  107. $query = $query->row_array();
  108. $query = (array_values($query));
  109. $curent_db_version = $query[0];
  110. //print $curent_db_version;
  111. if(intval($curent_db_version) == 0){
  112. $data = array(
  113. 'updated_on' => date("Y-m-d h:i:s"),
  114. 'option_key' => 'firecms_db_version',
  115. 'option_value' => $firecms_db_version
  116. );
  117. $this->db->insert(TABLE_PREFIX.'options', $data);
  118. } else {
  119. $data = array(
  120. 'updated_on' => date("Y-m-d h:i:s"),
  121. 'option_value' => $firecms_db_version
  122. );
  123. $this->db->where('option_key', 'firecms_db_version');
  124. $this->db->update(TABLE_PREFIX.'options', $data);
  125. }
  126. }*/
  127. } // end db_setup
  128. /**
  129. * Function set_db_tables
  130. *
  131. * @desc refresh tables in DB
  132. * @access public
  133. * @category Init API
  134. * @subpackage Init
  135. * @author Peter Ivanov
  136. * @link http://ooyes.net
  137. * @param varchar $table_name to alter table
  138. * @param array $fields_to_add to add new column
  139. * @param array $column_for_not_drop for not drop
  140. */
  141. function set_db_tables($table_name, $fields_to_add, $column_for_not_drop = array()) {
  142. //set $column_for_not_drop
  143. //return false;
  144. $dir = APPPATH . '/models/db/';
  145. $dir2 = CACHEDIR_ROOT . '/db_tmp/';
  146. if (is_dir ( $dir ) == false) {
  147. @mkdir ( $dir );
  148. }
  149. if (is_dir ( $dir2 ) == false) {
  150. @mkdir ( $dir2 );
  151. }
  152. if (is_writable ( $dir ) == false) {
  153. @chmod ( $dir, 0777 );
  154. }
  155. if (is_writable ( $dir2 ) == false) {
  156. @chmod ( $dir2, 0777 );
  157. }
  158. $function_cache_id = false;
  159. $args = func_get_args ();
  160. foreach ( $args as $k => $v ) {
  161. $function_cache_id = $function_cache_id . serialize ( $k ) . serialize ( $v );
  162. }
  163. $function_cache_id = __FUNCTION__ . '_' . $table_name . '_' . md5 ( $function_cache_id );
  164. if (is_file ( $dir2 . $function_cache_id ) == false) {
  165. @touch ( $dir2 . $function_cache_id );
  166. //@file_put_contents($dir2 . $function_cache_id , serialize($fields_to_add) );
  167. if ($table_name != 'firecms_sessions') { //Codeigniter System Table
  168. if (empty ( $column_for_not_drop ))
  169. $column_for_not_drop = array ('id' );
  170. $sql = "show columns from $table_name";
  171. $query = CI::db()->query ( $sql );
  172. $columns = $query->result_array ();
  173. $exisiting_fields = array ();
  174. $no_exisiting_fields = array ();
  175. foreach ( $columns as $fivesdraft ) {
  176. $fivesdraft = array_change_key_case ( $fivesdraft, CASE_LOWER );
  177. $exisiting_fields [strtolower ( $fivesdraft ['field'] )] = true;
  178. }
  179. for($i = 0; $i < count ( $columns ); $i ++) {
  180. $column_to_move = true;
  181. for($j = 0; $j < count ( $fields_to_add ); $j ++) {
  182. if (in_array ( $columns [$i] ['Field'], $fields_to_add [$j] )) {
  183. $column_to_move = false;
  184. }
  185. }
  186. $sql = false;
  187. if ($column_to_move) {
  188. if (! empty ( $column_for_not_drop )) {
  189. if (! in_array ( $columns [$i] ['Field'], $column_for_not_drop )) {
  190. $sql = "alter table $table_name drop column {$columns[$i]['Field']} ";
  191. }
  192. } else {
  193. $sql = "alter table $table_name drop column {$columns[$i]['Field']} ";
  194. }
  195. if ($sql)
  196. CI::db()->query ( $sql );
  197. }
  198. }
  199. foreach ( $fields_to_add as $the_field ) {
  200. $the_field [0] = strtolower ( $the_field [0] );
  201. $sql = false;
  202. if ($exisiting_fields [$the_field [0]] != true) {
  203. $sql = "alter table $table_name add column {$the_field[0]} {$the_field[1]} ";
  204. CI::db()->query ( $sql );
  205. } else {
  206. //$sql = "alter table $table_name modify {$the_field[0]} {$the_field[1]} ";
  207. //CI::db()->query ( $sql );
  208. }
  209. }
  210. }
  211. } //set_db_tables
  212. }
  213. /**
  214. * Add new table index if not exists
  215. * @example $this->addIndex('I_messages_parent_id', $table_name, array('parent_id'));
  216. *
  217. * @param unknown_type $aIndexName Index name
  218. * @param unknown_type $aTable Table name
  219. * @param unknown_type $aOnColumns Involved columns
  220. */
  221. public function addIndex($aIndexName, $aTable, $aOnColumns, $indexType = false )
  222. {
  223. $columns = implode(',', $aOnColumns);
  224. $query = CI::db()->query("SHOW INDEX FROM {$aTable} WHERE Key_name = '{$aIndexName}';");
  225. if($indexType != false){
  226. $index = $indexType;
  227. }else {
  228. $index = " INDEX ";
  229. //FULLTEXT
  230. }
  231. if ($query->num_rows() == 0) {
  232. $q = "
  233. ALTER TABLE {$aTable} ADD $index `{$aIndexName}` ({$columns});
  234. ";
  235. //var_dump($q);
  236. CI::db()->query($q);
  237. }
  238. }
  239. /**
  240. * Set table's engine
  241. *
  242. * @param unknown_type $aTable
  243. * @param unknown_type $aEngine
  244. */
  245. public function setEngine($aTable, $aEngine = 'InnoDB')
  246. {
  247. CI::db()->query("ALTER TABLE {$aTable} ENGINE={$aEngine};");
  248. }
  249. /**
  250. * Create foreign key if not exists
  251. *
  252. * @param unknown_type $aFKName Foreign key name
  253. * @param unknown_type $aTable Source table name
  254. * @param unknown_type $aColumns Source columns
  255. * @param unknown_type $aForeignTable Foreign table name
  256. * @param unknown_type $aForeignColumns Foreign columns
  257. * @param unknown_type $aOptions On update and on delete options
  258. */
  259. public function addForeignKey($aFKName, $aTable, $aColumns, $aForeignTable, $aForeignColumns, $aOptions = array())
  260. {
  261. $query = CI::db()->query("
  262. SELECT
  263. *
  264. FROM
  265. INFORMATION_SCHEMA.TABLE_CONSTRAINTS
  266. WHERE
  267. CONSTRAINT_TYPE = 'FOREIGN KEY'
  268. AND
  269. constraint_name = '{$aFKName}'
  270. ;");
  271. if ($query->num_rows() == 0) {
  272. $columns = implode(',', $aColumns);
  273. $fColumns = implode(',', $aForeignColumns);;
  274. $onDelete = 'ON DELETE ' . (isset($aOptions['delete']) ? $aOptions['delete'] : 'NO ACTION');
  275. $onUpdate = 'ON UPDATE ' . (isset($aOptions['update']) ? $aOptions['update'] : 'NO ACTION');
  276. $q = "
  277. ALTER TABLE {$aTable}
  278. ADD CONSTRAINT `{$aFKName}`
  279. FOREIGN KEY
  280. ({$columns})
  281. REFERENCES {$aForeignTable} ($fColumns)
  282. {$onDelete}
  283. {$onUpdate}
  284. ";
  285. CI::db()->query($q);
  286. }
  287. }
  288. }
  289. function parseTextForEmail($text) {
  290. $email = array ();
  291. $invalid_email = array ();
  292. $text = ereg_replace ( "[^A-Za-z._0-9@ ]", " ", $text );
  293. $token = trim ( strtok ( $text, " " ) );
  294. while ( $token !== "" ) {
  295. if (strpos ( $token, "@" ) !== false) {
  296. $token = ereg_replace ( "[^A-Za-z._0-9@]", "", $token );
  297. //checking to see if this is a valid email address
  298. if (is_valid_email ( $email ) !== true) {
  299. $email [] = strtolower ( $token );
  300. } else {
  301. $invalid_email [] = strtolower ( $token );
  302. }
  303. }
  304. $token = trim ( strtok ( " " ) );
  305. }
  306. $email = array_unique ( $email );
  307. $invalid_email = array_unique ( $invalid_email );
  308. return array ("valid_email" => $email, "invalid_email" => $invalid_email );
  309. }
  310. function is_valid_email($email) {
  311. if (eregi ( "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$", $email ))
  312. return true;
  313. else
  314. return false;
  315. }
  316. ?>