Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Filmotech publishing API
- (c) 2013 by Pascal PLUCHON
- http://www.filmotech.fr
- */
- /*
- WORDPRESS
- */
- require( '../wp-load.php' );
- include('../wp-admin/includes/taxonomy.php');
- $post_id_maj=null;
- $maj = false;
- require_once("rest.inc.php");
- require_once("json.inc.php");
- require_once("../include/config.inc.php");
- class API extends REST {
- public $data = "";
- private $cfg;
- private $db = NULL;
- // List of allowed methods
- private $services = array("check_server", "check_code", "get_config", "create_poster_directory",
- "get_movie_list", "create_table", "update_publishing_date", "publish" );
- public function __construct(){
- parent::__construct(); // Init parent contructor
- $this->cfg = new CONFIG(); // Init database parameters
- $this->dbConnect(); // Initiate Database connection
- }
- // Database connection
- private function dbConnect(){
- error_reporting(0); // Disable this to see PHP errors
- try
- {
- if ( $this->cfg->DB_TYPE == 'sqlite' ) {
- $db_init = new PDO('sqlite:../'.$this->cfg->DB_NAME.'.sqlite3');
- // $db_init->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Enable this to see PDO errors
- } else {
- $db_init = new PDO('mysql:host='.$this->cfg->DB_SERVER.';dbname='.$this->cfg->DB_NAME,
- $this->cfg->DB_USER, $this->cfg->DB_PASSWORD);
- $db_init->query("SET NAMES UTF8");
- }
- }
- catch (Exception $e)
- {
- $error = array( 'error_msg' => $e->getMessage() );
- $this->response($this->json($error), 412);
- }
- $this->db = $db_init;
- }
- // Public method for access api.
- // This method dynmically call the method based on the query string
- public function processApi(){
- $func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));
- // if((int)method_exists($this,$func) > 0)
- if (in_array($func,$this->services))
- $this->$func();
- else
- $this->response('',404); // If the method not exist with in this class,
- // response would be "Page not found".
- }
- // Check if the service is available
- private function check_server(){
- $success = array('status' => "OK" );
- $this->response($this->json($success),200);
- }
- // Check the security code (API_ACCESS_CODE) and the access method (POST)
- private function check_code(){
- if($this->get_request_method() != "POST")
- {
- $error = array('error_code' => "100" );
- $this->response($this->json($error),401);
- }
- if (!$this->_request['code']) {
- $error = array('error_code' => "101" );
- $this->response($this->json($error),401);
- }
- $code = $this->_request['code'];
- if ($code!=$this->cfg->API_ACCESS_CODE) {
- $error = array('error_code' => "102" );
- $this->response($this->json($error),401);
- }
- }
- // Get the configuration of the API and some parameters
- protected function get_config(){
- $this->check_code();
- $tableau = array('status' => 'OK' );
- $tableau["API_VERSION"] = $this->cfg->API_VERSION;
- $tableau["POSTERS_DIRECTORY"] = $this->cfg->POSTERS_DIRECTORY;
- $tableau["DB_TABLE"] = $this->cfg->DB_TABLE;
- $tableau["PHP_VERSION"] = PHP_VERSION;
- $this->response($this->json($tableau),200);
- }
- // Create poster directory
- private function create_poster_directory(){
- $this->check_code();
- $result = false;
- $repertoire_affiches = '../' . $this->cfg->POSTERS_DIRECTORY;
- if (!is_dir($repertoire_affiches)) {
- $result = mkdir($repertoire_affiches);
- if (!$result)
- {
- $error = array( 'error_code' => '201' );
- $this->response($this->json($error),424);
- }
- }
- if (isset($_POST['forceCHMOD'])) chmod( $repertoire_affiches , 0777 );
- $success = array('status' => 'OK' );
- $this->response($this->json($success),200);
- }
- // Empty poster directory
- private function empty_poster_directory(){
- $this->check_code();
- $repertoire_affiches = '../' . $this->cfg->POSTERS_DIRECTORY;
- foreach (glob($repertoire_affiches.'/Filmotech*.jpg') as $filename) {
- unlink($filename);
- }
- }
- // Return ID/Update date from the database
- private function get_movie_list(){
- $this->check_code();
- /*
- $tableau = array('status' => 'OK' );
- $res = $this->db->query("SELECT ID, DateHeureMAJ FROM " . $this->cfg->DB_TABLE );
- foreach ($res as $row) {
- $tableau[$row['ID']] = $row['DateHeureMAJ'];
- }
- */
- $filename = 'logs.txt';
- if (!$handle = fopen($filename, 'w')) {
- $error = array('error_code' , '400' );
- $this->response($this->json($$error),424);
- }
- $tableau = array('status' => 'OK' );
- $args = array('numberposts' => -1);
- $lastposts = get_posts( $args );
- foreach($lastposts as $post) : setup_postdata($post);
- $ID=get_post_meta($post->ID, 'id_filmotech',true);
- $tableau[$ID] = get_post_meta($post->ID, 'dvd_date_maj',true);
- fwrite($handle, $ID." - ".$tableau[$ID]."\n");
- endforeach;
- wp_reset_postdata();
- fclose($handle);
- $this->response($this->json($tableau),200);
- }
- // Return ID/Update date from the WORDPRESS
- private function get_movie_list_wordpress(){
- $this->check_code();
- $tableau = array('status' => 'OK' );
- /*
- $args = array('numberposts' => -1);
- $lastposts = get_posts( $args );
- foreach($lastposts as $post) : setup_postdata($post);
- //$chaine=the_ID(). $separator .utf8_decode(get_the_title(get_the_ID())). $separator.utf8_decode(get_the_date());
- //$chaine=get_post_meta(get_the_ID(), 'id_filmotech',true). $separator .utf8_decode(get_the_title(get_the_ID())). $separator.utf8_decode(get_the_date("Y-m-d H:i:s"));
- //$titre_=utf8_decode(get_the_title(get_the_ID()));
- //$chaine=get_post_meta(get_the_ID(), 'id_filmotech',true). $separator .html_entity_decode($titre_,ENT_NOQUOTES,"UTF-8"). $separator.utf8_decode(get_the_date("Y-m-d H:i:s"));
- //dvd_date_maj
- //$chaine=get_post_meta(get_the_ID(), 'id_filmotech',true). $separator .html_entity_decode($titre_,ENT_NOQUOTES,"UTF-8"). $separator.get_post_meta(get_the_ID(), 'dvd_date_maj',true);
- $ID=get_post_meta(get_the_ID(), 'id_filmotech',true);
- $tableau[$ID] = get_post_meta(get_the_ID(), 'dvd_date_maj',true);
- endforeach;
- */
- $this->response($this->json($tableau),200);
- }
- private function create_table_sqlite() {
- $sql =
- "CREATE TABLE " . $this->cfg->DB_TABLE . " ("
- . "ID integer NOT NULL PRIMARY KEY,"
- . "DateHeureMAJ TimeStamp NOT NULL default '0000-00-00 00:00:00',"
- . "TitreVF varchar(255) NOT NULL default '',"
- . "TitreVO varchar(255) default '',"
- . "Genre varchar(50) default '',"
- . "Pays varchar(255) default '',"
- . "Annee varchar(10) default '',"
- . "Duree int(11) default '0',"
- . "Note int(11) default '0',"
- . "Synopsis text ,"
- . "Acteurs text ,"
- . "Realisateurs text ,"
- . "Commentaires text ,"
- . "Support varchar(50) default '',"
- . "NombreSupport int(11) default '0',"
- . "Edition varchar(255) default '',"
- . "Zone varchar(10) default '',"
- . "Langues varchar(255) default '',"
- . "SousTitres varchar(255) default '',"
- . "Audio varchar(255) default '',"
- . "Bonus text ,"
- . "EntreeType varchar(255) default '',"
- . "EntreeSource varchar(255) default '',"
- . "EntreeDate date default '0000-00-00',"
- . "EntreePrix float default '0',"
- . "Sortie varchar(10) default '',"
- . "SortieType varchar(255) default '',"
- . "SortieDestinataire varchar(255) default '',"
- . "SortieDate date default '0000-00-00',"
- . "SortiePrix float default '0',"
- . "PretEnCours varchar(10) default '',"
- . "FilmVu varchar(5) default 'NON',"
- . "Reference varchar(255) default '',"
- . "BAChemin varchar(255) default '',"
- . "BAType varchar(10) default '',"
- . "MediaChemin varchar(255) default '',"
- . "MediaType varchar(10) default '');"
- . "CREATE INDEX films_idx ON " . $this->cfg->DB_TABLE . " (TitreVF ASC);";
- try
- {
- $this->db->query($sql);
- $success = array('status' => "OK" );
- $this->response($this->json($success),200);
- }
- catch (Exception $e)
- {
- $this->db->query($sql);
- $success = array('status' => "KO" );
- $this->response($this->json($success),200);
- }
- }
- private function create_table_mysql() {
- $sql = 'CREATE TABLE IF NOT EXISTS `' . $this->cfg->DB_TABLE . '` ('
- . ' `ID` bigint(20) NOT NULL,'
- . ' `DateHeureMAJ` datetime NOT NULL default \'0000-00-00 00:00:00\','
- . ' `TitreVF` varchar(255) NOT NULL default \'\','
- . ' `TitreVO` varchar(255) NOT NULL default \'\','
- . ' `Genre` varchar(50) NOT NULL default \'\','
- . ' `Pays` varchar(255) NOT NULL default \'\','
- . ' `Annee` varchar(10) NOT NULL default \'\','
- . ' `Duree` int(11) NOT NULL default \'0\','
- . ' `Note` int(11) NOT NULL default \'0\','
- . ' `Synopsis` text,'
- . ' `Acteurs` text,'
- . ' `Realisateurs` text,'
- . ' `Commentaires` text,'
- . ' `Support` varchar(50) NOT NULL default \'\','
- . ' `NombreSupport` int(11) NOT NULL default \'0\','
- . ' `Edition` varchar(255) NOT NULL default \'\','
- . ' `Zone` varchar(10) NOT NULL default \'\','
- . ' `Langues` varchar(255) NOT NULL default \'\','
- . ' `SousTitres` varchar(255) NOT NULL default \'\','
- . ' `Audio` varchar(255) NOT NULL default \'\','
- . ' `Bonus` text,'
- . ' `EntreeType` varchar(255) NOT NULL default \'\','
- . ' `EntreeSource` varchar(255) NOT NULL default \'\','
- . ' `EntreeDate` date NOT NULL default \'0000-00-00\','
- . ' `EntreePrix` float NOT NULL default \'0\','
- . ' `Sortie` varchar(10) NOT NULL default \'\','
- . ' `SortieType` varchar(255) NOT NULL default \'\','
- . ' `SortieDestinataire` varchar(255) NOT NULL default \'\','
- . ' `SortieDate` date NOT NULL default \'0000-00-00\','
- . ' `SortiePrix` float NOT NULL default \'0\','
- . ' `PretEnCours` varchar(10) NOT NULL default \'\','
- . ' `FilmVu` varchar(5) NOT NULL default \'NON\','
- . ' `Reference` varchar(255) NOT NULL default \'\','
- . ' `BAChemin` varchar(255) NOT NULL default \'\','
- . ' `BAType` varchar(10) NOT NULL default \'\','
- . ' `MediaChemin` varchar(255) NOT NULL default \'\','
- . ' `MediaType` varchar(10) NOT NULL default \'\','
- . ' PRIMARY KEY (`ID`),'
- . ' KEY `TitreVF` (`TitreVF`)'
- . ' ) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;';
- try
- {
- $this->db->query($sql);
- }
- catch (Exception $e)
- {
- $error = array('error_code' => "200" , 'error_msg' => $e->getMessage() );
- $this->response($this->json($error),424);
- }
- $success = array('status' => "OK" );
- $this->response($this->json($success),200);
- }
- // Create the table in the database
- private function create_table() {
- $this->check_code();
- if ( $this->cfg->DB_TYPE == 'sqlite' )
- { $this->create_table_sqlite(); }
- else
- { $this->create_table_mysql(); }
- }
- // Remove a record and his poster (if any)
- private function del_record() {
- $this->check_code();
- $sql = "DELETE FROM " . $this->cfg->DB_TABLE . " WHERE ID = " . $this->_request['ID'];
- try {
- $this->db->query($sql);
- } catch (Exception $e) {
- $error = array('error_code' => '500' , 'error_msg' => $e->getMessage() );
- $this->response($this->json($error),424);
- }
- $repertoire_affiches = '../' . $this->cfg->POSTERS_DIRECTORY;
- $filename = sprintf($repertoire_affiches.'/Filmotech_%05d.jpg' , $this->_request['ID'] );
- if (file_exists($filename)) { unlink($filename); }
- }
- // Remove a record and his poster (if any) WORDPRESS
- private function del_record_wordpress() {
- $this->check_code();
- //retrouver le post ui correspond a ID
- $args = array('meta_key'=>'id_filmotech', 'meta_value'=>$this->_request['ID']);
- $lastposts = get_posts( $args );
- foreach($lastposts as $post) : setup_postdata($post);
- //On efface l'affiche
- $id_filmo=get_post_meta(get_the_ID(), 'id_filmotech',true);
- $repertoire_affiches = '../' . $this->cfg->POSTERS_DIRECTORY;
- $filename = sprintf($repertoire_affiches.'/Filmotech_%05d.jpg' , $id_filmo );
- if (file_exists($filename)) unlink($filename);
- $post_id_maj=get_the_ID();
- $maj=true;
- //wp_delete_post(get_the_ID());
- endforeach;
- }
- // Add a poster
- private function add_poster() {
- $this->check_code();
- $repertoire_affiches = '../' . $this->cfg->POSTERS_DIRECTORY;
- if (isset($this->_request['Affiche'])) {
- $affiche = base64_decode($this->_request['Affiche']);
- $filename = sprintf($repertoire_affiches.'/Filmotech_%05d.jpg' , $this->_request['ID'] );
- if (!$handle = fopen($filename, 'wb')) {
- $error = array( 'error_code' => '301' );
- $this->response($this->json($error),424);
- }
- if (fwrite($handle, $affiche) === FALSE) {
- $error = array( 'error_code' => '302' );
- $this->response($this->json($error),424);
- }
- fclose($handle);
- if (isset($this->_request['forceCHMOD'])) chmod( $filename , 0777 );
- }
- }
- // Prepare SQL statement according to db type
- private function sql_escape($field) {
- if ( $this->cfg->DB_TYPE == 'sqlite' )
- { return str_replace('\'','\'\'',$field); }
- else
- { return addslashes($field); }
- }
- // Add a record and the poster (if any)
- private function add_record() {
- $this->check_code();
- $champs = array( "DateHeureMAJ", "TitreVF", "TitreVO", "Genre", "Pays", "Annee", "Duree", "Note", "Synopsis", "Acteurs", "Realisateurs", "Commentaires", "Support", "NombreSupport", "Edition", "Zone", "Langues", "SousTitres", "Audio", "Bonus", "EntreeType", "EntreeSource", "EntreeDate", "EntreePrix", "Sortie", "SortieType", "SortieDestinataire", "SortieDate", "SortiePrix", "PretEnCours", "FilmVu", "Reference", "BAChemin", "BAType", "MediaChemin", "MediaType" );
- $sql = 'INSERT INTO ' . $this->cfg->DB_TABLE . '(ID';
- foreach ($champs as $value) {
- $sql .= ', ' . $value;
- }
- $sql .= ') VALUES(\''.$this->_request['ID'].'\'';
- foreach ($champs as $value) {
- $sql .= ', \'' . $this->sql_escape($this->_request[$value]) . '\'';
- }
- $sql .= ");";
- try { $data = $this->db->query($sql);
- }
- catch (Exception $e)
- {
- $tableau = array('error_code' => '300' , 'error_msg' => $e->getMessage() );
- $this->response($this->json($tableau),424);
- }
- $this->add_poster();
- }
- // WORDPRESS
- // Add a record and the poster (if any)
- private function add_record_wordpress(){
- //Gestion des prêts en cours
- $pret = $_POST['PretEnCours'];
- $resultpret= "OUI";
- if ($pret == $resultpret) {
- $pretdef = "<font color= '#FF0000' > Oui </FONT>";
- }
- else {
- $pretdef= "<font color= '#008000' > Non </FONT>";
- }
- //Gestion des films vus
- $vus = $_POST['FilmVu'];
- $resultvus= "OUI";
- if ($vus == $resultvus) {
- $vusdef = "<font color= '#008000' > Oui </FONT>";
- }
- else {
- $vusdef= "<font color= '#FF0000' > Non </FONT>";
- }
- $bonus = $_POST['Bonus'];
- $synopsis = $_POST['Synopsis'];
- $this->check_code();
- $repertoire_affiches = $this->cfg->POSTERS_DIRECTORY;
- $filename = sprintf($repertoire_affiches.'/Filmotech_%05d.jpg' , $this->_request['ID']);
- //Gestion des acteurs pour ajouter un lien pour chacun
- $liens_acteurs="";
- $acteurs=str_replace(array("\r", "\r\n", "\n"),'|',$this->_request['Acteurs'], $count);
- //printf( "Count : " .$count."\n");
- $liste_acteurs = explode("|", $acteurs);
- $virgule=",";
- for($i=0;$i<count($liste_acteurs);$i++) {
- //printf( "liste_acteur : " .str_replace('\n','',$liste_acteurs[$i]));
- if ($i==count($liste_acteurs)-1) $virgule="";//si dernier éléments du tableau, pas de virgule a la fin de la chaine de caractères
- $critere_recherche=str_replace(' ','+',$liste_acteurs[$i]);//On remplace les espaces par des +
- $liens_acteurs.='<a href="?s='.$critere_recherche.'">'.$liste_acteurs[$i].'</a>'.$virgule.' ';
- }
- $id_category = wp_create_category($this->sql_escape($this->_request['Genre']));
- $post_title = stripcslashes($this->sql_escape($this->_request['TitreVF']));
- $post_content=' <div id="affiche">
- <div class="reflection" align="center">
- <img class="reflect" src="' . $filename . '" alt="Affiche">
- </div>
- </div>
- <p align="center">'.$this->sql_escape($this->_request['TitreVO']).'</p>
- <table width="90%" align="center">
- <tr><td><strong>Annee :</strong> '.$_POST['Annee'].'</td><td><strong>Duree :</strong> '.$this->sql_escape($this->_request['Duree']).' mn</td></tr>
- <tr><td><strong>Pays :</strong> '.$_POST['Pays'].'</td><td><strong>Genre :</strong> '.$this->sql_escape($this->_request['Genre']).'</td></tr>
- <tr><td><strong>Support :</strong> '.$_POST['Support'].'</td><td><strong>Zone :</strong> '.$this->sql_escape($this->_request['Zone']).'</td></tr>
- <tr><td><strong>Pret en Cours:</strong> '.$pretdef.'</td><td><strong>Film Vu:</strong> '.$vusdef.'</td></tr>
- </table>
- <p><strong>Synopsis :</strong> '.$synopsis.'</p>
- <!--more-->
- <p><strong>Acteurs :</strong> '.$liens_acteurs.'</p>
- <p><strong>Realisateurs :</strong> '.$this->sql_escape($this->_request['Realisateurs']).'</p>
- <p><strong>Langues :</strong> '.$this->sql_escape($this->_request['langues']).'</p>
- <p><strong>Sous-Titres :</strong> '.$this->sql_escape($this->_request['SousTitres']).'</p>
- <p><strong>Audio :</strong> '.$this->sql_escape($this->_request['Audio']).'</p>
- <p><strong>Bonus : </strong> '.$bonus.'</p>
- <p><strong>Bande-annonce :</strong> </p>
- '.$this->sql_escape($this->_request['BAChemin']); //pas de formatage (mise en page avec <p> retour chariot) de la ligne pour le bon fonctionnement du plugin Youtube
- //str_replace('<br />',', ',nl2br($_POST['Acteurs']))
- $my_post = array(
- 'ID' => $post_id_maj,
- 'post_title' => $post_title,
- 'post_content' => $post_content, //The full text of the post.
- 'post_status' => 'publish',
- 'post_author' => 1, //The user ID number of the author.
- 'post_category' => array($id_category), //Add some categories.
- 'post_date' => $this->sql_escape($this->_request['EntreeDate']), //The time post was made.
- 'post_name' => sanitize_file_name($this->sql_escape($this->_request['TitreVF'])), // The name (slug) for your post
- 'post_type' => 'post', // post, page, link, menu item , custom post type
- 'tags_input' => $this->sql_escape($this->_request['Genre']) // For tags
- );
- // Insert the post into the database
- $id_post=wp_insert_post( $my_post );
- if($maj==false){
- add_post_meta($id_post, "id_filmotech", $this->_request['ID']);
- add_post_meta($id_post, "dvd_cover", $filename);
- add_post_meta($id_post, "dvd_note",$this->_request['note']);
- add_post_meta($id_post, "dvd_ba", $this->_request['BAChemin']);
- add_post_meta($id_post, "dvd_date_maj", $this->_request['DateHeureMAJ']);//DateHeureMAJ
- add_post_meta($id_post, "dvd_pays", $this->_request['Pays']);//Pays
- }
- else {
- update_post_meta($id_post, "id_filmotech", $this->_request['ID']);
- update_post_meta($id_post, "dvd_cover", $filename);
- update_post_meta($id_post, "dvd_note", $this->_request['note']);
- update_post_meta($id_post, "dvd_ba", $this->_request['BAChemin']);
- update_post_meta($id_post, "dvd_date_maj", $this->_request['DateHeureMAJ']);//DateHeureMAJ
- update_post_meta($id_post, "dvd_pays", $this->_request['Pays']);//Pays
- }
- $this->add_poster();
- }
- // Update the last publishing date (shown in the movie list page)
- private function update_publishing_date(){
- $this->check_code();
- $filename = '../update.txt';
- if (!$handle = fopen($filename, 'w')) {
- $error = array('error_code' , '400' );
- $this->response($this->json($$error),424);
- }
- if (fwrite($handle, $_POST['DateMAJ'] ) === FALSE) {
- $error = array('error_code' , '401' );
- $this->response($this->json($error),424);
- }
- fclose($handle);
- $success = array('status' => 'OK' );
- $this->response($this->json($success),200);
- }
- // Main processs, add, update or remove records
- private function publish(){
- $this->check_code();
- if (isset($this->_request['ForceUpdate'])) $this->empty_poster_directory();
- if ($this->_request['ACTION']=='ADD') {
- //$this->add_record();
- /*WORDPRESS*/
- $this->add_record_wordpress();
- }
- if ($this->_request['ACTION']=='UPDATE') {
- //$this->del_record();
- //$this->add_record();
- /*WORDPRESS*/
- $this->del_record_wordpress();
- $this->add_record_wordpress();
- }
- if ($this->_request['ACTION']=='DELETE') {
- //$this->del_record();
- /*WORDPRESS*/
- $this->del_record_wordpress();
- }
- $tableau = array("action" => $this->_request['ACTION'] ,
- "TitreVF" => $this->_request['TitreVF'] , "ID" => $this->_request['ID'] );
- $this->response($this->json($tableau),200);
- }
- // Encode array into JSON
- private function json($data){
- // create a new instance of Services_JSON
- $json = new Services_JSON();
- if(is_array($data)){
- return $json->encode($data);
- }
- }
- }
- // Initiiate Library
- $api = new API;
- $api->processApi();
- ?>
Add Comment
Please, Sign In to add comment