1+ <?php
2+ class MInstall extends Core {
3+ // (A) IMPORT SQL FILE
4+ function sql ($ module ) {
5+ $ file = PATH_LIB . "SQL- $ module.sql " ;
6+ if (!file_exists ($ file )) { exit ("$ file not found! " ); }
7+ try {
8+ $ this ->DB ->query (file_get_contents ($ file ));
9+ } catch (Exception $ ex ) {
10+ exit ("Unable to import $ file - " . $ ex ->getMessage ());
11+ }
12+ }
13+
14+ // (B) BACKUP FILE
15+ function backup ($ file ) {
16+ if (!file_exists ($ file )) { exit ("$ file not found! " ); }
17+ $ ext = pathinfo ($ file , PATHINFO_EXTENSION );
18+ $ bak = $ ext == "htaccess " ? "$ file.old " : str_replace (". $ ext " , ".old " , $ file ) ;
19+ if (!copy ($ file , $ bak )) { exit ("Failed to backup $ file " ); }
20+ }
21+
22+ // (C) APPEND TO FILE
23+ function append ($ file , $ add ) {
24+ $ this ->backup ($ file );
25+ $ fh = fopen ($ file , "a " ) or exit ("Cannot open $ file " );
26+ if (fwrite ($ fh , $ add ) === false ) {
27+ fclose ($ fh );
28+ exit ("Failed to write to $ file " );
29+ }
30+ fclose ($ fh );
31+ }
32+
33+ // (D) INSERT INTO FILE
34+ function insert ($ file , $ search , $ add , $ offset =0 ) {
35+ // (D1) BACKUP SPECIFIED FILE
36+ $ this ->backup ($ file );
37+
38+ // (D2) SEEK "LINE TO INSERT AT"
39+ $ lines = file ($ file );
40+ $ at = -1 ;
41+ foreach ($ lines as $ l =>$ line ) {
42+ if (strpos ($ line , $ search ) !== false ) { $ at = $ l + 1 + $ offset ; break ; }
43+ }
44+ if ($ at == -1 ) { exit ("Failed to update $ file " ); }
45+
46+ // (D3) INSERT INTO FILE
47+ array_splice ($ lines , $ at , 0 , $ add );
48+ if (file_put_contents ($ file , implode ("" , $ lines )) == false ) {
49+ exit ("Failed to update $ file " );
50+ }
51+ }
52+
53+ // (E) CONDITIONAL INSERT
54+ function cinsert ($ condition , $ file , $ search , $ add , $ offset =0 ) {
55+ $ insert = true ;
56+ $ stream = fopen ($ file , "r " );
57+ while ($ line = fgets ($ stream )) {
58+ if (strpos ($ line , $ condition ) !== false ) { $ insert = false ; break ; }
59+ }
60+ if ($ insert ) { $ this ->insert ($ file , $ search , $ add , $ offset ); }
61+ }
62+
63+ // (F) CLEAN UP
64+ function clean ($ module ) {
65+ $ file = PATH_PAGES . "PAGE-install- $ module.php " ;
66+ if (!unlink ($ file )) { echo "Failed to delete $ file, please do so manually. " ; }
67+ echo "Installation complete " ;
68+ }
69+ }
0 commit comments