1+ <?php
2+
3+ /**
4+ * Insert new lines in "includes/application_top.php" file
5+ */
6+ $ applicationTop = 'includes/application_top.php ' ;
7+ $ applicationTopMatch = 'src= "includes/javascript/paylike.js" ' ;
8+ $ applicationTopFind = '' ;
9+ $ applicationTopInsert = '
10+ if ( basename( $PHP_SELF ) == \'checkout_confirmation.php \' ) {
11+ echo \'<script src="https://sdk.paylike.io/10.js"></script> \';
12+ echo \'<script src= "includes/javascript/paylike.js"></script> \';
13+ }
14+ ' ;
15+
16+ modifyFileContents ($ applicationTop , $ applicationTopMatch , $ applicationTopFind , $ applicationTopInsert , $ endOfFile = true );
17+
18+ /**
19+ * Insert new lines at the end in "includes/.htaccess" file
20+ */
21+ $ htaccess = 'includes/.htaccess ' ;
22+ $ htaccessMatch = 'FilesMatch "paylike.php" ' ;
23+ $ htaccessFind = '' ;
24+ $ htaccessInsert = '
25+ <FilesMatch "paylike.php">
26+ Require all granted
27+ </FilesMatch>
28+ ' ;
29+
30+ modifyFileContents ($ htaccess , $ htaccessMatch , $ htaccessFind , $ htaccessInsert , $ endOfFile = true );
31+
32+ /**
33+ * Insert (I) new lines in "admin/modules.php" file
34+ */
35+ $ modules_1 = 'admin/modules.php ' ;
36+ $ modules_1_Match = 'payment/paylike/errors.php ' ;
37+ $ modules_1_Find = 'require \'includes/application_top.php \'; ' ;
38+ $ modules_1_Insert = '
39+ require_once( \'includes/modules/payment/paylike/errors.php \');
40+ require \'includes/application_top.php \';
41+ ' ;
42+
43+ modifyFileContents ($ modules_1 , $ modules_1_Match , $ modules_1_Find , $ modules_1_Insert );
44+
45+ /**
46+ * Insert (II) new lines in "admin/modules.php" file
47+ */
48+ $ modules_2 = 'admin/modules.php ' ;
49+ $ modules_2_Match = 'payment/paylike/validate.php ' ;
50+ $ modules_2_Find = 'case \'save \': ' ;
51+ $ modules_2_Insert = '
52+ case \'save \':
53+ require_once( \'includes/modules/payment/paylike/validate.php \');
54+ ' ;
55+
56+ modifyFileContents ($ modules_2 , $ modules_2_Match , $ modules_2_Find , $ modules_2_Insert );
57+
58+ /**
59+ * Insert (III) new lines in "admin/modules.php" file
60+ */
61+ $ modules_3 = 'admin/modules.php ' ;
62+ $ modules_3_Match = 'if(sizeof($errors) === 0 || array_search($key, $validation_keys) === FALSE) ' ;
63+ $ modules_3_Find = 'tep_db_query("UPDATE configuration SET configuration_value = \'" . tep_db_input($value) . " \' WHERE configuration_key = \'" . tep_db_input($key) . " \'"); ' ;
64+ $ modules_3_Insert = '
65+ if(sizeof($errors) === 0 || array_search($key, $validation_keys) === FALSE){
66+ tep_db_query("UPDATE configuration SET configuration_value = \'" . tep_db_input($value) . " \' WHERE configuration_key = \'" . tep_db_input($key) . " \'");
67+ }
68+ ' ;
69+
70+ modifyFileContents ($ modules_3 , $ modules_3_Match , $ modules_3_Find , $ modules_3_Insert );
71+
72+ /**
73+ * Insert (IV) new lines in "admin/modules.php" file
74+ */
75+ $ modules_4 = 'admin/modules.php ' ;
76+ $ modules_4_Match = 'if(sizeof($errors)){ ' ;
77+ $ modules_4_Find = 'tep_redirect(tep_href_link( \'modules.php \', \'set= \' . $set . \'&module= \' . $_GET[ \'module \'])); ' ;
78+ $ modules_4_Insert = '
79+ if(sizeof($errors)){
80+ tep_redirect(tep_href_link( \'modules.php \', \'set= \' . $set . \'&module= \' . $_GET[ \'module \'] . \'&action=edit \'));
81+ }
82+ tep_redirect(tep_href_link( \'modules.php \', \'set= \' . $set . \'&module= \' . $_GET[ \'module \']));
83+ ' ;
84+
85+ modifyFileContents ($ modules_4 , $ modules_4_Match , $ modules_4_Find , $ modules_4_Insert );
86+
87+ /**
88+ * Insert (V) new lines in "admin/modules.php" file
89+ */
90+ $ modules_5 = 'admin/modules.php ' ;
91+ $ modules_5_Match = 'isset($errorHandler))$errorHandler->display() ' ;
92+ $ modules_5_Find = '<div class="row no-gutters"> ' ;
93+ $ modules_5_Insert = '
94+ <?php if(isset($errorHandler))$errorHandler->display(); ?>
95+ <div class="row no-gutters">
96+ ' ;
97+
98+ modifyFileContents ($ modules_5 , $ modules_5_Match , $ modules_5_Find , $ modules_5_Insert );
99+
100+
101+
102+ /** SELF-DELETE this file. */
103+ echo 'Now the script will be deleted ' ;
104+ unlink (__FILE__ );
105+
106+
107+
108+ /** ***************************** FUNCTION AREA ****************************************** */
109+
110+ /**
111+ * Modify file contents with desired text to insert
112+ * - search if text to insert is present
113+ * - replace portion of text with another OR insert text to the end of the file
114+ *
115+ * @param string $relativeFilePath - the file relative path as 'folder/anotherFolder/file.ext'
116+ * @param string $matchText - if this text is present the file will be untouched
117+ * @param string $findText - the text where to begin insertion
118+ * @param string $insertText - the text to be inserted
119+ * @param bool $endOfFile - specify if the desired text has to be inserted to the end of the file
120+ *
121+ * @return void
122+ *
123+ */
124+
125+ function modifyFileContents ($ relativeFilePath , $ matchText , $ findText , $ insertText , $ endOfFile = false ) : void
126+ {
127+ try {
128+ /** Check file existence. */
129+ if (file_exists ($ relativeFilePath )) {
130+ $ file = file_get_contents ($ relativeFilePath );
131+ } else {
132+ echo 'File not exists -> ' . $ relativeFilePath . PHP_EOL . '<br> ' ;
133+ }
134+
135+ /** Check if the file NOT contains the text for insertion. */
136+ if (false === strstr ($ file , $ matchText )) {
137+
138+ /** Check if "$endOfFile" is set to false. */
139+ if (false === $ endOfFile ) {
140+ /** Insert the new code lines. */
141+ $ modifiedFileContents =
142+ str_replace (
143+ $ search = $ findText ,
144+ $ replace = $ insertText ,
145+ $ subject = $ file
146+ );
147+
148+ /** Write modified contents to the file. */
149+ file_put_contents ($ relativeFilePath , $ modifiedFileContents );
150+
151+ } else {
152+ /** Append the new code lines and writes to the file. */
153+ file_put_contents ($ relativeFilePath , $ insertText , FILE_APPEND );
154+ }
155+
156+ echo 'Successfully changed ' . $ relativeFilePath . PHP_EOL . '<br> ' ;
157+
158+ } else {
159+ echo 'Changes already applied to ' . $ relativeFilePath . PHP_EOL . '<br> ' ;
160+ }
161+
162+ } catch (Exception $ e ) {
163+ echo $ e ->getMessage ();
164+ }
165+ }
0 commit comments