root/afridex/plugins/Flutter/RCCWP_CustomWriteModule.php @ 23

Revision 21, 9.6 kB (checked in by admin, 18 years ago)
Line 
1<?php
2
3/**
4 * @package FlutterDatabaseObjects
5 */
6
7/**
8 * Create/edit/delete modules
9 * @package FlutterDatabaseObjects
10 */
11
12class RCCWP_CustomWriteModule
13{
14       
15        /**
16         * Get all modules
17         *
18         * @return array of objects containing all modules.
19         */
20        function GetCustomModules()
21        {
22                global $wpdb;
23       
24                $sql = "SELECT * FROM " . RC_CWP_TABLE_MODULES;
25               
26                $sql .= " ORDER BY name ASC";
27                $results = $wpdb->get_results($sql);
28                if (!isset($results)) 
29                        $results = array();
30
31                return $results;
32        }
33       
34       
35        /**
36         * Create a new module
37         *
38         * @param string $name module name, the name will be sanitized.
39         * @param string $description module description
40         * @param boolean $create_folders whether to create a folder for the module containing sample template
41         * @return the id of the module or -1 if the module name already exist
42         */
43
44        function Create($name, $description, $create_folders = true)
45        {
46                require_once('RC_Format.php');
47                global $wpdb;
48               
49                $special_chars = array (' ','`','"','\'','\\','/'," ","#","$","%","^","&","*","!","~","‘","\"","’","'","=","?","/","[","]","(",")","|","<",">",";","\\",",");
50                $name = str_replace($special_chars,'',$name);
51
52                //Make sure the module doesn't already exist
53                $sql = "SELECT * FROM " . RC_CWP_TABLE_MODULES .
54                        " WHERE name = '" . $name . "'";
55                if ($wpdb->get_row($sql))       return -1;
56
57                $sql = sprintf(
58                        "INSERT INTO " . RC_CWP_TABLE_MODULES .
59                        " (name, description)" .
60                        " values" .
61                        " (%s, %s)", 
62                        RC_Format::TextToSql($name),
63                        RC_Format::TextToSql($description)
64                );
65                       
66                $wpdb->query($sql);
67                $customWriteModuleId = $wpdb->insert_id;
68
69                if ($create_folders == false)
70                        return $customWriteModuleId;
71
72                // Create template folder
73                $moduleTemplateFolder = dirname(__FILE__)."/modules/".$name;
74                if (@mkdir($moduleTemplateFolder, 0777)){
75                        @chmod($moduleTemplateFolder, 0777);
76                        if (mkdir($moduleTemplateFolder.'/templates', 0777)){
77                                @chmod($moduleTemplateFolder.'/templates', 0777);
78                                if (mkdir($moduleTemplateFolder.'/templates/default', 0777)){   
79                                        @chmod($moduleTemplateFolder.'/templates/default', 0777);
80                                        RCCWP_CustomWriteModule::CreateTemplateFiles($moduleTemplateFolder.'/templates/default/small', $name);
81                                }
82                        }
83
84                        //Create basic configuration file
85                        $configurationFileContent = 
86<<<EOF
87        <?xml version="1.0"?>
88        <canvas>
89                <function>
90                        <author>
91                        Flutter
92                        </author>
93                        <description>
94                        This is module $name.
95                        </description>
96                        <variables>
97                                <variable>
98                                        <name>
99                                        the_text
100                                        </name>
101                                        <description>
102                                        Text to display (HTML allowed)
103                                        </description>
104                                        <type>
105                                        Textarea
106                                        </type>
107                                        <default>
108                                        <![CDATA[<p>This is some text generated by $name module. </p>]]>
109                                        </default>
110                                </variable>
111                        </variables>
112                </function>
113        </canvas>
114EOF;
115
116       
117                        file_put_contents ($moduleTemplateFolder."/configure.xml", $configurationFileContent); 
118                        @chmod($moduleTemplateFolder."/configure.xml", 0666);
119                }
120               
121                return $customWriteModuleId;
122        }
123
124        /**
125         * @access private
126         */
127        function CreateTemplateFiles($templatePath, $name){
128                        $defaultFileContent = 
129<<<EOF
130        <?php
131                echo '<div class="block">';
132                echo mod_var('the_text');
133                echo '</div>';
134        ?>
135EOF;
136
137                if (mkdir($templatePath, 0777)){
138                        @chmod($templatePath, 0777);
139                        file_put_contents ($templatePath."/default.php", $defaultFileContent);
140                        @chmod($templatePath."/default.php", 0666);
141                }
142        }
143       
144        /**
145         * Deletes a module and all its child fields as well as the module folder.
146         *
147         * @param integer $customWriteModuleId module id.
148         */
149       
150        function Delete($customWriteModuleId = null)
151        {
152                include_once ('RCCWP_CustomGroup.php');
153                if (isset($customWriteModuleId))
154                {
155                        global $wpdb;
156                       
157                        $customWriteModule = RCCWP_CustomWriteModule::Get($customWriteModuleId);
158                        $customWriteModuleName = $customWriteModule->name;
159                       
160                        $sql = sprintf(
161                                "DELETE FROM " . RC_CWP_TABLE_MODULES .
162                                " WHERE id = %d",
163                                $customWriteModuleId
164                                );
165                        $wpdb->query($sql);
166
167                        // Remove template folder
168                        if (!empty($customWriteModuleName)){
169                                $moduleTemplateFolder = dirname(__FILE__)."/modules/".$customWriteModuleName;
170                                RCCWP_CustomWriteModule::remove_dir($moduleTemplateFolder);
171                        }
172
173                        // Remove canvas info
174                        include_once "canvas-import_functions.php";
175                        canvas_delete_block_by_mod_ID($customWriteModuleId);
176                       
177                }
178               
179        }
180
181        /**
182         * @access private
183         */
184        function remove_dir($dir)
185        {
186                if(is_dir($dir))
187                {
188                $dir = (substr($dir, -1) != "/")? $dir."/":$dir;
189                $openDir = opendir($dir);
190                while($file = readdir($openDir))
191                {
192                        if(!in_array($file, array(".", "..")))
193                        {
194                        if(!is_dir($dir.$file))
195                                @unlink($dir.$file);
196                        else
197                                RCCWP_CustomWriteModule::remove_dir($dir.$file);
198                        }
199                }
200                closedir($openDir);
201                @rmdir($dir);
202                }
203        }
204       
205        /**
206         * Retrieves the basic information of the module.
207         *
208         * @param integer $customWriteModuleId module id
209         * @return an object containing id, name and description
210         */
211        function Get($customWriteModuleId)
212        {
213                global $wpdb;
214       
215                $sql = "SELECT * FROM " . RC_CWP_TABLE_MODULES .
216                        " WHERE id = " . (int)$customWriteModuleId;
217               
218                $results = $wpdb->get_row($sql);
219               
220                return $results;
221        }
222
223        /**
224         * Retrieves the basic information of the module given module name.
225         *
226         * @param string $customWriteModuleName module name
227         * @return an object containing id, name and description
228         */
229        function GetByName($customWriteModuleName)
230        {
231                global $wpdb;
232       
233                $sql = "SELECT * FROM " . RC_CWP_TABLE_MODULES .
234                        " WHERE name = '$customWriteModuleName'";
235               
236                $results = $wpdb->get_row($sql);
237               
238                return $results;
239        }
240       
241        /**
242         * Updates the basic information of a module
243         *
244         * @param integer $customWriteModuleId the id of the module that will be updated
245         * @param string $name new name
246         * @param string $description new description
247         * @return the id of the module or -1 if the module name already exist
248         */
249       
250        function Update($customWriteModuleId, $name, $description)
251        {
252                require_once('RC_Format.php');
253                global $wpdb;
254               
255                //$capabilityName = RCCWP_CustomWriteModule::GetCapabilityName($name);
256                $special_chars = array (' ','`','"','\'','\\','/'," ","#","$","%","^","&","*","!","~","‘","\"","’","'","=","?","/","[","]","(",")","|","<",">",";","\\",",");
257                $name = str_replace($special_chars,'',$name);
258
259                //Make sure the module doesn't already exist
260                $sql = "SELECT * FROM " . RC_CWP_TABLE_MODULES .
261                        " WHERE name = '" . $name . "' AND id <> $customWriteModuleId";
262                if ($wpdb->get_row($sql))       return -1;
263
264                //Get old name
265                $sql = "SELECT name FROM " . RC_CWP_TABLE_MODULES .
266                        " WHERE id = $customWriteModuleId";
267                $originalModName = $wpdb->get_var($sql);
268                $oldModuleTemplateFolder = dirname(__FILE__)."/modules/".$originalModName;
269
270                // Update name
271                $sql = sprintf(
272                        "UPDATE " . RC_CWP_TABLE_MODULES .
273                        " SET name = %s,".
274                        "     description = %s".
275                        " where id = %d",
276                        RC_Format::TextToSql($name),
277                        RC_Format::TextToSql($description),
278                        $customWriteModuleId );
279               
280                $wpdb->query($sql);
281
282                //Rename module folder
283                $newModuleTemplateFolder = dirname(__FILE__)."/modules/".$name;
284                rename($oldModuleTemplateFolder, $newModuleTemplateFolder);
285               
286                return $customWriteModuleId;
287               
288        }
289
290       
291        /**
292         * Import a module given the zip file path. Importing a module means inserting it in
293         * the database and copying its folder to modules folder. If a module with the same
294         * name already exists, the function will append a number to the module name. You must
295         * have either php ZipArchive extension or unzip program installed.
296         *
297         * @param string $zipFilePath the full path of the zip file
298         * @param string $moduleName the module name, if this value if false, the function will
299         *                                                      use the zip filename as the module name. The default value is false
300         * @return the module id, or false in case of error.
301         */
302
303        function Import($zipFilePath, $moduleName = false)
304        {
305                include_once('RCCWP_CustomGroup.php');
306                include_once('RCCWP_CustomField.php');
307                include_once('RCCWP_CustomWritePanel.php');
308                include_once('RCCWP_Application.php');
309                include_once('RCCWP_CustomWritePanel.php');
310
311                if (!$moduleName)
312                        //use zip filename
313                        $moduleName = basename($zipFilePath, ".zip");
314
315                if ($moduleName == '') return false;
316
317                // Append a number if the module already exists,
318                $i = 1;
319                $newModuleName = $moduleName;
320                while (file_exists(FLUTTER_MODULES_DIR.$newModuleName)){
321                        $newModuleName = $moduleName. "_" . $i++;
322                }
323                $moduleName = $newModuleName;
324
325
326                mkdir(FLUTTER_MODULES_DIR.$moduleName);
327                chmod(FLUTTER_MODULES_DIR.$moduleName, 0777);
328
329                $destPath = FLUTTER_MODULES_DIR.$moduleName.DIRECTORY_SEPARATOR;
330
331                if (class_exists('ZipArchive')){
332                        $zip = new ZipArchive() ;
333
334                        // open archive
335                        if ($zip->open($zipFilePath) !== TRUE) {
336                                die ("Could not open archive");
337                        }
338                       
339                        // extract contents to destination directory
340                        $zip->extractTo($destPath);
341                       
342                        // close archive
343                        $zip->close();
344                }else{
345                        if (RCCWP_Application::CheckDecompressionProgramUnzip()) 
346                                $command = "unzip $zipFilePath -d ". $destPath;
347                        else{
348                                die("Cannot find unzip program!");
349                        }
350                        exec($command, $out, $err);
351                }
352                $moduleInfo_imported_data = unserialize(file_get_contents($destPath.'module_info.exp'));
353               
354                //Import module
355                $moduleID = RCCWP_CustomWriteModule::Create($moduleName, $moduleInfo_imported_data['moduleinfo']->description, false);
356               
357                //Import any panels
358                if ($handle = @opendir($destPath)) {
359                        while (false !== ($file = readdir($handle))) { 
360                                if (substr($file, 0, 1) == "_"){
361                                        $panelName = basename(substr($file, 1), ".pnl");
362                                        RCCWP_CustomWritePanel::Import($destPath.$file, $panelName);
363                                }
364                        }
365                }
366               
367                //Create Duplicates
368                foreach($moduleInfo_imported_data['duplicates'] as $moduleDuplicate){
369                        RCCWP_ModuleDuplicate::Create($moduleID, $moduleDuplicate->duplicate_name);
370                }               
371               
372                return $moduleID;
373        }
374}
375?>
Note: See TracBrowser for help on using the browser.